说一下我理解的tf.pad(),先来看一下定义:

def pad(tensor, paddings, mode="CONSTANT", name=None, constant_values=0):

什么意思呢?目的就是对输入tensor进行扩展,那么扩展的宽度就由paddings来控制了;至于modeconstant_values则表示对tensor扩展时填充的方式。

一维tensor扩展:

import tensorflow as tf

tensor = tf.constant([[1, 2, 3]])
paddings = tf.constant([[1, 2], [3, 4]])
result = tf.pad(tensor, paddings) with tf.Session() as sess:
print(sess.run(result))
[[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, 2, 3]](其实还是二维的)四个方向进行扩展,paddings=[[1, 2], [3, 4]]分别就对应着上、下、左、右四个边界扩展的宽度;

二维tensor扩展:

import tensorflow as tf

tensor = tf.constant([[1, 2], [3, 4]])
paddings = tf.constant([[1, 2], [3, 4]])
result = tf.pad(tensor, paddings) with tf.Session() as sess:
print(sess.run(result))
[[0 0 0 0 0 0 0 0 0]
[0 0 0 1 2 0 0 0 0]
[0 0 0 3 4 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

tensor = tf.constant([[[1, 2, 3], [3, 4, 5]], [[5, 6, 7], [7, 8, 9]]]) # shape: (2, 2, 3)
paddings = tf.constant([[1, 2], [3, 4], [5, 6]])
result = tf.pad(tensor, paddings) with tf.Session() as sess:
print(tensor.shape) # shape: (2, 2, 3)
print(sess.run(result))
print(result.shape) # shape: (5, 9, 14)

输出结果如下:

paddings是一个\(3\times 2\)的矩阵,第一行[1, 2]表示对tensor的第一个维度进行扩展;第二行[3, 4]tensor的第二个维度进行扩展;第三行[5, 6]tensor的第三个维度进行扩展;

  可以看到,paddings的要求都是\(N\times 2\)的矩阵,其中\(N\)可能就是与tensor的维度相关了吧。

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

参数mode

tf.pad()方法提供了三种填充tensor的方式:

  • mode="CONSTNAT", constant_values=0: 默认,以常数值0来填充;
  • mode="REFLECT"
  • mode="SYMMETRIC"

不同modetensor的shape有着不同的要求。

tf.pad()的更多相关文章

  1. tf.pad(one_hot_encoding, [[0, 0], [1, 0]], mode='CONSTANT')

    tf.pad(one_hot_encoding, [[0, 0], [1, 0]], mode='CONSTANT') tf.pad 是扩展的意思,其中[0, 0], [1, 0] 分别代表的是[上, ...

  2. tensorflow 笔记 16:tf.pad

    函数: tf.compat.v1.pad tf.pad 函数表达式如下: tf.pad(    tensor,    paddings,    mode='CONSTANT',    name=Non ...

  3. 『TensorFlow』pad图片

    tf.pad()文档如下, pad(tensor, paddings, mode='CONSTANT', name=None, constant_values=0)    Pads a tensor. ...

  4. 解释张量及TF的一些API

    张量的定义 张量(Tensor)理论是数学的一个分支学科,在力学中有重要应用.张量这一术语起源于力学,它最初是用来表示弹性介质中各点应力状态的,后来张量理论发展成为力学和物理学的一个有力的数学工具.张 ...

  5. 【学习笔记】tensorflow基础

    目录 认识Tensorflow Tensorflow特点 下载以及安装 Tensorflow初体验 Tensorflow进阶 图 op 会话 Feed操作 张量 变量 可视化学习Tensorboard ...

  6. Self-organizing Maps及其改进算法Neural gas聚类在异常进程事件识别可行性初探

    catalogue . SOM简介 . SOM模型在应用中的设计细节 . SOM功能分析 . Self-Organizing Maps with TensorFlow . SOM在异常进程事件中自动分 ...

  7. 第三十六节,目标检测之yolo源码解析

    在一个月前,我就已经介绍了yolo目标检测的原理,后来也把tensorflow实现代码仔细看了一遍.但是由于这个暑假事情比较大,就一直搁浅了下来,趁今天有时间,就把源码解析一下.关于yolo目标检测的 ...

  8. 第七节,TensorFlow编程基础案例-TensorBoard以及常用函数、共享变量、图操作(下)

    这一节主要来介绍TesorFlow的可视化工具TensorBoard,以及TensorFlow基础类型定义.函数操作,后面又介绍到了共享变量和图操作. 一 TesnorBoard可视化操作 Tenso ...

  9. 『TensorFlow』读书笔记_ResNet_V2

    『PyTorch × TensorFlow』第十七弹_ResNet快速实现 要点 神经网络逐层加深有Degradiation问题,准确率先上升到饱和,再加深会下降,这不是过拟合,是测试集和训练集同时下 ...

随机推荐

  1. 【题解】P2602[JZOI2010]数字计数

    [题解][P2602ZJOI2010]数字计数 乍看此题,感觉直接从数字的位上面动手,感觉应该很容易. 但是仔细看数据范围,发现如果不利用计数原理,肯定会超时,考虑数码出现的特征: \(A000\)到 ...

  2. 【题解】P4799[CEOI2015 Day2]世界冰球锦标赛

    [题解][P4799 CEOI2015 Day2]世界冰球锦标赛 发现买票顺序和答案无关,又发现\(n\le40\),又发现从后面往前面买可以通过\(M\)来和从前面往后面买的方案进行联系.可以知道是 ...

  3. table表格隔行变色

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  4. 5.2 《锋利的jQuery》jQuery对表格的操作(选项卡/换肤)

    表格隔行变色以及单选/复选 表格展开关闭 表格筛选 字体变大/缩小 选项卡 网页换肤 tip1: $("tr:odd")和$("tr:even")选择器索引是从 ...

  5. poj 3041 Asteroids(二分图 *【矩阵实现】【最小点覆盖==最大匹配数】)

    Asteroids Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16379   Accepted: 8930 Descri ...

  6. python的easygui

    1.利用msgbox(单词messagebox的缩写)给出一个提示信息: import easygui as g reply=g.msgbox('This is a basic message box ...

  7. Redis常用数据结构和操作

    1.String 存入字符类型 Set name luowen 设置name = luowen 存储 Get name 获取设置好的name的值 Setnx name luowen 设置name键值为 ...

  8. 2017 google IO大会——5.17

    大家好! 每年一度的全球互联网及新型技术的盛会 Google IO,今年的大会日期和地址已经公布了:大会将在5月17至19日在谷歌公司总部边上的会场,美国加州 Mountain View的 Shore ...

  9. 递归------python实现列表创建二叉树

    # -*- coding:utf-8 -*- '二叉树结点类' class TreeNode: def __init__(self, x): self.val = x self.left = None ...

  10. MySQL_杭州拱墅区、西湖区近9-11月销售过的产品_20161125

    需求:杭州拱墅区.西湖区近9-11月销售过的产品 这个SQL如果对数据表熟悉的话可以一步到位,为了看的更清楚,时间间隔在3个月产品数量也不是很多,采取先找明细再进行汇总. 一.第一种写法 分步骤的写S ...