简体   繁体   中英

TF2 padded_batch on supervised dataset

Problem setting

I am following this tutorial . The tutorial first loads a supervised dataset (using tfds.load with 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)

Then the tutorial suggests to shuffle and pad the dataset like this:

Movie reviews can be different lengths. We will use the padded_batch method to standardize the lengths of the reviews.

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

... but unfortunately the padded_batch method needs an extra argument that the tutorial seems to have forgotten:

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'

Important assumption

Although the errors stack says that padded_shapes is the missing argument, from the tutorial I think it's fair to deduce that the missing argument is in fact batch_size (that should precede padded_shapes ).

What I have tried

I thought this might be easily fixed like this:

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]))

... but my solution is apparently wrong:

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 ().

Substituting None with () gives me ValueError: Padded shape [()] must be a 1-D tensor of tf.int64 values, but its shape was (1, 0).

Substituting None with 1 gives me ValueError: The padded shape (1,) is not compatible with the corresponding input component shape ().

Problem

What value should I give to the padded_shapes argument? Or, more generally, what am I doing wrong here?

Thank you very much for your help.

Take a look at this blog. https://medium.com/@a.ydobon/tensorflow-2-0-word-embeddings-part3-964b2b9caf94

It recommends

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)

This worked for me.

This also works fine

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)

Than you can define your LSTM layers and fit train_batches ,test_batches

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM