Jiangning Liu Jiangning Liu - 7 months ago 26
Python Question

How does tensorflow.pad work?

There is the example of tensorflow.pad():


# 't' = is [[1, 2, 3], [4, 5, 6]].
# 'paddings' is [[1, 1,], [2, 2]].
# rank of 't' is 2.
' tf.pad(t, paddings, "CONSTANT")'
==> [[0, 0, 0, 0, 0, 0, 0],
[0, 0, 1, 2, 3, 0, 0],
[0, 0, 4, 5, 6, 0, 0],
[0, 0, 0, 0, 0, 0, 0]]



my question is how to pad zeros in every dimention of input? And the shape of t is [2,3], why output after pad() is [4,x],how the '4' comes?
Thanks for helping me!!!

Answer Source

The documentation is pretty clear about this. 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.

why out put is [4, x]?

4 is the size of dimension 0, dimension 0 has padding [1, 1], which according to the docs add one before the zero dimension of t and one after, the size of zero dimension of t is 2, 2 + 1 + 1, you have 4 in the result. i.e. it padded one zero row at the beginning and ending of t respectively. Similarly for dimension 1, since padding[1] is [2,2], two zero columns are added to t at the beginning and ending respectively.