理解tf.pad

5 篇文章 1 订阅
2 篇文章 0 订阅

官方文档

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.

这里的[D, 0]如何理解,应该是[:,0],比如

paddings = tf.constant([[1, 1], [2, 2]])
paddings[:,0]

输出

<tf.Tensor: id=00001, shape=(2,), dtype=int32, numpy=array([1, 2])>

这个代表对应维度之间要添加的pad
比如例子中 paddings[:,0] == [1,2],第一个数是第零个维度,也就是二维张量里面的行数,它的值为1,也就是说在第零维中前面pad 1 个单位,第二个数是第一个维度,也就是二维张量里面的列数,它的值为2,也就是在第一个维度前面pad 2 个单位。
paddings[:,1] == [1,2] 是在对应维度后面 pad 相应单位。
二维张量的例子在官方文档有显示,再来看三维张量

t = tf.constant([[[1, 2, 3],[1,2,3],[1,2,3]], [[4, 5, 6],[4,5,6],[4,5,6]],[[7,8,9],[7,8,9],[7,8,9]]])

为了和 t tensor rank 数一致,paddings[D,0]后应该有三个数而不是两个数,所以

paddings = tf.constant([[0, 0],[0, 0],[0, 2]])
paddings[:,0] == [0,0,2]
tf.pad(t, paddings, "CONSTANT")

输出

<tf.Tensor: id=44136, shape=(3, 3, 5), dtype=int32, numpy=
array([[[1, 2, 3, 0, 0],
        [1, 2, 3, 0, 0],
        [1, 2, 3, 0, 0]],

       [[4, 5, 6, 0, 0],
        [4, 5, 6, 0, 0],
        [4, 5, 6, 0, 0]],

       [[7, 8, 9, 0, 0],
        [7, 8, 9, 0, 0],
        [7, 8, 9, 0, 0]]])>
paddings = tf.constant([[1, 0],[0, 0],[0, 0]])
paddings[:,0] == [1,0,0]
tf.pad(t, paddings, "CONSTANT")

输出

<tf.Tensor: id=44138, shape=(4, 3, 3), dtype=int32, numpy=
array([[[0, 0, 0],
        [0, 0, 0],
        [0, 0, 0]],

       [[1, 2, 3],
        [1, 2, 3],
        [1, 2, 3]],

       [[4, 5, 6],
        [4, 5, 6],
        [4, 5, 6]],

       [[7, 8, 9],
        [7, 8, 9],
        [7, 8, 9]]])>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值