Unexpected shape returned from function mapped to tensorflow dataset

Viewed 208

I tried to pad a dataset containing tensors of varying length in the first dimension. The lengths are any number up to 13 and I would like to pad zeros to the front at the first dimension. It seems the function is working properly when I apply it on a tensor but the dataset didn't return a shape of (13,128) as I expected. I got a shape of (None, None).

The code goes like this:

print(train_dataset_filtered.element_spec, '\n')

def pad_seq(eng, ger):
    n = 13 - tf.shape(eng)[0]
    paddings = tf.concat(([[n,0]], [[0,0]]), axis=0)
    return tf.pad(eng, paddings), ger

print(pad_seq(tf.ones((4,128)), tf.ones((14,))), '\n')

print(train_dataset_filtered.map(pad_seq).element_spec)

The output is as follows:

(TensorSpec(shape=(None, 128), dtype=tf.float32, name=None), TensorSpec(shape=(14,), dtype=tf.int32, name=None)) 

(<tf.Tensor: id=402, shape=(13, 128), dtype=float32, numpy=
array([[0., 0., 0., ..., 0., 0., 0.],
       [0., 0., 0., ..., 0., 0., 0.],
       [0., 0., 0., ..., 0., 0., 0.],
       ...,
       [1., 1., 1., ..., 1., 1., 1.],
       [1., 1., 1., ..., 1., 1., 1.],
       [1., 1., 1., ..., 1., 1., 1.]], dtype=float32)>, <tf.Tensor: id=388, shape=(14,), dtype=float32, numpy=
array([1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
      dtype=float32)>) 

(TensorSpec(shape=(None, None), dtype=tf.float32, name=None), TensorSpec(shape=(14,), dtype=tf.int32, name=None))
1 Answers

The shape of n = 13 - tf.shape(eng)[0] is causing this behavior here when you use concat().

In your example it is tf.Tensor(9, shape=(), dtype=int32). So the best way is to use stack(). Change your code with:

def pad_seq(eng, ger):
    n = 13 - tf.shape(eng)[0]
    paddings = tf.stack(([[n,0], [0,0]]))
    return (tf.pad(eng, paddings), ger)

Also I noticed you did not accept any answers in SO. Please take a look at What should I do when someone answers my question?

Related