【问题标题】:TF2 padded_batch on supervised dataset监督数据集上的 TF2 padded_batch
【发布时间】:2020-05-29 09:06:05
【问题描述】:

问题设置

我关注this tutorial。本教程首先加载一个监督数据集(使用tfds.load 和as_supervised=True):

(train_data, test_data), info = tfds.load(
    'imdb_reviews/subwords8k', 
    split = (tfds.Split.TRAIN, tfds.Split.TEST), 
    with_info=True, as_supervised=True)

然后教程建议像这样洗牌和填充数据集:

电影评论可以有不同的长度。我们将使用padded_batch 方法来标准化评论的长度。

train_batches = train_data.shuffle(1000).padded_batch(10)
test_batches = test_data.shuffle(1000).padded_batch(10)

...但不幸的是,padded_batch 方法需要一个额外的参数,教程似乎已经忘记了:

Traceback (most recent call last):
  File "imdb_reviews.py", line 14, in <module>
    train_batches = train_data.shuffle(1000).padded_batch(10)
TypeError: padded_batch() missing 1 required positional argument: 'padded_shapes'

重要假设

虽然错误堆栈说 padded_shapes 是缺少的参数,但我认为从教程中推断缺少的参数实际上是 batch_size 是公平的(应该在 padded_shapes 之前)。

我尝试过的

我认为这可能很容易解决:

batch_sz = 100 # arbitrary number
train_batches = train_data.shuffle(1000).padded_batch(batch_sz, ([10],[None]))
test_batches = test_data.shuffle(1000).padded_batch(batch_sz, ([10],[None]))

...但我的解决方案显然是错误的:

Traceback (most recent call last):
  File "imdb_reviews.py", line 15, in <module>
    train_batches = train_data.shuffle(1000).padded_batch(batch_sz, ([10],[None]))
  File "/home/ggiuffre/.local/lib/python3.7/site-packages/tensorflow_core/python/data/ops/dataset_ops.py", line 2298, in padded_batch
    batch_size, padded_shapes, padding_values, drop_remainder))
  File "/home/ggiuffre/.local/lib/python3.7/site-packages/tensorflow_core/python/data/ops/dataset_ops.py", line 1481, in padded_batch
    drop_remainder)
  File "/home/ggiuffre/.local/lib/python3.7/site-packages/tensorflow_core/python/data/ops/dataset_ops.py", line 3813, in __init__
    _padded_shape_to_tensor(padded_shape, input_component_shape))
  File "/home/ggiuffre/.local/lib/python3.7/site-packages/tensorflow_core/python/data/ops/dataset_ops.py", line 3741, in _padded_shape_to_tensor
    % (padded_shape_as_shape, input_component_shape))
ValueError: The padded shape (None,) is not compatible with the corresponding input component shape ().

用() 替换None 得到ValueError: Padded shape [()] must be a 1-D tensor of tf.int64 values, but its shape was (1, 0).

用1 替换None 得到ValueError: The padded shape (1,) is not compatible with the corresponding input component shape ().

问题

我应该为padded_shapes 参数赋予什么值?或者,更一般地说,我在这里做错了什么?

非常感谢您的帮助。

【问题讨论】:

    标签: python tensorflow tensorflow-datasets tensorflow2.x


    【解决方案1】:

    看看这个博客。 https://medium.com/@a.ydobon/tensorflow-2-0-word-embeddings-part3-964b2b9caf94

    推荐

    padded_shapes = ([None],())
    train_batches = train_data.shuffle(1000).padded_batch(10,padded_shapes=padded_shapes)
    test_batches = test_data.shuffle(1000).padded_batch(10,padded_shapes=padded_shapes)
    

    这对我有用。

    【讨论】:

      【解决方案2】:

      这也很好用

      batch_sz = 100 # arbitrary number
      train_batches = train_data.shuffle(1000)
      test_batches = test_data.shuffle(1000)
      
      train_batches = train_batches.padded_batch(batch_sz, train_batches.output_shapes)
      test_batches = test_batches.padded_batch(batch_sz, test_batches.output_shapes)
      

      你可以定义你的 LSTM 层并适应 train_batches ,test_batches

      【讨论】:

      猜你喜欢
      • 2018-07-11
      • 2018-08-10
      • 1970-01-01
      • 1970-01-01
      • 2015-06-18
      • 2017-09-04
      • 2019-09-19
      • 2012-11-08
      • 2017-11-10
      相关资源
      最近更新 更多