tumbleweed
tumbleweed

Reputation: 4660

What is the correct way of pad and reshape a tensor in tensorflow?

Given the following tensor:

<tf.Tensor: shape=(59,), dtype=int64, numpy=
array([1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1,
       1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 2, 2, 1, 1, 1, 1, 1, 2, 2, 1, 1,
       1, 0, 1, 2, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1])>

What is the correct way of reshape it and pad it into (32, 59)? From keras docs I tried to:

keras.preprocessing.sequence.pad_sequences(t, 32)

Nevertheles I am getting:

ValueError: `sequences` must be a list of iterables. Found non-iterable: tf.Tensor(1, shape=(), dtype=int64)

Also, I tried:

tf.reshape(tf.data.Dataset.from_tensors(a[0]).padded_batch(32), [32,59])

However, I am getting:

ValueError: Attempt to convert a value (<PaddedBatchDataset shapes: (None, 59), types: tf.int64>) with an unsupported type (<class 'tensorflow.python.data.ops.dataset_ops.PaddedBatchDataset'>) to a Tensor.

What is the correct way of doing a 32 padding and reshape it into 32,59?

Upvotes: 0

Views: 604

Answers (1)

Dominik Ficek
Dominik Ficek

Reputation: 566

If you're useing tf.keras tf.pad should be preferred choice for tensor padding (see docs).

From what it looks like you have tensor of shape (59, ) and want to pad the tensor to shape (32, 59). This would be done as

# 15 rows before, 16 rows after, 0 cols before and after
paddings = tf.constant([[15, 16], [0, 0]])
# first reshape tensor to (1, 59), then pad it
padded = tf.pad(tensor[tf.newaxis, ...], paddings)

default padding is with zeros, see the docs for other options.

Upvotes: 1

Related Questions