Tensorflow恢复模型和预测

时间:2017-04-02 19:59:40

标签: 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 个答案:

答案 0 :(得分:4)

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

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

将占位符替换为原始图形中的名称