2016-09-18 131 views
0

我想節省一些變量(重量和偏見)後使用它們,但我已經檢測到的錯誤,我不知道如果我的步驟是正確與否:保存tensorflow一些變量

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") 

當我運行代碼時,我得到這個錯誤:

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

您定義了兩個詞典:權重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')