问题:
尝试通过tf.train.import_meta_graph("saved_models/model.meta")
恢复meta_graph会出现以下错误:
InvalidArgumentError (see above for traceback): Shape [-1] has negative dimensions
[[Node: Placeholder_2 = Placeholder[dtype=DT_INT32, shape=[?], _device="/job:localhost/replica:0/task:0/cpu:0"]()]]
占位符的形状和传递的数据如下: 占位符:(?,?,50) 数据:(1,2,50)
<小时/> 参与的代码:
self.x_placeholder_input = tf.placeholder(tf.float32, shape=[None, None, n_inputs])
另一个占位符(标签):self.y_placeholder_label = tf.placeholder(tf.int32, shape=[None, self.num_of_classes])
预测方法:
def predict(self):
with tf.name_scope("predict"):
with tf.Session(graph=tf.Graph()) as sess:
saver = tf.train.import_meta_graph("saved_models/model.meta")
saver.restore(sess, "saved_models/model")
graph = tf.get_default_graph()
output = graph.get_tensor_by_name("optimize/cal_loss/model_network/model_network_NN_network/output/BiasAdd:0")
x_placeholder = graph.get_tensor_by_name("Placeholder:0")
print x_placeholder.shape
print np.array(self.data_x).shape
print sess.run(output, feed_dict={x_placeholder: self.data_x})
训练方法:
def train(self):
writer = writer = tf.summary.FileWriter("mygraph/logs", tf.get_default_graph())
num_of_epoch = 10
with tf.Session() as sess:
for epoch in range(num_of_epoch):
# initialise all variables
optimize = self.optimize
sess.run(tf.global_variables_initializer())
sess.run(optimize,
feed_dict={self.x_placeholder_input: np.array(self.data_x),
self.y_placeholder_label: np.array(self.data_y),
self.sq_placeholder_seq_length: np.array(self.seq_length)})
if num_of_epoch % 10 == 0:
# Create Saver to save model
print "Cycle " + str(epoch) + " out of " + str(num_of_epoch) + " done"
saver = tf.train.Saver()
location = saver.save(sess, "saved_models/model")
print "Model saved to : " + str(location)
<小时/> 问题:在定义它的形状时,问题是由占位符 两个 引起的?虽然训练很好。 **完整代码(如果有帮助):** (https://gist.github.com/duemaster/660208e6cd7856af2522c2efa67911da)
答案 0 :(得分:1)
根据我的经验,您收到此错误是因为您没有在已还原的模型中提供该占位符的值。当你忘记喂它时,你通常会得到错误“你必须为占位符XX提供值”,但我注意到当占位符在其形状向量中有None
时(使用已恢复的模型),错误将是你得到的关于负面维度的错误。即使在占位符形状中有1 None
,我也会遇到此错误,并且正确输入其值可以解决问题。