2017-04-02 236 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] 

替換不管它的名字是在原有圖形

相關問題