gpt4 book ai didi

python - 如何让 estimator.predict 预测一个样本

转载 作者:太空宇宙 更新时间:2023-11-04 00:17:52 25 4
gpt4 key购买 nike

我正在尝试让 mnist cnn 工作,以便一次对一张图像进行预测。我已经学习了 tensorflow 教程代码并尝试将 estimator.predict 与模型一起使用,但目前出现错误:

InvalidArgumentError (see above for traceback): Input to reshape is a tensor with 1 values, but the requested shape requires a multiple of 784
[[Node: Reshape = Reshape[T=DT_FLOAT, Tshape=DT_INT32, _device="/job:localhost/replica:0/task:0/device:GPU:0"](fifo_queue_DequeueUpTo/_53, Reshape/shape)]]

如果我打印提供给预测输入函数的 predict_data 列表,它包含 784 个元素。

模型训练正常,评估正常。该模型已经过训练,所以我在这里跳过了训练代码,但这就是我所拥有的:

def main(unused_argv):
# Load training and eval data
mnist = tf.contrib.learn.datasets.load_dataset("mnist")
train_data = mnist.train.images # Returns np.array
train_labels = np.asarray(mnist.train.labels, dtype=np.int32)
eval_data = mnist.test.images # Returns np.array
eval_labels = np.asarray(mnist.test.labels, dtype=np.int32)

# Create the Estimator
mnist_classifier = tf.estimator.Estimator(
model_fn=cnn_model_fn, model_dir="/tmp/mnist_convnet_model")

# Set up logging for predictions
# Log the values in the "Softmax" tensor with label "probabilities"
tensors_to_log = {"probabilities": "softmax_tensor"}
logging_hook = tf.train.LoggingTensorHook(
tensors=tensors_to_log, every_n_iter=50)

# # Train the model
# train_input_fn = tf.estimator.inputs.numpy_input_fn(
# x={"x": train_data},
# y=train_labels,
# batch_size=100,
# num_epochs=None,
# shuffle=True)
# mnist_classifier.train(
# input_fn=train_input_fn,
# steps=20000,
# hooks=[logging_hook])

# Evaluate the model and print results
# eval_input_fn = tf.estimator.inputs.numpy_input_fn(
# x={"x": eval_data},
# y=eval_labels,
# num_epochs=1,
# shuffle=False)
# eval_results = mnist_classifier.evaluate(input_fn=eval_input_fn)
# print(eval_results)

predict_data = eval_data[1]
predict_input_fn = tf.estimator.inputs.numpy_input_fn(
x={"x": predict_data},
y=None,
batch_size=1,
num_epochs=1,
shuffle=False,
num_threads=1)

predict_results = mnist_classifier.predict(predict_input_fn)

print(predict_data)
for idx, prediction in enumerate(predict_results):
print(idx)
# print(prediction)

如能提供帮助,我们将不胜感激。

更新:我尝试按照下面的建议 reshape ,但得到了同样的错误。完整的跟踪是:

Traceback (most recent call last):
File "C:\Users\artma\Miniconda3\envs\vpilot\lib\site-packages\tensorflow\python\client\session.py", line 1323, in _do_call
return fn(*args)
File "C:\Users\artma\Miniconda3\envs\vpilot\lib\site-packages\tensorflow\python\client\session.py", line 1302, in _run_fn
status, run_metadata)
File "C:\Users\artma\Miniconda3\envs\vpilot\lib\site-packages\tensorflow\python\framework\errors_impl.py", line 473, in __exit__
c_api.TF_GetCode(self.status.status))
tensorflow.python.framework.errors_impl.InvalidArgumentError: Input to reshape is a tensor with 1 values, but the requested shape requires a multiple of 784
[[Node: Reshape = Reshape[T=DT_FLOAT, Tshape=DT_INT32, _device="/job:localhost/replica:0/task:0/device:GPU:0"](fifo_queue_DequeueUpTo/_53, Reshape/shape)]]

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "D:\Workspace\eclipse\mnist_cnn\cnn_mnist.py", line 180, in <module>
tf.app.run()
File "C:\Users\artma\Miniconda3\envs\vpilot\lib\site-packages\tensorflow\python\platform\app.py", line 48, in run
_sys.exit(main(_sys.argv[:1] + flags_passthrough))
File "D:\Workspace\eclipse\mnist_cnn\cnn_mnist.py", line 170, in main
for idx, prediction in enumerate(predict_results):
File "C:\Users\artma\Miniconda3\envs\vpilot\lib\site-packages\tensorflow\python\estimator\estimator.py", line 420, in predict
preds_evaluated = mon_sess.run(predictions)
File "C:\Users\artma\Miniconda3\envs\vpilot\lib\site-packages\tensorflow\python\training\monitored_session.py", line 521, in run
run_metadata=run_metadata)
File "C:\Users\artma\Miniconda3\envs\vpilot\lib\site-packages\tensorflow\python\training\monitored_session.py", line 892, in run
run_metadata=run_metadata)
File "C:\Users\artma\Miniconda3\envs\vpilot\lib\site-packages\tensorflow\python\training\monitored_session.py", line 967, in run
raise six.reraise(*original_exc_info)
File "C:\Users\artma\Miniconda3\envs\vpilot\lib\site-packages\six.py", line 693, in reraise
raise value
File "C:\Users\artma\Miniconda3\envs\vpilot\lib\site-packages\tensorflow\python\training\monitored_session.py", line 952, in run
return self._sess.run(*args, **kwargs)
File "C:\Users\artma\Miniconda3\envs\vpilot\lib\site-packages\tensorflow\python\training\monitored_session.py", line 1024, in run
run_metadata=run_metadata)
File "C:\Users\artma\Miniconda3\envs\vpilot\lib\site-packages\tensorflow\python\training\monitored_session.py", line 827, in run
return self._sess.run(*args, **kwargs)
File "C:\Users\artma\Miniconda3\envs\vpilot\lib\site-packages\tensorflow\python\client\session.py", line 889, in run
run_metadata_ptr)
File "C:\Users\artma\Miniconda3\envs\vpilot\lib\site-packages\tensorflow\python\client\session.py", line 1120, in _run
feed_dict_tensor, options, run_metadata)
File "C:\Users\artma\Miniconda3\envs\vpilot\lib\site-packages\tensorflow\python\client\session.py", line 1317, in _do_run
options, run_metadata)
File "C:\Users\artma\Miniconda3\envs\vpilot\lib\site-packages\tensorflow\python\client\session.py", line 1336, in _do_call
raise type(e)(node_def, op, message)
tensorflow.python.framework.errors_impl.InvalidArgumentError: Input to reshape is a tensor with 1 values, but the requested shape requires a multiple of 784
[[Node: Reshape = Reshape[T=DT_FLOAT, Tshape=DT_INT32, _device="/job:localhost/replica:0/task:0/device:GPU:0"](fifo_queue_DequeueUpTo/_53, Reshape/shape)]]

Caused by op 'Reshape', defined at:
File "D:\Workspace\eclipse\mnist_cnn\cnn_mnist.py", line 180, in <module>
tf.app.run()
File "C:\Users\artma\Miniconda3\envs\vpilot\lib\site-packages\tensorflow\python\platform\app.py", line 48, in run
_sys.exit(main(_sys.argv[:1] + flags_passthrough))
File "D:\Workspace\eclipse\mnist_cnn\cnn_mnist.py", line 170, in main
for idx, prediction in enumerate(predict_results):
File "C:\Users\artma\Miniconda3\envs\vpilot\lib\site-packages\tensorflow\python\estimator\estimator.py", line 411, in predict
features, None, model_fn_lib.ModeKeys.PREDICT, self.config)
File "C:\Users\artma\Miniconda3\envs\vpilot\lib\site-packages\tensorflow\python\estimator\estimator.py", line 694, in _call_model_fn
model_fn_results = self._model_fn(features=features, **kwargs)
File "D:\Workspace\eclipse\mnist_cnn\cnn_mnist.py", line 31, in cnn_model_fn
input_layer = tf.reshape(features["x"], [-1, 28, 28, 1])
File "C:\Users\artma\Miniconda3\envs\vpilot\lib\site-packages\tensorflow\python\ops\gen_array_ops.py", line 3937, in reshape
"Reshape", tensor=tensor, shape=shape, name=name)
File "C:\Users\artma\Miniconda3\envs\vpilot\lib\site-packages\tensorflow\python\framework\op_def_library.py", line 787, in _apply_op_helper
op_def=op_def)
File "C:\Users\artma\Miniconda3\envs\vpilot\lib\site-packages\tensorflow\python\framework\ops.py", line 2956, in create_op
op_def=op_def)
File "C:\Users\artma\Miniconda3\envs\vpilot\lib\site-packages\tensorflow\python\framework\ops.py", line 1470, in __init__
self._traceback = self._graph._extract_stack() # pylint: disable=protected-access

InvalidArgumentError (see above for traceback): Input to reshape is a tensor with 1 values, but the requested shape requires a multiple of 784
[[Node: Reshape = Reshape[T=DT_FLOAT, Tshape=DT_INT32, _device="/job:localhost/replica:0/task:0/device:GPU:0"](fifo_queue_DequeueUpTo/_53, Reshape/shape)]]

更新:好像破解了。感谢 xdurch0 让我走上正轨。

最佳答案

predict_data 只是一个 784 元素向量。这将被视为包含 784 个元素(即不同的输入)的数据集,每个元素都是一个标量。您需要将 predict_data reshape 为 (1, 784),让 TF 知道这是一个只有一个元素的数据集,它是一个 784 元素向量。例如。 predict_data[np.newaxis, :]predict_data.reshape((1, 784))

关于python - 如何让 estimator.predict 预测一个样本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50251878/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com