Tensorflow Pad
函数定义
tf.pad(
tensor,
paddings,
mode='CONSTANT',
name=None,
constant_values=0
)
参数说明
- tensor :输入
- paddings :一个2*2的Tensor,分别是上、下、左、右的padding宽度
- mode :“CONSTANT”表示补常数(默认0),“REFLECT”效果上是用输入矩阵的边界以外的部分反向填充(看下面例子更直观),“SYMMETRIC”和“REFLECT”类似,但是填充物包含了输入矩阵的边界。
- constant_values :“CONSTANT”模式下的填充常数。
例子
1. CONSTANT
代码
x = tf.constant([[1,2,3], [4,5,6], [7,8,9]])
paddings = tf.constant([[1,2], [3,4]])
y = tf.pad(x, paddings, "CONSTANT")
结果
[[0 0 0 0 0 0 0 0 0 0]
[0 0 0 1 2 3 0 0 0 0]
[0 0 0 4 5 6 0 0 0 0]
[0 0 0 7 8 9 0 0 0 0]
[0 0 0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0 0 0]]
2. REFLECT
要求paddings不能超过原矩阵对应维度边长减一,这里原矩阵是 3 ∗ 3 3*3 3∗3的,所以paddings最大只能是2.
代码
x = tf.constant([[1,2,3], [4,5,6], [7,8,9]])
paddings = tf.constant([[1,1], [2,2]])
y = tf.pad(x, paddings, "REFLECT")
结果
[[6 5 4 5 6 5 4]
[3 2 1 2 3 2 1]
[6 5 4 5 6 5 4]
[9 8 7 8 9 8 7]
[6 5 4 5 6 5 4]]
3. SYMMETRIC
代码
x = tf.constant([[1,2,3], [4,5,6], [7,8,9]])
paddings = tf.constant([[3,3], [3,3]])
y = tf.pad(x, paddings, "SYMMETRIC")
结果
[[9 8 7 7 8 9 9 8 7]
[6 5 4 4 5 6 6 5 4]
[3 2 1 1 2 3 3 2 1]
[3 2 1 1 2 3 3 2 1]
[6 5 4 4 5 6 6 5 4]
[9 8 7 7 8 9 9 8 7]
[9 8 7 7 8 9 9 8 7]
[6 5 4 4 5 6 6 5 4]
[3 2 1 1 2 3 3 2 1]]