1)示例
t = [[2, 3, 4], [5, 6, 7]]
paddings = [[1, 2], [2, 3]]
with tf.Session() as sess:
print(sess.run(tf.pad(t, paddings, "CONSTANT", constant_values=1)))
[[1 1 1 1 1 1 1 1]
[1 1 2 3 4 1 1 1]
[1 1 5 6 7 1 1 1]
[1 1 1 1 1 1 1 1]
[1 1 1 1 1 1 1 1]]
官方解释
函数原型:
tf.pad( tensor, paddings, mode='CONSTANT', name=None, constant_values=0)
input : 待填充的张量
padding : 指定待填充的区域
name : 代表此操作的名字
Pads a tensor with zeros.
This operation pads ainputwith zeros according to thepaddingsyouspecify.paddingsis an integer tensor with shape[Dn, 2], where n is therank ofinput. For each dimension D ofinput,paddings[D, 0]indicateshow many zeros to add before the contents ofinputin that dimension, andpaddings[D, 1]indicates how many zeros to add after the contents ofinputin that dimension.
The padded size of each dimension D of the output is:
paddings(D, 0) + input.dim_size(D) + paddings(D, 1)
其实从参数可以看出,填充1也是可以的,不必一定填充0.
mode 可以取三个值,分别是"CONSTANT" ,"REFLECT","SYMMETRIC"
mode="CONSTANT" 是填充0
mode="REFLECT"是映射填充,上下(1维)填充顺序和paddings是相反的,左右(零维)顺序补齐
mode="SYMMETRIC"是对称填充,上下(1维)填充顺序是和paddings相同的,左右(零维)对称补齐
2)特定的二维矩阵与三维张量
1. 对于二维矩阵,就是在 上 ,下,左,右填充
import numpyas np
import tensorflowas tf
# 创建一个二维变量,默认执行CONSTANT填充
vct = tf.ones([3, 4])
# 指定填充方式
#pad1 = np.array([['上','下'], ['左','右']])
pad_up_1line = [[1, 0], [0, 0]]
pad_down_2line = [[0, 2], [0, 0]]
pad_left_3line = [[0, 0], [3, 0]]
pad_right_4line = [[0, 0], [0, 4]]
# tf.pad进行填充
vct_上边补1行 = tf.pad(vct, pad_up_1line)
vct_下边补2行 = tf.pad(vct, pad_down_2line)
vct_左边补3行 = tf.pad(vct, pad_left_3line)
vct_右边补4行 = tf.pad(vct, pad_right_4line)
#vct_pad1 = tf.pad(vct, pad1, name='pad_1')
# 创建会话
with tf.Session()as sess:
sess.run(tf.global_variables_initializer())
print('原始矩阵')
print(sess.run(vct))
print('上边补1行')
print(sess.run(vct_上边补1行))
print('下边补2行')
print(sess.run(vct_下边补2行))
print('左边补3行')
print(sess.run(vct_左边补3行))
print('vct_右边补4行')
print(sess.run(vct_右边补4行))
输出:
原始矩阵
[[1. 1. 1. 1.]
[1. 1. 1. 1.]
[1. 1. 1. 1.]]
上边补1行
[[0. 0. 0. 0.]
[1. 1. 1. 1.]
[1. 1. 1. 1.]
[1. 1. 1. 1.]]
下边补2行
[[1. 1. 1. 1.]
[1. 1. 1. 1.]
[1. 1. 1. 1.]
[0. 0. 0. 0.]
[0. 0. 0. 0.]]
左边补3行
[[0. 0. 0. 1. 1. 1. 1.]
[0. 0. 0. 1. 1. 1. 1.]
[0. 0. 0. 1. 1. 1. 1.]]
vct_右边补4行
[[1. 1. 1. 1. 0. 0. 0. 0.]
[1. 1. 1. 1. 0. 0. 0. 0.]
[1. 1. 1. 1. 0. 0. 0. 0.]]
2. 对于三维张量的pad,就是在 顶楼,底楼 ,每层楼的上 ,下,左,右填充
#pad1 = np.array([[‘顶’,‘底’],[‘上’,‘下’], [‘左’,‘右’ ]])
import numpyas np
import tensorflowas tf
tsr = tf.ones([2, 3, 4])
pad_top = [[1, 0], [0, 0], [0, 0]]
pad_left = [[0, 0], [0, 0], [3, 0]]
tsr_pad_top = tf.pad(tsr, pad_top, name='pad_top')
tsr_pad_left = tf.pad(tsr, pad_left, name='pad_left')
with tf.Session()as sess:
sess.run(tf.global_variables_initializer())
print('original tensor')
print(sess.run(tsr))
print(' pad top')
print(sess.run(tsr_pad_top))
print('pad left')
print(sess.run(tsr_pad_left))
输出:
original tensor
[[[1. 1. 1. 1.]
[1. 1. 1. 1.]
[1. 1. 1. 1.]]
[[1. 1. 1. 1.]
[1. 1. 1. 1.]
[1. 1. 1. 1.]]]
pad top
[[[0. 0. 0. 0.]
[0. 0. 0. 0.]
[0. 0. 0. 0.]]
[[1. 1. 1. 1.]
[1. 1. 1. 1.]
[1. 1. 1. 1.]]
[[1. 1. 1. 1.]
[1. 1. 1. 1.]
[1. 1. 1. 1.]]]
pad left
[[[0. 0. 0. 1. 1. 1. 1.]
[0. 0. 0. 1. 1. 1. 1.]
[0. 0. 0. 1. 1. 1. 1.]]
[[0. 0. 0. 1. 1. 1. 1.]
[0. 0. 0. 1. 1. 1. 1.]
[0. 0. 0. 1. 1. 1. 1.]]]
参考:
https://blog.csdn.net/luoganttcc/article/details/83303522