由于张量数据类型和形状Tensorflow,运行会话失败

时间:2019-01-31 14:06:23

标签: python python-3.x tensorflow machine-learning

我尝试使用以下方法加载模型和图形:

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:\Python35\lib\site-packages\tensorflow\python\training\saver.py", line 1674, in import_meta_graph
    meta_graph_or_file, clear_devices, import_scope, **kwargs)[0]
  File "C:\Python35\lib\site-packages\tensorflow\python\training\saver.py", line 1696, in _import_meta_graph_with_return_elements
    **kwargs))
  File "C:\Python35\lib\site-packages\tensorflow\python\framework\meta_graph.py", line 806, in import_scoped_meta_graph_with_return_elements
    return_elements=return_elements)
  File "C:\Python35\lib\site-packages\tensorflow\python\util\deprecation.py", line 488, in new_func
    return func(*args, **kwargs)
  File "C:\Python35\lib\site-packages\tensorflow\python\framework\importer.py", line 442, in import_graph_def
    _ProcessNewOps(graph)
  File "C:\Python35\lib\site-packages\tensorflow\python\framework\importer.py", line 234, in _ProcessNewOps
    for new_op in graph._add_new_tf_operations(compute_devices=False):  # pylint: disable=protected-access
  File "C:\Python35\lib\site-packages\tensorflow\python\framework\ops.py", line 3440, in _add_new_tf_operations
    for c_op in c_api_util.new_tf_operations(self)
  File "C:\Python35\lib\site-packages\tensorflow\python\framework\ops.py", line 3440, in <listcomp>
    for c_op in c_api_util.new_tf_operations(self)
  File "C:\Python35\lib\site-packages\tensorflow\python\framework\ops.py", line 3299, in _create_op_from_tf_operation
    ret = Operation(c_op, self)
  File "C:\Python35\lib\site-packages\tensorflow\python\framework\ops.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 OS上使用最新版本的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)

1 个答案:

答案 0 :(得分:1)

当您尝试评估图形中依赖于占位符值的节点时,会发生这种情况。因此,您会收到一条错误消息,指出您必须为占位符提供一个值。看这个例子:

1/   delete "node-modules"
2/   "npm install node-sass@4.10.0 --save"
3/   "npm install"
4/   "npm rebuild node-sass"

现在,就您而言,您不应该执行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正在加载名为output的占位符。您不需要它,您需要对输出进行切片的操作。在图形创建所在的代码部分中,执行以下操作:

outputs = graph.get_tensor_by_name('output:0')

然后,在加载模型时,加载以下两个张量:

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')