读者可能还记得本系列博客(二)和(六)中 tf.nn 模块,其中最关心的是 conv2d 这个函数。

首先将博客(二) MNIST 例程中 convolutional.py 关键源码列出:

  def model(data, train=False):"""The Model definition."""# 2D convolution, with 'SAME' padding (i.e. the output feature map has# the same size as the input). Note that {strides} is a 4D array whose# shape matches the data layout: [image index, y, x, depth].conv = tf.nn.conv2d(data,conv1_weights,strides=[1, 1, 1, 1],padding='SAME')# Bias and rectified linear non-linearity.relu = tf.nn.relu(tf.nn.bias_add(conv, conv1_biases))

看到第一个卷积层的实现使用 tf.nn.conv2d( input_tensor, weight_tensor, strides_param, padding_method) 这个函数。追踪至 tensorflow/tensorflow/python/ops/gen_nn_ops.py 这个文件中,将代码列出:

def conv2d(input, filter, strides, padding, use_cudnn_on_gpu=None,data_format=None, name=None):r"""Computes a 2-D convolution given 4-D `input` and `filter` tensors.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]`, this opperforms the following:1. Flattens the filter to a 2-D matrix with shape`[filter_height * filter_width * in_channels, output_channels]`.2. Extracts image patches from the input tensor to form a *virtual*tensor of shape `[batch, out_height, out_width,filter_height * filter_width * in_channels]`.3. For each patch, right-multiplies the filter matrix and the image patchvector.In detail, with the default NHWC format,output[b, i, j, k] =sum_{di, dj, q} input[b, strides[1] * i + di, strides[2] * j + dj, q] *filter[di, dj, q, k]Must have `strides[0] = strides[3] = 1`.  For the most common case of the samehorizontal and vertices strides, `strides = [1, stride, stride, 1]`.Args:input: A `Tensor`. Must be one of the following types: `float32`, `float64`.filter: A `Tensor`. Must have the same type as `input`.strides: A list of `ints`.1-D of length 4.  The stride of the sliding window for each dimensionof `input`. Must be in the same order as the dimension specified with format.padding: A `string` from: `"SAME", "VALID"`.The type of padding algorithm to use.use_cudnn_on_gpu: An optional `bool`. Defaults to `True`.data_format: An optional `string` from: `"NHWC", "NCHW"`. Defaults to `"NHWC"`.Specify the data format of the input and output data. With thedefault format "NHWC", the data is stored in the order of:[batch, in_height, in_width, in_channels].Alternatively, the format could be "NCHW", the data storage order of:[batch, in_channels, in_height, in_width].name: A name for the operation (optional).Returns:A `Tensor`. Has the same type as `input`."""return _op_def_lib.apply_op("Conv2D", input=input, filter=filter,strides=strides, padding=padding,use_cudnn_on_gpu=use_cudnn_on_gpu,data_format=data_format, name=name)

该文件内容为编译时自动生成。生成器的源码位于 tensorflow/tensorflow/python/framework/python_op_gen.h 和 python_op_gen.cc。

_op_def_lib 是这样构建的:

def _InitOpDefLibrary():op_list = op_def_pb2.OpList()text_format.Merge(_InitOpDefLibrary.op_list_ascii, op_list)op_def_registry.register_op_list(op_list)op_def_lib = op_def_library.OpDefLibrary()op_def_lib.add_op_list(op_list)return op_def_lib_InitOpDefLibrary.op_list_ascii = """%s"""_op_def_lib = _InitOpDefLibrary()

看到 _op_def_lib 实际上是个 op_def_pb2.OpList 对象,实现了记录 TensorFlow 支持全部运算的列表。

TensorFlow 从入门到精通(八):TensorFlow tf.nn.conv2d 一路追查相关推荐

  1. [TensorFlow 学习笔记-04]卷积函数之tf.nn.conv2d

    [版权说明] TensorFlow 学习笔记参考: 李嘉璇 著 TensorFlow技术解析与实战 黄文坚 唐源 著 TensorFlow实战郑泽宇  顾思宇 著 TensorFlow实战Google ...

  2. tensorflow从入门到精通100讲(七)-TensorFlow房价预估使用Keras快速构建模型

    前言 这篇文章承接上一篇tensorflow从入门到精通100讲(二)-IRIS数据集应用实战 https://wenyusuran.blog.csdn.net/article/details/107 ...

  3. Tensorflow系列 | Tensorflow从入门到精通(二):附代码实战

    作者 | AI小昕 编辑 | 安可 [导读]:本文讲了Tensorflow从入门到精通.欢迎大家点击上方蓝字关注我们的公众号:深度学习与计算机视觉. Tensor介绍 Tensor(张量)是Tenso ...

  4. 【TensorFlow】理解tf.nn.conv2d方法 ( 附代码详解注释 )

    最近在研究学习TensorFlow,在做识别手写数字的demo时,遇到了tf.nn.conv2d这个方法,查阅了官网的API 发现讲得比较简略,还是没理解.google了一下,参考了网上一些朋友写得博 ...

  5. 【TensorFlow】tf.nn.conv2d是怎样实现卷积的?

    int height_col= (height + 2 * pad_h - kernel_h) / stride_h + 1; int width_col = (width + 2 * pad_w - ...

  6. Tensorflow BatchNormalization详解:4_使用tf.nn.batch_normalization函数实现Batch Normalization操作...

    使用tf.nn.batch_normalization函数实现Batch Normalization操作 觉得有用的话,欢迎一起讨论相互学习~Follow Me 参考文献 吴恩达deeplearnin ...

  7. TensorFlow tf.nn.conv2d是怎样实现卷积的?

    [TensorFlow]tf.nn.conv2d是怎样实现卷积的? 原文:http://blog.csdn.net/mao_xiao_feng/article/details/78004522 实验环 ...

  8. tensorflow详解-tf.nn.conv2d(),tf.nn.max_pool()

    tf.nn.conv2d() 函数来计算卷积,weights 作为滤波器,[1, 2, 2, 1] 作为 strides.TensorFlow 对每一个 input 维度使用一个单独的 stride ...

  9. TensorFlow学习笔记(十七)tf.nn.conv2d

    在给定的4D input与filter下计算2D卷积输入shape为[batch, height, width, in_channels] TensorFlow的CNN代码中有 tf.nn.conv2 ...

最新文章

  1. git找回误删的文件
  2. 在C语言里_大学生:我学了这么久的C语言,为什么感觉它啥都做不了?网友:恰恰相反!...
  3. nodejs shell交互_nodejs调用shell
  4. 牛客练习赛29 题解
  5. c++ 使用nacos_超赞!用阿里开源的Nacos做SpringCloud注册中心真贴心...
  6. http://www.uupoop.com/ps/
  7. 【AAAI2021】NLP所有方向论文列表(情感分析、句法、NER、对话/问答、关系抽取、KD等)...
  8. 谷粒商城:Oss endpoint can‘t be empty.
  9. VTK(三)---在Linux系统上配置NDI Aurora磁导航API(用于手术导航系统的开发)
  10. 2022道路运输企业安全生产管理人员操作证考试题及在线模拟考试
  11. 物联网在工业企业的应用与实践 (1) 物联网与工业4.0
  12. android horizontalscrollview 动画,Android 用HorizontalScrollView实现滑动标签tabView
  13. 一幅图理解计算机系统硬件组成
  14. pwntcha库的安装依赖
  15. HbuilderX中的MuMu模拟器调试
  16. 微信小程序API-设备- 网络状态
  17. 【人工智能】作业1: Bait游戏 实验报告
  18. 一部适合有一点点lingo编程基础的人阅读的lingo入门教程——重学lingo,发现很多遗忘的小知识,并将其整理成册——运算符、数学函数、金融函数、概率密度函数、变量定界与集操作函数
  19. 表单控件<input>
  20. 洛谷是什么?hydro是什么

热门文章

  1. 旧手机升级android9,给力!一加两款旧旗舰获得安卓9.0升级:新增手势功能
  2. 21、控件使用之滚字轮UIC文件的组态和控件设置
  3. 阿迪达斯DevOps成熟度框架
  4. XJOI 1145 选班长
  5. app图标角标产品设计_APP图标风格与创意设计的方法分享
  6. day 16: 二叉树 补卡!
  7. python实现TCP通信代码以及错误98,99的解决方法
  8. miRNA数据库篇——TargetScan:哺乳动物miRNA靶基因数据库
  9. Linux中的黑洞(black hole)-/dev/null
  10. 09.06app端自动化