Tensorflow,如何从数组恢复变量?
我举了一个例子,有如下变量Tensorflow,如何从数组恢复变量?,tensorflow,Tensorflow,我举了一个例子,有如下变量 weights = { # 5x5 conv, 1 input, 32 outputs 'wc1': tf.Variable(tf.random_normal([5, 5, 1, 32])), # 5x5 conv, 32 inputs, 64 outputs 'wc2': tf.Variable(tf.random_normal([5, 5, 32, 64])), # fully connected, 7*7*64 input
weights = {
# 5x5 conv, 1 input, 32 outputs
'wc1': tf.Variable(tf.random_normal([5, 5, 1, 32])),
# 5x5 conv, 32 inputs, 64 outputs
'wc2': tf.Variable(tf.random_normal([5, 5, 32, 64])),
# fully connected, 7*7*64 inputs, 1024 outputs
'wd1': tf.Variable(tf.random_normal([7*7*64, 1024])),
# 1024 inputs, 10 outputs (class prediction)
'out': tf.Variable(tf.random_normal([1024, n_classes]))
}
biases = {
'bc1': tf.Variable(tf.random_normal([32])),
'bc2': tf.Variable(tf.random_normal([64])),
'bd1': tf.Variable(tf.random_normal([1024])),
'out': tf.Variable(tf.random_normal([n_classes]))
}
所以我不能使用下面的代码来恢复变量
wc1 = tf.get_variables("weights[wc1]")
那么如何使用tensorflow恢复变量呢?您只需通过
weights["wc1"]
命令tf.get_variable
以另一种方式使用,如果要使用它来恢复已创建的变量,则需要位于reuse=True
的变量范围内,并使用tensorflow与该变量关联的名称,而不是python指针。例如:
with tf.variable_scope('var_scope'):
v = tf.Variable(5, shape=(), dtype=tf.float32, name='my_var')
with tf.variable_scope('var_scope', reuse=True):
v_again = tf.get_variable(name='my_var', dtype=tf.float32)
现在v
和v_
是指向同一tensorflow变量的两个python变量