Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!!!

like image 212
Jiangning Liu Avatar asked May 12 '17 03:05

Jiangning Liu


2 Answers

'paddings' is [[1, 1,], [2, 2]]. Try to map this vale us as [[top,bottom],[left,right]]. i.e.

top = 1,      //Extra padding introduce on top
bottom = 1,   //Extra padding introduce on bottom
left = 2,     //Extra padding introduce on left 
right = 2.    //Extra padding introduce on right

Try another example where 'padding' is [[2, 1], [2, 3]]. Output will be:

[[0 0 0 0 0 0 0 0]
 [0 0 0 0 0 0 0 0]
 [0 0 1 2 3 0 0 0]
 [0 0 4 5 6 0 0 0]
 [0 0 0 0 0 0 0 0]]

Here top=2, bottom=1, left=2, right=3.

like image 132
vipin bansal Avatar answered Sep 28 '22 13:09

vipin bansal


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.

like image 45
Psidom Avatar answered Sep 28 '22 13:09

Psidom