2017-04-07 141 views
1

我是機器學習和Tensorflow的新手,通過使用其示例教程源代碼,模型得到訓練和打印的準確性,但它不包括源代碼導出模型和預測新圖像的變量和導入。Tensorflow報告錯誤與恢復的訓練模型

因此,我修改了源代碼以導出模型,並使用測試數據集創建了一個新的python腳本來進行預測。

這裏是源代碼導出的人才培養模式:

mnist = input_data.read_data_sets(FLAGS.data_dir, one_hot=True) 
print("run here3") 
# Create the model 
x = tf.placeholder(tf.float32, [None, 784], name="x") 
W = tf.Variable(tf.zeros([784, 10]), name="W") 
b = tf.Variable(tf.zeros([10])) 
y = tf.matmul(x, W) + b 
saver = tf.train.Saver() 
sess = tf.InteractiveSession() 
... ignore the source code for the cost function definition and train the model 
#after the model get trained, save the variables and y 
tf.add_to_collection('W', W) 
tf.add_to_collection('b', b) 
tf.add_to_collection('y', y) 

saver.save(sess, 'result') 

在新的Python腳本,我試圖恢復模型,並重新執行y功能

sess = tf.Session() 
saver = tf.train.import_meta_graph('result.meta') 
saver.restore(sess, tf.train.latest_checkpoint('./')) 
W = tf.get_collection('W')[0] 
b = tf.get_collection('b')[0] 
y = tf.get_collection('y')[0] 


mnist = input_data.read_data_sets('/tmp/tensorflow/mnist/input_data', one_hot=True) 
img = mnist.test.images[0] 
x = tf.placeholder(tf.float32, [None, 784]) 
sess.run(y, feed_dict={x: mnist.test.images}) 

一切正常正確地說,如果我打印它們,我可以得到W和b值,但是在執行最後一個語句(運行y函數)時出現錯誤。

Caused by op u'x', defined at: 
File "predict.py", line 58, in <module> 
tf.app.run(main=main, argv=[sys.argv[0]] + unparsed) 
File "/Users/zhouqi/git/machine-learning/tensorflow/lib/python2.7/site-packages/tensorflow/python/platform/app.py", line 44, in run 
_sys.exit(main(_sys.argv[:1] + flags_passthrough)) 
File "predict.py", line 25, in main 
saver = tf.train.import_meta_graph('result.meta') 
File "/Users/zhouqi/git/machine-learning/tensorflow/lib/python2.7/site- packages/tensorflow/python/training/saver.py", line 1566, in import_meta_graph 
**kwargs) 
File "/Users/zhouqi/git/machine-learning/tensorflow/lib/python2.7/site-packages/tensorflow/python/framework/meta_graph.py", line 498, in import_scoped_meta_graph 
producer_op_list=producer_op_list) 
File "/Users/zhouqi/git/machine-learning/tensorflow/lib/python2.7/site-packages/tensorflow/python/framework/importer.py", line 288, in import_graph_def 
op_def=op_def) 
File "/Users/zhouqi/git/machine-learning/tensorflow/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 2327, in create_op 
original_op=self._default_original_op, op_def=op_def) 
File "/Users/zhouqi/git/machine-learning/tensorflow/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 1226, in __init__ 
self._traceback = _extract_stack() 

InvalidArgumentError (see above for traceback): You must feed a value for placeholder tensor 'x' with dtype float 
[[Node: x = Placeholder[dtype=DT_FLOAT, shape=[], _device="/job:localhost/replica:0/task:0/cpu:0"]()]] 

這很奇怪,因爲我確實使用了同樣的語句來定義x和進給X使用相同的方法在執行y功能,我不知道爲什麼它不工作?

+0

爲什麼你用'mnist = input_data.read_data_sets('/ tmp/tensorflow/mnist/input_data',one_hot = True)替換這一行:'mnist = input_data.read_data_sets(FLAGS.data_dir,one_hot = True)'' ? – tagoma

+0

哦,對於我爲恢復模型而創建的新腳本,我將其簡化爲使用硬編碼的數據文件夾,FLAGS.data_dir與/ tmp/tensorflow/mnist/input_data相同。 – mailme365

回答

1

的問題是新的佔位符:

x = tf.placeholder(tf.float32, [None, 784]) 

創建具有相同名稱的佔位符是不夠的。實際上您需要創建模型時使用的相同佔位符。因此,你還必須添加x到集合中的第一個文件:

tf.add_to_collection('x', x) 

,並在新文件中加載:

x = tf.get_collection('x')[0] 

,而不是創建一個新的。