SantoshGupta7
SantoshGupta7

Reputation: 6197

How to pad 1 dimensinal vector in tensorflow? Getting InvalidArgumentError: paddings must be a matrix with 2 columns with tf.pad

I am trying to use tf.pad. Here is my attempt to pad the tensor to length 20, with values 10.

tf.pad(tf.constant([1, 2, 3, 45]), paddings=20, constant_values=10)

I get this error message

InvalidArgumentError: paddings must be a matrix with 2 columns: [2,1] [Op:PadV2]

I am looking at the documentation

https://www.tensorflow.org/api_docs/python/tf/pad

paddings is an integer tensor with shape [n, 2], where n is the rank of tensor. For each dimension D of input, paddings[D, 0] indicates how many values to add before the contents of tensor in that dimension, and paddings[D, 1] indicates how many values to add after the contents of tensor in that dimension

But I am unable to figure out how to shape the pad value

Upvotes: 2

Views: 1063

Answers (1)

Tou You
Tou You

Reputation: 1184

You have to specify the padding at the beginning and the padding at the end of your vector by matrix of shape (1,2) :

tf.pad(tf.constant([1, 2, 3, 45]), [[ 0 , 20]], constant_values=10)

if you have three-dimensional tensor (rank = 3 e.g : (225,225,3) ) the padding matrix has to be of shape (3, 2 ) where "3" is the rank, and "2" to specify the padding at the beginning and end of each dimension.

For example, a padding matrix = [ [0,2], [5,5], [2,0] ], means that we want to pad the first dimension by 0 at the beginning (=no padding) and 2 at the end .padding the second dimension by 5 at beginning and 5 at the end.

Upvotes: 4

Related Questions