Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Tensorflow,如何从数组恢复变量?_Tensorflow - Fatal编程技术网

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变量