说一下我理解的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. VM tools安装错误The path "" is not a valid path to the xx generic kernel headers.

    VMWARE TOOLS安装提示THE PATH IS NOT A VALID PATH TO THE GENERIC KERNEL HEADERSI solved this problem, I g ...

  2. MongoDB入门学习(三):MongoDB的增删查改

            对于我们这样的菜鸟来说,最重要的不是数据库的管理,也不是数据库的性能,更不是数据库的扩展,而是怎么用好这款数据库,也就是一个数据库提供的最核心的功能,增删查改.         由于M ...

  3. cron表达式(转)

    原文地址:http://www.cnblogs.com/linjiqin/archive/2013/07/08/3178452.html Cron表达式是一个字符串,字符串以5或6个空格隔开,分为6或 ...

  4. ftl总结

    当前项目前端是用freemarker,是第一次使用这种页面,一般语法不介绍,这里只是记录工作中遇到的问题 ---------2016.6.25-------------- 1.关于ftl字符串的问题 ...

  5. Bootstrap——组件

    1.字体图标 <span class="glyphicon glyphicon-star" aria-hidden="true"></span ...

  6. java集合讲解干货集

    文章都来自网络,收集后便于查阅. 1.Java 集合系列01之 总体框架 2.Java 集合系列02之 Collection架构 3.Java 集合系列03之 ArrayList详细介绍(源码解析)和 ...

  7. bzoj 3685

    线段树 方法一: 值域线段树,递归去写的,每次节点存出现次数. 对于几个操作, 1,2 直接加减就好 ; 3,4 操作贪心往某一个方向找 .7也很简单,主要说前驱后继怎么找.我是先找这个数第几小,根据 ...

  8. L87

    Fear Makes Art More Engaging Emmanuel Kant spoke often about the sublime, and specifically how art b ...

  9. codeforces 660A A. Co-prime Array(水题)

    题目链接: A. Co-prime Array time limit per test 1 second memory limit per test 256 megabytes input stand ...

  10. hdu-5656 CA Loves GCD(dp+数论)

    题目链接: CA Loves GCD Time Limit: 6000/3000 MS (Java/Others)     Memory Limit: 262144/262144 K (Java/Ot ...