tensorflow - 为什么 dataset.output_shapes 在批处理后返回维度(无)

标签 tensorflow

我在 TensorFlow(版本:r1.2)中将数据集 API 用于输入管道。我构建了我的数据集,并以 128 的批量大小对其进行了批处理。该数据集输入到 RNN 中。

不幸的是,dataset.output_shape返回第一个维度中的维度(无),因此 RNN 引发错误:

Traceback (most recent call last):
  File "untitled1.py", line 188, in <module>
    tf.app.run(main=main, argv=[sys.argv[0]] + unparsed)
  File "/home/harold/anaconda2/envs/tensorflow_py2.7/lib/python2.7/site-packages/tensorflow/python/platform/app.py", line 48, in run
    _sys.exit(main(_sys.argv[:1] + flags_passthrough))
  File "untitled1.py", line 121, in main
    run_training()
  File "untitled1.py", line 57, in run_training
    is_training=True)
  File "/home/harold/huawei/ConvLSTM/ConvLSTM.py", line 216, in inference
    initial_state=initial_state)
  File "/home/harold/anaconda2/envs/tensorflow_py2.7/lib/python2.7/site-packages/tensorflow/python/ops/rnn.py", line 566, in dynamic_rnn
    dtype=dtype)
  File "/home/harold/anaconda2/envs/tensorflow_py2.7/lib/python2.7/site-packages/tensorflow/python/ops/rnn.py", line 636, in _dynamic_rnn_loop
    "Input size (depth of inputs) must be accessible via shape inference,"
ValueError: Input size (depth of inputs) must be accessible via shape inference, but saw value None.

我认为这个错误是由输入的形状引起的,第一个维度应该是批量大小但不是没有。

这是代码:
origin_dataset = Dataset.BetweenS_Dataset(FLAGS.data_path)
train_dataset = origin_dataset.train_dataset
test_dataset = origin_dataset.test_dataset
shuffle_train_dataset = train_dataset.shuffle(buffer_size=10000)
shuffle_batch_train_dataset = shuffle_train_dataset.batch(128)
batch_test_dataset = test_dataset.batch(FLAGS.batch_size)

iterator = tf.contrib.data.Iterator.from_structure(
                           shuffle_batch_train_dataset.output_types,
                            shuffle_batch_train_dataset.output_shapes)
(images, labels) = iterator.get_next()

training_init_op = iterator.make_initializer(shuffle_batch_train_dataset)
test_init_op = iterator.make_initializer(batch_test_dataset)

print(shuffle_batch_train_dataset.output_shapes)

我打印 output_shapes它给出:
(TensorShape([Dimension(None), Dimension(36), Dimension(100)]), TensorShape([Dimension(None)]))

我想它应该是 128,因为我有批处理数据集:
(TensorShape([Dimension(128), Dimension(36), Dimension(100)]), TensorShape([Dimension(128)]))

最佳答案

此功能已添加到 drop_remainder 使用的参数如下:

batch_test_dataset = test_dataset.batch(FLAGS.batch_size, drop_remainder=True)

从文档:

drop_remainder: (Optional.) A tf.bool scalar tf.Tensor, representing whether the last batch should be dropped in the case its has fewer than batch_size elements; the default behavior is not to drop the smaller batch.

关于tensorflow - 为什么 dataset.output_shapes 在批处理后返回维度(无),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44299379/

相关文章:

python - 理解张量的秩和行为

python - 在keras中创建一个神经网络来乘以两个输入整数

swift - Swift Tensorflow 中的#tfop 是什么,它在哪里定义?

python - Tensorflow 1.6 中的 SSD_MOBILENET V1 到 TensorRT

python - 在 TF 2.0 上设置随机种子

python - 如何编写 Keras 自定义指标来过滤或屏蔽某些值?

python - 重置 tensorflow 优化器

python - TensorFlow 模型的 tf.data 管道中存在问题

python - 如何使用 learning_phase 在 TF 2.3 Eager 中获得中间输出?

python-3.x - 循环神经网络架构