Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

regarding the correct way to understand the result of tf.pad

Tags:

tensorflow

When reading the document for tf.pad, I feel quite confusing about the example given in the tutorial. For instance, padding is [[1,1,],[2,2]], how does it cause the resulting tensor has the shape as shown in the figure. Besides, what's the mechanism to generate those padded values, e.g., the ones marked in red circle. It is not very clear how to connect the explanation with the example.

enter image description here

like image 997
user785099 Avatar asked Aug 29 '16 15:08

user785099


3 Answers

Agree that the document doesn't explain very well the method. Any how,

For "paddings" in pad(t, paddings, "CONSTANT"); let's start by dimension 0 (i.e., row wise), according to example, paddings[0,0]=1, means adding one row above, and paddings[0,1]=1, means adding one row at the end.

Now, consider, dimension 1 (column wise) of paddings. paddings[1,0]=2, means to add two columns in the beginning, and paddings[1,1]=2, means to add two columns at the end.

I hope this is helpful.

like image 85
mehdi Avatar answered Oct 28 '22 11:10

mehdi


the first pair in the padding tuple is the horizontal padding and the second pair is the vertical padding

the reflected padding seems to quite literally reflect as if you placed a mirror on the last digit before the padding

input: 123

output for [2,2] horizontal: 32 123 21

same logic for horizontal

Symmetric seems to do the same thing, except it also repeats the boundary number first

21 123 32

the diagonals (corners) apply the padding scheme to the vertical padding output

reflected with [1,1],[2,2] and input:

123
456

output:

65 456 54
32 123  23
65  456 54
32  123 21
like image 30
Jules G.M. Avatar answered Oct 28 '22 12:10

Jules G.M.


Let's read the document line by line.

This operation pads a tensor(either placeholder or variable) according to the paddings you specify. paddings is an integer tensor with shape [n, 2](it must be two because there are only two directions in every dimension specified by n), where n is the rank of tensor(how many dimensions of the tensor).

For example, this paddings: [[1, 2], [1, 1], [2, 2], [3, 1]], indicates the rank of the tensor is 4(4 dimensions).

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.

Following the above example, for the 0 dimension we add 1 row ahead and 2 rows behind([1, 2]); the 1 dimension we add 1 column ahead and 1 column behind([1, 1]); the dimension 2, 2 ahead and 2 behind([2, 2]); the last dimension, 3 ahead and 1 behind([3, 1]).

If mode is "REFLECT" then both paddings[D, 0] and paddings[D, 1] must be no greater than tensor.dim_size(D) - 1. If mode is "SYMMETRIC" then both paddings[D, 0] and paddings[D, 1] must be no greater than tensor.dim_size(D).

This line is so obvious to understand with the examples there.

like image 33
Lerner Zhang Avatar answered Oct 28 '22 10:10

Lerner Zhang