Tensorflow:我的模型的预测总是相同的



我已经训练了一个深度CNN,可以预测一维数组,并以.ckpt格式保存权重变量。但是当我给模型新的输入时,它总是输出相同的数组。我已经检查了输入的预处理,我相信它们没问题。这是我预测的代码。

import pandas as pd
import numpy as np
import os
import tensorflow as tf
sess = tf.Session()
sess.run(tf.global_variables_initializer())
filename = os.listdir("D:/project/test datasets/image")
new_dir  = "D:/project/test datasets/"
for img in filename:
img=os.path.splitext(img)[0]
xs = pd.read_csv(new_dir+img+'.csv',index_col=0)
xs = xs.values.flatten()
xs = np.expand_dims(xs,0)
saver = tf.train.import_meta_graph('model.ckpt.meta')
saver.restore(sess, 'model.ckpt')  
graph = tf.get_default_graph()  
x = graph.get_tensor_by_name("x:0")  
keep_prob = graph.get_tensor_by_name("keep_prob:0")
y_conv = graph.get_tensor_by_name("y_conv:0")
print(sess.run(y_conv,feed_dict={x:xs,keep_prob:1.0}))

而且我还发现,当我在循环末尾添加代码语句y_conv = tf.constant(0)时,以下输出将全部0,这意味着我的预测y_conv不会在每个循环中更新。

我不知道哪里出了问题。任何反馈或建议将不胜感激。

你的代码对我来说看起来不错。请您尝试以下格式

with tf.Session() as sess:
saver = tf.train.import_meta_graph(savefile)
saver.restore(sess, tf.train.latest_checkpoint(savedir))
graph = tf.get_default_graph()
input_x = graph.get_tensor_by_name("input_x:0")
result = graph.get_tensor_by_name("result:0")
feed_dict = {input_x: x_data,}
predictions = result.eval(feed_dict=feed_dict)

最新更新