tf.pad使用

TensorFlow pad函数使用方法

  • 函数定义
tf.pad(
    tensor,
    paddings,
    mode='CONSTANT',
    name=None,
    constant_values=0
)
  • 参数说明
tensor      被填充的张量
paddings    填充的格式
mode        填充模式:"CONSTANT""REFLECT""SYMMETRIC"
name        该操作张量的名称
constant_values 用于在"CONSTANT"模式下,设置的填充值
  • 使用例子
    (二维、三维、其他维度类推)
import tensorflow as tf

t1 = tf.constant([[1, 2, 3], [4, 5, 6]])     #
# [1,1]代表在第一维度pad,即在t1的第一维上填充
# [2,2]代表在第二维度pad,即在t1的第二维上填充
paddings1 = tf.constant([[1, 1], [2, 2]])   

t2 = tf.constant([[[1,2,3], [4,5,6], [7,8,9]]])     #
# [0,0]代表在第一维度pad,即在t2的第一维上填充
# [2,2]代表在第二维度pad,即在t2的第二维上填充
# [1,1]代表在第二维度pad,即在t2的第三维上填充
paddings2 = tf.constant([[0, 0], [2, 2], [1, 1]]) 

with tf.Session() as sess:

    # 例子1 二维
    op = tf.pad(t1, paddings1, "CONSTANT")
    print(t1.get_shape())
    print(paddings1.get_shape())
    print(sess.run(op))

    # 例子2 三维
    print(t2.get_shape())
    print(paddings2.get_shape())
    op = tf.pad(t2, paddings2, "CONSTANT")
    print(sess.run(op))
  • 输出结果
    输出结果
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值