无法为从图形加载的张量变量赋值

时间:2017-08-09 17:19:16

标签: python tensorflow

我训练了一个模型并保存了它。现在,我试图看看权重扰动如何影响其准确性,所以我需要修改我的权重变量上保存的值,实质上是为它添加一些噪音。问题是我加载后无法为它们赋值。我正在使用tensorflow版本1.2.1。来训练和加载模型。这是我的代码:

import tensorflow as tf
tf.reset_default_graph()
sess = tf.InteractiveSession()
saver = tf.train.import_meta_graph('/scratch/pedro/TFModels/Checks_and_Logs/20170803_215828/beta_model-1.meta')
print("Graph restored")
saver.restore(sess, tf.train.latest_checkpoint('/scratch/pedro/TFModels/Checks_and_Logs/20170803_215828/'))
print("Model restored")
tf.global_variables() #prints the list of variables in the graph

这会产生以下输出:

[<tf.Variable 'FF_NN/Model/hidden_layer_1/weights/Variable:0' shape=(3960, 256) dtype=float32_ref>,
 <tf.Variable 'FF_NN/Model/hidden_layer_1/bias/bias_hidden_layer_1:0' shape=(256,) dtype=float32_ref>,
 <tf.Variable 'FF_NN/Model/hidden_layer_2/weights/Variable:0' shape=(256, 256) dtype=float32_ref>,
 <tf.Variable 'FF_NN/Model/hidden_layer_2/bias/bias_hidden_layer_2:0' shape=(256,) dtype=float32_ref>,
 <tf.Variable 'FF_NN/Model/hidden_layer_3/weights/Variable:0' shape=(256, 256) dtype=float32_ref>,
 <tf.Variable 'FF_NN/Model/hidden_layer_3/bias/bias_hidden_layer_3:0' shape=(256,) dtype=float32_ref>,
 <tf.Variable 'FF_NN/Model/output_layer/weights/Variable:0' shape=(256, 5) dtype=float32_ref>,
 <tf.Variable 'FF_NN/Model/output_layer/bias/bias_output_layer:0' shape=(5,) dtype=float32_ref>,
 <tf.Variable 'FF_NN/Model/Training/Optimizer/Variable:0' shape=() dtype=int32_ref>,
 <tf.Variable 'FF_NN/Model/Training/Optimizer/beta1_power:0' shape=() dtype=float32_ref>,
 <tf.Variable 'FF_NN/Model/Training/Optimizer/beta2_power:0' shape=() dtype=float32_ref>,
 <tf.Variable 'FF_NN/Model/Training/Optimizer/FF_NN/Model/hidden_layer_1/weights/Variable/Adam:0' shape=(3960, 256) dtype=float32_ref>,
 <tf.Variable 'FF_NN/Model/Training/Optimizer/FF_NN/Model/hidden_layer_1/weights/Variable/Adam_1:0' shape=(3960, 256) dtype=float32_ref>,
 <tf.Variable 'FF_NN/Model/Training/Optimizer/FF_NN/Model/hidden_layer_1/bias/bias_hidden_layer_1/Adam:0' shape=(256,) dtype=float32_ref>,
 <tf.Variable 'FF_NN/Model/Training/Optimizer/FF_NN/Model/hidden_layer_1/bias/bias_hidden_layer_1/Adam_1:0' shape=(256,) dtype=float32_ref>,
 <tf.Variable 'FF_NN/Model/Training/Optimizer/FF_NN/Model/hidden_layer_2/weights/Variable/Adam:0' shape=(256, 256) dtype=float32_ref>,
 <tf.Variable 'FF_NN/Model/Training/Optimizer/FF_NN/Model/hidden_layer_2/weights/Variable/Adam_1:0' shape=(256, 256) dtype=float32_ref>,
 <tf.Variable 'FF_NN/Model/Training/Optimizer/FF_NN/Model/hidden_layer_2/bias/bias_hidden_layer_2/Adam:0' shape=(256,) dtype=float32_ref>,
 <tf.Variable 'FF_NN/Model/Training/Optimizer/FF_NN/Model/hidden_layer_2/bias/bias_hidden_layer_2/Adam_1:0' shape=(256,) dtype=float32_ref>,
 <tf.Variable 'FF_NN/Model/Training/Optimizer/FF_NN/Model/hidden_layer_3/weights/Variable/Adam:0' shape=(256, 256) dtype=float32_ref>,
 <tf.Variable 'FF_NN/Model/Training/Optimizer/FF_NN/Model/hidden_layer_3/weights/Variable/Adam_1:0' shape=(256, 256) dtype=float32_ref>,
 <tf.Variable 'FF_NN/Model/Training/Optimizer/FF_NN/Model/hidden_layer_3/bias/bias_hidden_layer_3/Adam:0' shape=(256,) dtype=float32_ref>,
 <tf.Variable 'FF_NN/Model/Training/Optimizer/FF_NN/Model/hidden_layer_3/bias/bias_hidden_layer_3/Adam_1:0' shape=(256,) dtype=float32_ref>,
 <tf.Variable 'FF_NN/Model/Training/Optimizer/FF_NN/Model/output_layer/weights/Variable/Adam:0' shape=(256, 5) dtype=float32_ref>,
 <tf.Variable 'FF_NN/Model/Training/Optimizer/FF_NN/Model/output_layer/weights/Variable/Adam_1:0' shape=(256, 5) dtype=float32_ref>,
 <tf.Variable 'FF_NN/Model/Training/Optimizer/FF_NN/Model/output_layer/bias/bias_output_layer/Adam:0' shape=(5,) dtype=float32_ref>,
 <tf.Variable 'FF_NN/Model/Training/Optimizer/FF_NN/Model/output_layer/bias/bias_output_layer/Adam_1:0' shape=(5,) dtype=float32_ref>]

所以,我一直在尝试修改第一个(FF_NN / Model / hidden_​​layer_1 / weights / Variable:0),但这给了我一个错误:

x = data_train[:batch_size]
y = data_train_labels[:batch_size]
graph = tf.get_default_graph()
data_train_tensor = graph.get_tensor_by_name("Train_Dataset:0")
data_train_labels_onehot = graph.get_tensor_by_name("Train_Labels:0")
acc_te = graph.get_tensor_by_name("Test_Data_Accuracy/Mean:0")
acc_tr = graph.get_tensor_by_name("Train_Data_Accuracy/Mean:0")


w1 = graph.get_tensor_by_name("FF_NN/Model/hidden_layer_1/weights/Variable:0")
print('w1:\n', w1.eval())
training_acc, test_acc = sess.run([acc_tr, acc_te], feed_dict={data_train_tensor: x, data_train_labels_onehot: y})
print(test_acc)

w1 = w1 + 50
print('w1:\n', w1.eval())
sess.run(w1.assign(w1))
training_acc, test_acc, _ = sess.run([acc_tr, acc_te, w1], feed_dict={data_train_tensor: x, data_train_labels_onehot: y})
print(test_acc) 

这在赋值操作中给出了一个错误:

w1:
 [[-0.0531723   0.73768502  0.14098917 ...,  1.67111528  0.2495033
   0.20415793]
 [ 1.20964873 -0.99254322 -3.01407313 ...,  0.40427083  0.33289135
   0.2326804 ]
 [ 0.70157909 -1.61257529 -0.59762233 ...,  0.20860809 -0.02733657
   1.57942903]
 ..., 
 [ 1.23854971 -2.28062844 -1.01647282 ...,  1.18426156  0.65342903
  -0.45519635]
 [ 1.02164841 -0.11143603  1.71673298 ..., -0.85511237  1.15535712
   0.50917912]
 [-2.52524352 -0.04488864  0.66239733 ..., -0.45516238 -0.76003599
  -1.2073245 ]]
0.242335
w1:
 [[ 49.94682693  50.73768616  50.1409874  ...,  51.67111588  50.24950409
   50.20415878]
 [ 51.20964813  49.00745773  46.98592758 ...,  50.40427017  50.33288956
   50.23268127]
 [ 50.70158005  48.38742447  49.40237808 ...,  50.20860672  49.97266388
   51.57942963]
 ..., 
 [ 51.23854828  47.7193718   48.98352814 ...,  51.18426132  50.65342712
   49.54480362]
 [ 51.02164841  49.88856506  51.71673203 ...,  49.14488602  51.15535736
   50.50917816]
 [ 47.47475815  49.95511246  50.66239548 ...,  49.54483795  49.23996353
   48.79267502]]
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-34-da5d05216392> in <module>()
     16 w1 = w1 +50
     17 print('w1:\n', w1.eval())
---> 18 sess.run(w1.assign(w1))
     19 #print('w1:\n', w1.eval())
     20 training_acc, test_acc, _ = sess.run([acc_tr, acc_te, w1], feed_dict={data_train_tensor: x, data_train_labels_onehot: y})

AttributeError: 'Tensor' object has no attribute 'assign'

所有类似的问题都指出,w1应该是一个tf.Variable类型,根据tf.global_variables()的输出,这似乎就是这种情况。

2 个答案:

答案 0 :(得分:3)

以下代码可行。使用get_variable

的最佳方式
w1 = tf.get_variable("FF_NN/Model/hidden_layer_1/weights/Variable:0")
sess.run(tf.assign(w1, w1+50))

现在这一步不起作用,这是张量流https://github.com/tensorflow/tensorflow/issues/1325中的一个错误

一个有效的解决方案:

w1 = [v for v in tf.global_variables() if v.name=="FF_NN/Model/hidden_layer_1/weights/Variable:0"][0]
sess.run(tf.assign(w1, w1+50))

答案 1 :(得分:-1)

您需要使用tf.get_variabletf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES)[0]

获取基础变量对象

Ishant提到get_variable目前存在范围变量的错误,所以在修复之前你需要使用get_collection