Tensorflow中一些方法的使用

1. tf.pad()

函数形式及参数

 tf.pad(tensor,paddings, mode='CONSTANT',name=None)

tensor:就是要填充的张量
paddings :代表每一维填充多少行或列
参考:https://blog.csdn.net/qq_40994943/article/details/85331327,https://www.cnblogs.com/chenzhen0530/p/10816149.html
mode 可以取三个值,表示三种不同的填充方法,分别是"CONSTANT" ,“REFLECT”,“SYMMETRIC”
1.mode=“CONSTANT” 直接填充0
2.mode="REFLECT"映射填充,上下(第1维)填充顺序和paddings是相反的,左右(第0维)顺序补齐。
3.mode="SYMMETRIC"对称填充,上下(第1维)填充顺序是和paddings相同的,左右(第0维)对称补齐。

以示例进行解释

对一维tensor进行填充:

import tensorflow as tf

t = tf.constant([[1, 2, 3]])#
paddings = tf.constant([[1, 2], [3, 4]])
t_padding = tf.pad(t, paddings)
with tf.Session() as sess:
    print(sess.run(t_padding))

一维填充的输出结果

[[0 0 0 0 0 0 0 0 0 0]
 [0 0 0 1 2 3 0 0 0 0]
 [0 0 0 0 0 0 0 0 0 0]
 [0 0 0 0 0 0 0 0 0 0]]

可以看到输出是一个二维矩阵并且在矩阵向上填充1行,向下填充两行,右边填充4行,左边填充3行。是在默认mode=“CONSTANT”下填充的,因此,填充的时0。
对二维tensor进行填充:

import tensorflow as tf

t = tf.constant([[1, 2, 3], [4, 5, 6]])
paddings = tf.constant([[1, 2], [3, 4]])
t_padding  = tf.pad(t, paddings)
with tf.Session() as sess:
    print(sess.run(t_padding ))

二维填充的输出结果

[[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 0 0 0 0 0 0 0]
 [0 0 0 0 0 0 0 0 0 0]]

可以看到填充结果与一维填充效果一致,维度没有改变,仍是二维。
对三维tensor进行填充:

import tensorflow as tf

t = tf.constant([[[1, 2, 3], [3, 4, 5]], [[5, 6, 7], [7, 8, 9]]])
paddings = tf.constant([[1, 2], [3, 4], [5, 6]])
t_padding = tf.pad(t, paddings)
with tf.Session() as sess:
    print(sess.run(t_padding)) 

三维填充的输出结果
先看一下原始三维数组:

[[[1 2 3]
  [3 4 5]]
  
 [[5 6 7]
  [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 0 0 0 0]
  [0 0 0 0 0 0 0 0 0 0 0 0 0 0]
  [0 0 0 0 0 0 0 0 0 0 0 0 0 0]
  [0 0 0 0 0 0 0 0 0 0 0 0 0 0]
  [0 0 0 0 0 0 0 0 0 0 0 0 0 0]
  [0 0 0 0 0 0 0 0 0 0 0 0 0 0]
  [0 0 0 0 0 0 0 0 0 0 0 0 0 0]
  [0 0 0 0 0 0 0 0 0 0 0 0 0 0]]

 [[0 0 0 0 0 0 0 0 0 0 0 0 0 0]
  [0 0 0 0 0 0 0 0 0 0 0 0 0 0]
  [0 0 0 0 0 0 0 0 0 0 0 0 0 0]
  [0 0 0 0 0 1 2 3 0 0 0 0 0 0]
  [0 0 0 0 0 3 4 5 0 0 0 0 0 0]
  [0 0 0 0 0 0 0 0 0 0 0 0 0 0]
  [0 0 0 0 0 0 0 0 0 0 0 0 0 0]
  [0 0 0 0 0 0 0 0 0 0 0 0 0 0]
  [0 0 0 0 0 0 0 0 0 0 0 0 0 0]]

 [[0 0 0 0 0 0 0 0 0 0 0 0 0 0]
  [0 0 0 0 0 0 0 0 0 0 0 0 0 0]
  [0 0 0 0 0 0 0 0 0 0 0 0 0 0]
  [0 0 0 0 0 5 6 7 0 0 0 0 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 0 0 0 0 0 0 0 0 0 0]
  [0 0 0 0 0 0 0 0 0 0 0 0 0 0]
  [0 0 0 0 0 0 0 0 0 0 0 0 0 0]]

 [[0 0 0 0 0 0 0 0 0 0 0 0 0 0]
  [0 0 0 0 0 0 0 0 0 0 0 0 0 0]
  [0 0 0 0 0 0 0 0 0 0 0 0 0 0]
  [0 0 0 0 0 0 0 0 0 0 0 0 0 0]
  [0 0 0 0 0 0 0 0 0 0 0 0 0 0]
  [0 0 0 0 0 0 0 0 0 0 0 0 0 0]
  [0 0 0 0 0 0 0 0 0 0 0 0 0 0]
  [0 0 0 0 0 0 0 0 0 0 0 0 0 0]
  [0 0 0 0 0 0 0 0 0 0 0 0 0 0]]

 [[0 0 0 0 0 0 0 0 0 0 0 0 0 0]
  [0 0 0 0 0 0 0 0 0 0 0 0 0 0]
  [0 0 0 0 0 0 0 0 0 0 0 0 0 0]
  [0 0 0 0 0 0 0 0 0 0 0 0 0 0]
  [0 0 0 0 0 0 0 0 0 0 0 0 0 0]
  [0 0 0 0 0 0 0 0 0 0 0 0 0 0]
  [0 0 0 0 0 0 0 0 0 0 0 0 0 0]
  [0 0 0 0 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维上方填充1个二维矩阵,下方填充2个二维矩阵。在第一维上方填充三个一维向量下方填充四个一维向量。在第0维上左边填充5个0右边填充6个0.

mode=REFLECT的效果

import tensorflow as tf

t = tf.constant([[2,3,4],[5,6,7]])
paddings = tf.constant([[1,1],[2,2]])
t_padding = tf.pad(t, paddings, mode="REFLECT")
with tf.Session() as sess:
    print(sess.run(t_padding)) 

结果如下:

[[7, 6, 5, 6, 7, 6, 5],
[4, 3, 2, 3, 4, 3, 2],
[7, 6, 5, 6, 7, 6, 5],
[4, 3, 2, 3, 4, 3, 2]]

mode=SYMMETRIC的效果

import tensorflow as tf

t = tf.constant([[2,3,4],[5,6,7]])
paddings = tf.constant([[1,1],[2,2]])
t_padding = tf.pad(t, paddings, mode="SYMMETRIC")
with tf.Session() as sess:
    print(sess.run(t_padding)) 

结果如下:

  [[3, 2, 2, 3, 4, 4, 3],
   [3, 2, 2, 3, 4, 4, 3],
   [6, 5, 5, 6, 7, 7, 6],
   [6, 5, 5, 6, 7, 7, 6]]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

凉寒

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值