tf.nn.conv2d(input, filter, strides, padding, use_cudnn_on_gpu=None, data_format=None, name=None)

官方教程说明:

给定四维的inputfilter tensor,计算一个二维卷积

Args:
  • input: A Tensor. type必须是以下几种类型之一: halffloat32float64.
  • filter: A Tensor. type和input必须相同
  • strides: A list of ints.一维,长度4, 在input上切片采样时,每个方向上的滑窗步长,必须和format指定的维度同阶
  • padding: A string from: "SAME", "VALID". padding 算法的类型
  • use_cudnn_on_gpu: An optional bool. Defaults to True.
  • data_format: An optional string from: "NHWC", "NCHW", 默认为"NHWC"
    指定输入输出数据格式,默认格式为"NHWC", 数据按这样的顺序存储:
    [batch, in_height, in_width, in_channels]
    也可以用这种方式:"NCHW", 数据按这样的顺序存储:
    [batch, in_channels, in_height, in_width]
  • name: 操作名,可选.
Returns:

Tensor. type与input相同

Given an input tensor of shape [batch, in_height, in_width, in_channels]
and a filter / kernel tensor of shape
[filter_height, filter_width, in_channels, out_channels]

conv2d实际上执行了以下操作:

  1. 将filter转为二维矩阵,shape为
    [filter_height * filter_width * in_channels, output_channels].
  2. 从input tensor中提取image patches,每个patch是一个virtual tensor,shape[batch, out_height, out_width, filter_height * filter_width * in_channels].
  3. 将每个filter矩阵和image patch向量相乘

具体来讲,当data_format为NHWC时:

  1. output[b, i, j, k] =
  2. sum_{di, dj, q} input[b, strides[1] * i + di, strides[2] * j + dj, q] *
  3. filter[di, dj, q, k]

input 中的每个patch都作用于filter,每个patch都能获得其他patch对filter的训练
需要满足strides[0] = strides[3] = 1. 大多数水平步长和垂直步长相同的情况下:strides = [1, stride, stride, 1].

下面举例来进行说明

在最基本的例子中,没有padding和stride = 1。让我们假设你的inputkernel有:

当您的内核您将收到以下输出:,它按以下方式计算:

  • 14 = 4 * 1 + 3 * 0 + 1 * 1 + 2 * 2 + 1 * 1 + 0 * 0 + 1 * 0 + 2 * 0 + 4 * 1
  • 6 = 3 * 1 + 1 * 0 + 0 * 1 + 1 * 2 + 0 * 1 + 1 * 0 + 2 * 0 + 4 * 0 + 1 * 1
  • 6 = 2 * 1 + 1 * 0 + 0 * 1 + 1 * 2 + 2 * 1 + 4 * 0 + 3 * 0 + 1 * 0 + 0 * 1
  • 12 = 1 * 1 + 0 * 0 + 1 * 1 + 2 * 2 + 4 * 1 + 1 * 0 + 1 * 0 + 0 * 0 + 2 * 1

TF的conv2d函数批量计算卷积,并使用稍微不同的格式。对于一个输入,它是[batch, in_height, in_width, in_channels]内核的[filter_height, filter_width, in_channels, out_channels]。所以我们需要以正确的格式提供数据:

  1. import tensorflow as tf
  2. k = tf.constant([
  3. [1, 0, 1],
  4. [2, 1, 0],
  5. [0, 0, 1]
  6. ], dtype=tf.float32, name='k')
  7. i = tf.constant([
  8. [4, 3, 1, 0],
  9. [2, 1, 0, 1],
  10. [1, 2, 4, 1],
  11. [3, 1, 0, 2]
  12. ], dtype=tf.float32, name='i')
  13. kernel = tf.reshape(k, [3, 3, 1, 1], name='kernel')
  14. image = tf.reshape(i, [1, 4, 4, 1], name='image')

之后,卷积用下式计算:

  1. res = tf.squeeze(tf.nn.conv2d(image, kernel, [1, 1, 1, 1], "VALID"))
  2. # VALID means no padding
  3. with tf.Session() as sess:
  4. print sess.run(res)

并将相当于我们手工计算的,输出结果:

[[ 14. 6.]
[ 6. 12.]]

附上一张图:

区别SAME和VALID

VALID

  1. input = tf.Variable(tf.random_normal([1,5,5,5]))
  2.  
  3. filter = tf.Variable(tf.random_normal([3,3,5,1]))
  4.  
  5. op = tf.nn.conv2d(input, filter, strides=[1, 1, 1, 1], padding='VALID')

输出图形:

  1. .....
  2. .xxx.
  3. .xxx.
  4. .xxx.
  5. .....

SAME

  1. input = tf.Variable(tf.random_normal([1,5,5,5]))
  2. filter = tf.Variable(tf.random_normal([3,3,5,1]))
  3.  
  4. op = tf.nn.conv2d(input, filter, strides=[1, 1, 1, 1], padding='SAME')

输出图形:

  1. xxxxx
  2. xxxxx
  3. xxxxx
  4. xxxxx
  5. xxxxx

参考链接

TensorFlow conv2d原理及实践的更多相关文章

  1. 转:fastText原理及实践(达观数据王江)

    http://www.52nlp.cn/fasttext 1条回复 本文首先会介绍一些预备知识,比如softmax.ngram等,然后简单介绍word2vec原理,之后来讲解fastText的原理,并 ...

  2. 使用腾讯云 GPU 学习深度学习系列之二:Tensorflow 简明原理【转】

    转自:https://www.qcloud.com/community/article/598765?fromSource=gwzcw.117333.117333.117333 这是<使用腾讯云 ...

  3. Atitit 管理原理与实践attilax总结

    Atitit 管理原理与实践attilax总结 1. 管理学分类1 2. 我要学的管理学科2 3. 管理学原理2 4. 管理心理学2 5. 现代管理理论与方法2 6. <领导科学与艺术4 7. ...

  4. Atitit.ide技术原理与实践attilax总结

    Atitit.ide技术原理与实践attilax总结 1.1. 语法着色1 1.2. 智能提示1 1.3. 类成员outline..func list1 1.4. 类型推导(type inferenc ...

  5. Atitit.异步编程技术原理与实践attilax总结

    Atitit.异步编程技术原理与实践attilax总结 1. 俩种实现模式 类库方式,以及语言方式,java futuretask ,c# await1 2. 事件(中断)机制1 3. Await 模 ...

  6. Atitit.软件兼容性原理与实践 v5 qa2.docx

    Atitit.软件兼容性原理与实践   v5 qa2.docx 1. Keyword2 2. 提升兼容性的原则2 2.1. What 与how 分离2 2.2. 老人老办法,新人新办法,只新增,少修改 ...

  7. Atitit 表达式原理 语法分析 原理与实践 解析java的dsl  递归下降是现阶段主流的语法分析方法

    Atitit 表达式原理 语法分析 原理与实践 解析java的dsl  递归下降是现阶段主流的语法分析方法 于是我们可以把上面的语法改写成如下形式:1 合并前缀1 语法分析有自上而下和自下而上两种分析 ...

  8. Atitit.gui api自动化调用技术原理与实践

    Atitit.gui api自动化调用技术原理与实践 gui接口实现分类(h5,win gui, paint opengl,,swing,,.net winform,)1 Solu cate1 Sol ...

  9. Atitit.提升语言可读性原理与实践

    Atitit.提升语言可读性原理与实践 表1-1  语言评价标准和影响它们的语言特性1 1.3.1.2  正交性2 1.3.2.2  对抽象的支持3 1.3.2.3  表达性3 .6  语言设计中的权 ...

随机推荐

  1. pb传输优化浅谈

    在正式切入今天要谈的优化之前,先碎碎念一些自己过去这几年的经历.很久没有登录过博客园了,今天也是偶然兴起打开上来看一下,翻看了下自己的随笔,最后一篇原创文章发布时间是2015年的4月,今天是2017年 ...

  2. lucene全文搜索之一:lucene的主要功能和基本结构(基于lucene5.5.3)

    前言:lucene并不是像solr或elastic那样提供现成的.直接部署可用的系统,而是一套jar包,提供了一些常见语言分词.构建索引和创建搜索器等等功能的API,我们常用到的也就是分词器.索引目录 ...

  3. ecshop的详细安装步骤

    从网上找个ecshop包,然后下载,解压,解压后的ecshop是不能直接用的,要更改几个目录的权限才能用. ecshop要放在www目录下,这样访问的话就可以直接 http://localhost/e ...

  4. Mac OSX 搭建 Apache php mySql phpMyAdmin 开发环境

    基本环境和配置 Mac 系统:  OS X EI Caption  10.11.4 当前Mac用户名: ceshi 需要熟知的几个基本概念和操作: 1. 新建一个终端默认的是路径是: /Users/当 ...

  5. Linux配置浮动IP实现WEB高可用

    在高可用集群环境中,一般都需要使用浮动IP来实现web高可用(High Availability). 浮动IP的概念以及为什么需要浮动IP请参考:浮动IP(FLOAT IP) 本篇文章主要讲实际操作步 ...

  6. HTTP协议 状态码 telnet 笔记分享

    最近计算机网络课讲到这个,实习的笔试也考到了,做笔记总结一下.

  7. 《javascript高级程序设计》笔记三

    第三章 基本概念 任何语言的核心必然会描述这门语言最基本的工作原理.这部分内容对我们来说,读起来很乏味,甚至会产生困意,但这部分内容却是重要的!我有幸拜读<JavaScript高级程序设计> ...

  8. php使用openssl进行数字签名验证

    <?php /** * Created by PhpStorm. * User: hanks * Date: 6/2/2017 * Time: 6:03 PM */ /* [数字签名] 使用完全 ...

  9. 第14章 Linux开机详细流程

    本文目录: 14.1 按下电源和bios阶段 14.2 MBR和各种bootloader阶段 14.2.1 boot loader 14.2.2 分区表 14.2.3 采用VBR/EBR方式引导操作系 ...

  10. python迭代器生成器(一)

    for循环可以用于python中任何序列类型,包括序列.元组以及字符串.例如: >>> for x in [1,2,3,4]: print(x * 2,end='')...2468 ...