2017-04-02 244 views
2

我已经训练了一个Tensorflow模型并保存了输出层的张量。恢复时,我恢复了输出层的张量,并尝试用它进行预测,但得到一个错误,说我从来没有分配到占位符。我的代码如下,请协助。Tensorflow恢复模型和预测

with tf.Session() as sess: 
model_saver = tf.train.import_meta_graph(model_save_folder + '/my-model.meta') 
model_saver.restore(sess, model_save_folder + '/my-model') 
x = tf.placeholder('float') 
output = tf.get_collection("output")[0] #output will be the tensor for model's last layer 
print("Model restored.") 
print('Initialized') 
#print(sess.run(tf.get_default_graph().get_tensor_by_name('w_conv1:0'))) 

#collect list of preprocessed data on submission set 
inputData = [] 
with open('stage1_sample_submission.csv') as f: 
    reader = csv.reader(f) 
    num = 0 

    for row in reader: 
     if num > 0: 
      patient = row[0] 
      #print(patient) 
      inputData.append(process_data(patient, img_px_size=IMG_SIZE_PX, hm_slices=SLICE_COUNT)) 
     num += 1 

#prediction! 
prediction = sess.run(output, feed_dict={x: inputData}) 
print(prediction) 
+1

我认为你需要恢复占位符以同样的方式占位符。 x = tf.get_collection(“placeholder”)[0]将占位符替换为原始图中的名称。 – Steven

+0

谢谢,它的工作原理。 –

+0

我只是将它发布为答案,以便您可以关闭该问题。 – Steven

回答

4

您需要以相同的方式恢复占位符。

x = tf.get_collection("placeholder")[0] 

替换不管它的名字是在原有图形

相关问题