我正在尝试使用 TensorFlow Estimator API 可视化卷积自动编码器的输出
input_fn = tf.estimator.inputs.numpy_input_fn(
x={"x": train_data},
y=None,
batch_size=8,
num_epochs=None,
shuffle=True)
autoencoder = tf.estimator.Estimator(model_fn=autoencoder_model_fn, model_dir=model_dir)
tensors_to_log = {"loss": "loss"}
logging_hook = tf.train.LoggingTensorHook(tensors=tensors_to_log, every_n_iter=1000)
autoencoder.train(
input_fn=input_fn,
steps=50000,
hooks=[logging_hook])
input_fn_predict = tf.estimator.inputs.numpy_input_fn(
x={"x": train_data},
y=None,
batch_size=1,
num_epochs=None,
shuffle=False)
predictions = autoencoder.predict(input_fn=input_fn_predict)
predictions = [p['decoded_image'] for p in predictions]
print predictions[0].shape
我收到以下错误:
Traceback (most recent call last):
File "AutoEncoder.py", line 164, in <module>
main()
File "AutoEncoder.py", line 157, in main
predictions = [p['decoded_image'] for p in predictions]
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/estimator/estimator.py", line 425, in predict
for i in range(self._extract_batch_length(preds_evaluated)):
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/estimator/estimator.py", line 592, in _extract_batch_length
'different batch length then others.' % key)
ValueError: Batch length of predictions should be same. features has different batch length then others.
谁能看出我做错了什么?据我了解,我在预测期间的批量大小是一个常数,等于 1 ... 提前致谢!
请您参考如下方法:
我成功地解决了这个问题。 我刚刚更新了 tensorflow(我有 1.6 版本,你需要 1.7 才能有不同的批量大小)
要在批处理中查看您只需要的 tensorflow 版本:
>python
>>>import tensorflow as tf
>>>print(tf.__version__)
然后,当您拥有 1.7 版本(或更高版本)时,您可以使用 inside predict the argument :
yield_single_examples=False
(默认为True,你也会犯同样的错误)。
在您的代码中,它将是:
predictions = autoencoder.predict(input_fn=input_fn_predict, yield_single_examples=False)
