在tensorflow中保存一些变量

时间:2016-09-18 11:24:55

标签: python tensorflow convolution

我试图保存一些变量(重量和偏差)以便稍后使用它们但我检测到错误,我不知道我的步骤是否正确:

graph = tf.Graph()

with graph.as_default():

   weights = {
    'wc1_0': tf.Variable(tf.random_normal([patch_size_1, patch_size_1, num_channels, depth],stddev=0.1)),
    'wc1_1': tf.Variable(tf.random_normal([patch_size_2, patch_size_2, depth, depth], stddev=0.1)), 
     ......
     }

   biases = {
    'bc1_0' : tf.Variable(tf.zeros([depth])), 
    'bc1_1' : tf.Variable(tf.constant(1.0, shape=[depth])),
     .....
     }

def model(data):

   conv_1 = tf.nn.conv2d(data, wc1_0 , [1, 2, 2, 1], padding='SAME')

   hidden_1 = tf.nn.relu(conv_1 + bc1_0)

   pool_1 = tf.nn.max_pool(hidden_1,ksize = [1,5,5,1], strides= [1,2,2,1],padding ='SAME' )
   .......
   .......

weights_saver = tf.train.Saver(var_list=weights)
biases_saver = tf.train.Saver(var_list=biases)

with tf.Session(graph=graph) as sess:

   sess.run()
   for loop....
   ......
   save_path_weights = weights_saver.save(sess, "my_path")
   save_path_biases = biases_saver.save(sess, "my_path")

当我运行代码时,我得到了这个erorr:

  conv_1 = tf.nn.conv2d(data, wc1_0 , [1, 2, 2, 1], padding='SAME')
  NameError: global name 'wc1_0' is not defined

如何在conv_1中分配变量?

1 个答案:

答案 0 :(得分:1)

您定义了两个词典:1个用于权重,1个用于偏见。 你已经用Tensorflow变量对象填充了字典..所以,你为什么不使用它们呢?

   conv_1 = tf.nn.conv2d(data,  weights['wc1_0'] , [1, 2, 2, 1], padding='SAME')
   hidden_1 = tf.nn.relu(conv_1 + biases['bc1_0'])
   pool_1 = tf.nn.max_pool(hidden_1,ksize = [1,5,5,1], strides= [1,2,2,1],padding ='SAME' )