Tensorflow Pad

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 33的,所以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]]
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值