我尝试使用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 ...... 提前谢谢!
答案 0 :(得分:2)
我成功地解决了这个问题。 我只是更新了tensorflow (我有1.6版本,你需要1.7才能有不同的批量大小)
要查看批量中您需要的张量流版本:
>python
>>>import tensorflow as tf
>>>print(tf.__version__)
然后,当您拥有1.7版本(或更多)时,您使用内部预测参数:
yield_single_examples =假
(默认情况下, True ,你会有同样的错误。)
在您的代码中,它将是:
predictions = autoencoder.predict(input_fn=input_fn_predict, yield_single_examples=False)