user3497636
user3497636

Reputation: 13

How to create padded batch in tensorflow 2.0?

I am trying to create a padded batch from tensorflow dataset, it is throwing me an error.

Sample data:

A = [
 ([[101,9385,13302,102], [1,1,1,1], [0,0,0,0]]),
 ([[101,9385,13302], [1,1,1], [0,0,0]]),

 ([[101,9385,13302,102], [1,1,1,1], [0,0,0,0]]),
 ([[101,9385,13302], [1,1,1], [0,0,0]])     
 ]

Expected Output:

Output = [
 ([[101,9385,13302,102], [1,1,1,1], [0,0,0,0]]),
 ([[101,9385,13302,-100], [1,1,1,-100], [0,0,0,-100]]),

 ([[101,9385,13302,102], [1,1,1,1], [0,0,0,0]]),
 ([[101,9385,13302,-100], [1,1,1,-100], [0,0,0,-100]])     
 ]

Below is the code:

chk = tf.data.Dataset.from_generator(lambda: A,
                                         output_types = (tf.int32))
BATCH_SIZE = 2
chk_batched = chk.padded_batch(BATCH_SIZE ,
                                   padded_shapes=(4),
                                   padding_values=(0))

for elem in chk_batched.as_numpy_iterator():
   print(elem)

The error is given below:

InvalidArgumentError: All elements in a batch must have the same rank as the padded shape for component0: expected rank 1 but got element with rank 2

Upvotes: 0

Views: 1162

Answers (1)

Varchita Lalwani
Varchita Lalwani

Reputation: 77

Its 2 dimensional therefore padded_shapes = (4,3) and no need to write padding_values=(0). By default the value for padding is 0

Upvotes: 1

Related Questions