由于张量数据类型和形状Tensorflow,运行会话失败
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了由于张量数据类型和形状Tensorflow,运行会话失败相关的知识,希望对你有一定的参考价值。
我尝试使用以下方法加载模型和图形:
saver = tf.train.import_meta_graph(tf.train.latest_checkpoint(model_path)+".meta")
graph = tf.get_default_graph()
outputs = graph.get_tensor_by_name('output:0')
outputs = tf.cast(outputs,dtype=tf.float32)
X = graph.get_tensor_by_name('input:0')
sess = tf.Session()
sess.run(tf.global_variables_initializer())
sess.run(tf.local_variables_initializer())
if(tf.train.checkpoint_exists(tf.train.latest_checkpoint(model_path))):
saver.restore(sess, tf.train.latest_checkpoint(model_path))
print(tf.train.latest_checkpoint(model_path) + "Session Loaded for Testing")
有效!... 但是当我尝试运行会话时,我收到以下错误:
y_test_output= sess.run(outputs, feed_dict={X: x_test})
错误是:
Caused by op 'output', defined at:
File "testing_reality.py", line 21, in <module>
saver = tf.train.import_meta_graph(tf.train.latest_checkpoint(model_path)+".meta")
File "C:Python35libsite-packages ensorflowpython rainingsaver.py", line 1674, in import_meta_graph
meta_graph_or_file, clear_devices, import_scope, **kwargs)[0]
File "C:Python35libsite-packages ensorflowpython rainingsaver.py", line 1696, in _import_meta_graph_with_return_elements
**kwargs))
File "C:Python35libsite-packages ensorflowpythonframeworkmeta_graph.py", line 806, in import_scoped_meta_graph_with_return_elements
return_elements=return_elements)
File "C:Python35libsite-packages ensorflowpythonutildeprecation.py", line 488, in new_func
return func(*args, **kwargs)
File "C:Python35libsite-packages ensorflowpythonframeworkimporter.py", line 442, in import_graph_def
_ProcessNewOps(graph)
File "C:Python35libsite-packages ensorflowpythonframeworkimporter.py", line 234, in _ProcessNewOps
for new_op in graph._add_new_tf_operations(compute_devices=False): # pylint: disable=protected-access
File "C:Python35libsite-packages ensorflowpythonframeworkops.py", line 3440, in _add_new_tf_operations
for c_op in c_api_util.new_tf_operations(self)
File "C:Python35libsite-packages ensorflowpythonframeworkops.py", line 3440, in <listcomp>
for c_op in c_api_util.new_tf_operations(self)
File "C:Python35libsite-packages ensorflowpythonframeworkops.py", line 3299, in _create_op_from_tf_operation
ret = Operation(c_op, self)
File "C:Python35libsite-packages ensorflowpythonframeworkops.py", line 1770, in __init__
self._traceback = tf_stack.extract_stack()
InvalidArgumentError (see above for traceback): You must feed a value for placeholder tensor 'output' with dtype float and shape [?,1]
[[node output (defined at testing_reality.py:21) = Placeholder[dtype=DT_FLOAT, shape=[?,1], _device="/job:localhost/replica:0/task:0/device:CPU:0"]()]]
没有得到导致这个问题的问题。 请帮我找到缺失的链接。
我检查过:
>>> outputs
<tf.Tensor 'output:0' shape=(?, 1) dtype=float32>
仍然无法理解错误的原因。
我在Windows 10操作系统上使用最新版本的Tensorflow'1.12.0'。
这就是我创建的图表:
X = tf.placeholder(tf.float32, [None, n_steps, n_inputs],name="input")
y = tf.placeholder(tf.float32, [None, n_outputs],name="output")
layers = [tf.contrib.rnn.LSTMCell(num_units=n_neurons,activation=tf.nn.relu6, use_peepholes = True,name="layer"+str(layer))
for layer in range(n_layers)]
multi_layer_cell = tf.contrib.rnn.MultiRNNCell(layers)
rnn_outputs, states = tf.nn.dynamic_rnn(multi_layer_cell, X, dtype=tf.float32)
stacked_rnn_outputs = tf.reshape(rnn_outputs, [-1, n_neurons])
stacked_outputs = tf.layers.dense(stacked_rnn_outputs, n_outputs)
outputs = tf.reshape(stacked_outputs, [-1, n_steps, n_outputs])
outputs = outputs[:,n_steps-1,:] # keep only last output of sequence
loss = tf.reduce_mean(tf.square(outputs - y)) # loss function = mean squared error
optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate)
training_op = optimizer.minimize(loss)
答案
当您尝试评估图中依赖于占位符值的节点时,会发生这种情况。因此,您收到一条错误消息,指出您必须为占位符提供值。看看这个例子:
tf.reset_default_graph()
a = tf.placeholder(tf.float32)
b = tf.placeholder(tf.float32)
c = a + b
d = a
with tf.Session() as sess:
print(c.eval(feed_dict={a:1.0}))
# Error because in order to evaluate c we must have the value for b.
with tf.Session() as sess:
print(d.eval(feed_dict={a:1.0}))
# It works because d is not dependent on b.
现在,在您的情况下,您不应该执行outputs
占位符。您应该执行的操作是用于对模型进行预测的操作,同时在X
占位符中提供值(假设您使用该值来提供模型中的输入)。另一方面,我猜您在训练时使用output
占位符来提供标签,因此无需在该占位符中提供数据。
根据您的最新更新:
通过执行:outputs = graph.get_tensor_by_name('output:0')
您正在加载名为output的占位符。您不需要,您需要切片输出的操作。在创建图表的代码部分中,执行以下操作:
outputs = tf.identity(outputs[:,n_steps-1,:], name="prediction")
然后,在加载模型时,加载这两个张量:
X = graph.get_tensor_by_name('input:0')
prediction = graph.get_tensor_by_name('prediction:0')
最后,要获得您想要的输入的预测:
sess = tf.Session()
sess.run(tf.global_variables_initializer())
sess.run(prediction, feed_dict={X: x_test})
以上是关于由于张量数据类型和形状Tensorflow,运行会话失败的主要内容,如果未能解决你的问题,请参考以下文章