2017-06-22 100 views
0

我用一个例子有变量,如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 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?

回答

1

你只需

weights["wc1"] 

命令tf.get_variable以另一种方式用于对变量的引用,如果你想用它来恢复已经创建了一个变量,你需要在一个变量的作用域与​​,并使用张量流与变量相关联的名称,而不是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) 

现在vv_again都指向同一个tensorflow变量两个Python变量。