tf.pad(tensor, paddings, mode="CONSTANT", name=None, constant_values=0)
该函数有四个参数
Args:
tensor: A Tensor
.
paddings: A Tensor
of type int32
.
mode: One of “CONSTANT”, “REFLECT”, or “SYMMETRIC” (case-insensitive)
name: A name for the operation (optional).
constant_values: In “CONSTANT” mode, the scalar pad value to use. Must be same type as tensor
.当mode为CONSTANT时,填充的值默认为0,如果想换成别的值,更改constant_values即可
Returns:
A Tensor
. Has the same type as tensor
.
官方文档的注释
“”"Pads a tensor.
This operation pads a tensor
according to the paddings
you specify.
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. 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)
.
The padded size of each dimension D of the output is:
paddings[D, 0] + tensor.dim_size(D) + paddings[D, 1]
**
下面示例理解文档
**注意:**paddings是一个类型为int32,shape为(n,2)的张量,这里的n是tensor(函数的第一个参数)的秩,
import tensorflow as tf
# a与b都是5维张量,a的shape为(1, 1, 1, 1, 2),b的shape为(1,1,1,1,1)
# 对b进行填充使得b的第四维(第几维的几从0开始算起)与a的第四维相等
sess = tf.Session()
a = tf.constant([1,2])
a = tf.reshape(a,[1,1,1,1,2]) # a的shape为(1, 1, 1, 1, 2)
#print(sess.run(a)) # a 为 [[[[[1 2]]]]]
b = tf.constant([1])
b = tf.reshape(b, [1,1,1,1,1]) # b的shape为(1,1,1,1,1)
#print(sess.run(b)) # [[[[[1]]]]]
c = tf.constant([[0, 0], [0, 0], [0, 0], [0, 0],[0, 1])
#print(c.get_shape())# c是paddings,shape是(5, 2),因为b的秩是5
#print(sess.run(c[4,1]))# c[4,1]=1,是指在b的第4维张量内容的后面补一个0
#对应官方文档注释`paddings[D, 1]` indicates how many values to add after the contents of `tensor` in that dimension.这里的D是4,在[[[[[1]]]]]后面补一个0,得到[[[[[1 0]]]]]
print(sess.run(tf.pad(tensor=b, paddings=c))) # [[[[[1 0]]]]]
如果paddings[D, 0]=1,即在tensor的第D维内容前面补一个0,对应“paddings[D, 0]
indicates how many values to add before the contents of tensor
in that dimensio”如下
c = tf.constant([[0, 0], [0, 0], [0, 0], [0, 0],[1, 0])# c[4, 0]=1
print(sess.run(tf.pad(tensor=b, paddings=c)))# [[[[[0, 1]]]]]
官方例子
t = tf.constant([[1, 2, 3], [4, 5, 6]])
paddings = tf.constant([[1, 1,], [2, 2]])
# 'constant_values' is 0.
# 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]]
tf.pad(t, paddings, "REFLECT") # [[6, 5, 4, 5, 6, 5, 4],
# [3, 2, 1, 2, 3, 2, 1],
# [6, 5, 4, 5, 6, 5, 4],
# [3, 2, 1, 2, 3, 2, 1]]
tf.pad(t, paddings, "SYMMETRIC") # [[2, 1, 1, 2, 3, 3, 2],
# [2, 1, 1, 2, 3, 3, 2],
# [5, 4, 4, 5, 6, 6, 5],
# [5, 4, 4, 5, 6, 6, 5]]
对于CONSTANT:
t是一个2维张量,2行3列,paddings的[1,1]是指paddings[0, 0]=1,也就是在t的第0维度(也就是行)前面(也就是上面)补充1行0,paddings[0, 1]=1,也就是在t的第0维度后面(下面)补充1行0;paddings的[2,2]是指paddings[1, 0]=2,也就是在t的第1维度(列)前面(左边)补充2列0,paddings[1, 1]=2,也就是在t的第0维度后面(右边)补充2列0;
对于REFLECT(映像):
原本的tensor是
第一步:以 1 2 3为对称轴,向上添加一行,因此上面添加的是4 5 6;以 4 5 6为对称轴下面添加的是 1 2 3
第二步:
tensor内容的前面需要镜像添加两列,也就是以[4, 1, 4, 1]^T(表示转置。也就是
)为中心轴,在左边映射两列,也就得到
然后再以[6,3,6,3]^T为对称轴向右边映射两列得到
此时完成填充,得到最终的tensor
对于symmetric(对称):
第一步,以红线为对称轴向上添加一列得到如下矩阵
然后以红线为对称轴向下添加一列得到如下矩阵
第二步 :以红线为对称轴向左添加两列得到如下矩阵
然后以红线为对称轴向右添加两列得到如下矩阵
此时完成填充,最后得到