方法定义

tf.nn.conv2d(input, filter, strides, padding, use_cudnn_on_gpu=True, data_format="NHWC", dilations=[1,1,1,1], name=None)

参数:

  • input:  输入的要做卷积的数据体,要求是一个`Tensor`
  • filter: 卷积核,要求也是一个`Tensor`, shape= [filter_height, filter_width, in_channels, out_channels], 其中 filter_height 为卷积核高度,filter_weight 为卷积核宽度,in_channel 是要做卷积的数据体的通道数 ,out_channel 是卷积核数量。
  • strides: 卷积步长(1-D tensor of length 4), shape=[1, strides, strides, 1],第一位和最后一位固定是1
  • padding: A `string` from: `"SAME", "VALID"`. "SAME" 表示使用0去填充边界, "VALID"则不填充
  • data_format: An optional `string` from: `"NHWC", "NCHW"`. Defaults to `"NHWC"`.
    Specify the data format of the input and output data.
    With the default format "NHWC", the data is stored in the order of:  [batch, height, width, channels].

  • name: A name for the operation (optional).

具体实现

input shape: [batch, in_height, in_width, in_channels]

filter shape: [filter_height, filter_width, in_channels, out_channels]

计算过程:

1. 将filter展开成2-D matrix, shape: [filter_height*filter_width*in_channels, output_channels]

2. 从input tensor中提取patches构成一个virtual tensor, shape: [batch, out_height, out_width, filter_height*filter_width*in_channels]

3. 对于每一个patch,右乘上1中的filter matrix。即 [batch, out_height, out_width, filter_height*filter_width*in_channels] x [filter_height * filter_width * in_channels, output_channels], 那么输出的shape: [batch, out_height, out_width, output_channels]

【注:必须有 strides[0] = strides[3] = 1】。绝大多数情况下,水平的stride和竖直的stride一样,即strides = [1, stride, stride, 1]。

输出结果的shape计算:

在caffe中是这样的:

out_height =floor(in_height+2*pad-filter_height)/stride+1; floor向下取整

out_width=floor(in_width+2*pad-filter_width)/stride+1

在TensorFlow中是这样的:

"SAME" 类型的padding:

out_height = ceil(in_height / strides[1]); ceil向上取整

out_width = ceil(in_width / strides[2])

"VALID"类型的padding:

out_height = ceil((in_height - filter_height + 1) / striders[1])

out_width = ceil((in_width - filter_width + 1) / striders[2]

验证代码

# -*- coding:utf-8 -*-

from __future__ import division
import tensorflow as tf
import numpy as np
import math
import pandas as pd input_arr = np.zeros((12, 15), dtype=np.float32)
number = 0
for row_idx in range(input_arr.shape[0]):
for col_idx in range(input_arr.shape[1]):
input_arr[row_idx][col_idx] = number
number +=1 number = 6
w_arr = np.zeros((2, 3), dtype=np.float32)
for row_idx in range(w_arr.shape[0]):
for col_idx in range(w_arr.shape[1]):
w_arr[row_idx][col_idx] = number
number += 1 stride = [1, 1, 1, 1] # 从卷积的定义【实际上不是卷积,而是cross-correlation】进行计算验证---对VALID类型卷积进行
res_shape_h = int(math.ceil((input_arr.shape[0] - w_arr.shape[0] + 1) / stride[1]))
res_shape_w = int(math.ceil(input_arr.shape[1] - w_arr.shape[1] + 1) / stride[2])
validation_res = np.zeros(shape=(res_shape_h, res_shape_w), dtype=np.float32) for row_idx in range(validation_res.shape[0]):
for col_idx in range(validation_res.shape[1]):
patch = input_arr[row_idx : row_idx+w_arr.shape[0], col_idx : col_idx+w_arr.shape[1]]
# 这里的 * 实际上代表的是点积,即对应元素位置相乘
res = np.sum(patch * w_arr)
validation_res[row_idx][col_idx] = res print('result of convolution from its definition: validation_res')
print(validation_res)
pd.DataFrame(validation_res).to_csv('validation_res.csv', index = False, header=False) # 从TensorFlow实现出发
input_arr = np.reshape(input_arr, [1, input_arr.shape[0], input_arr.shape[1], 1])
w_arr = np.reshape(w_arr, [w_arr.shape[0], w_arr.shape[1], 1, 1]) # 输入Tensor, shape: [1, 12, 15, 1]
net_in = tf.constant(value=input_arr, dtype=tf.float32) # filter, shape: [2, 3, 1, 1]
W = tf.constant(value=w_arr, dtype=tf.float32) # TensorFlow卷积的计算结果
# valid卷积结果, shape: [1, 11, 13, 1]
result_conv_valid = tf.nn.conv2d(net_in, W, stride, 'VALID')
# same卷积结果, shape: [1, 12, 15, 1]
result_conv_smae = tf.nn.conv2d(net_in, W, stride, 'SAME') with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
valid_conv_res, same_conv_res = sess.run([result_conv_valid, result_conv_smae]) print(valid_conv_res.shape)
valid_conv_res = np.reshape(valid_conv_res, [valid_conv_res.shape[1], valid_conv_res.shape[2]])
same_conv_res = np.reshape(same_conv_res, [same_conv_res.shape[1], same_conv_res.shape[2]])
print('TensorFlow con res: valid_conv_res')
print(valid_conv_res)
pd.DataFrame(valid_conv_res).to_csv('conv_res.csv', index=False, header=False)
pd.DataFrame(same_conv_res).to_csv('same_res.csv', index=False, header=False)

TensorFlow使用记录 (二): 理解tf.nn.conv2d方法的更多相关文章

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

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

  2. 深度学习原理与框架-Tensorflow卷积神经网络-卷积神经网络mnist分类 1.tf.nn.conv2d(卷积操作) 2.tf.nn.max_pool(最大池化操作) 3.tf.nn.dropout(执行dropout操作) 4.tf.nn.softmax_cross_entropy_with_logits(交叉熵损失) 5.tf.truncated_normal(两个标准差内的正态分布)

    1. tf.nn.conv2d(x, w, strides=[1, 1, 1, 1], padding='SAME')  # 对数据进行卷积操作 参数说明:x表示输入数据,w表示卷积核, stride ...

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

    tf.nn.conv2d是TensorFlow里面实现卷积的函数,参考文档对它的介绍并不是很详细,实际上这是搭建卷积神经网络比较核心的一个方法,非常重要 tf.nn.conv2d(input, fil ...

  4. tf.nn.conv2d卷积函数之图片轮廓提取

    一.tensorflow中二维卷积函数的参数含义:def conv2d(input, filter, strides, padding, use_cudnn_on_gpu=True, data_for ...

  5. tf.nn.conv2d 和 tf.nn.max_pool 中 padding 分别为 'VALID' 和 'SAME' 的直觉上的经验和测试代码

    这个地方一开始是迷糊的,写代码做比较分析,总结出直觉上的经验. 某人若想看精准的解释,移步这个网址(http://blog.csdn.net/fireflychh/article/details/73 ...

  6. TF-卷积函数 tf.nn.conv2d 介绍

    转自 http://www.cnblogs.com/welhzh/p/6607581.html 下面是这位博主自己的翻译加上测试心得 tf.nn.conv2d是TensorFlow里面实现卷积的函数, ...

  7. tf.nn.conv2d。卷积函数

    tf.nn.conv2d是TensorFlow里面实现卷积的函数,参考文档对它的介绍并不是很详细,实际上这是搭建卷积神经网络比较核心的一个方法,非常重要 tf.nn.conv2d(input, fil ...

  8. tf.nn.conv2d 参数介绍

    tf.nn.conv2d是TensorFlow里面实现卷积的函数,参考文档对它的介绍并不是很详细,实际上这是搭建卷积神经网络比较核心的一个方法,非常重要 tf.nn.conv2d(input, fil ...

  9. tf入门-tf.nn.conv2d是怎样实现卷积的?

    转自:https://blog.csdn.net/mao_xiao_feng/article/details/78004522 实验环境:tensorflow版本1.2.0,python2.7 介绍 ...

随机推荐

  1. windows下命令行利器---Cmder(安装,中文乱码,配置右键菜单)

    很多人都是在win下开发的,这样就会出现,经常需要命令行操作,而win cmd命令和linux命令有很大差异,导致大家很难受,今天给大家介绍一个win下命令行的利器-Cmder 一.先看一下它的容颜 ...

  2. 字符串转数组(php版)

    思路: 1.判断当前传来的值是否为数组 2.若不是现将传来的值转换为字符串类型 3.判断当前值是否为空 4.若不为空,采用正则进行匹配,如下图 preg_match('/^{.*?}$/', $str ...

  3. 下载安装npm和cnpm

    下载安装npm http://nodejs.cn/download/ 下载安装cnpm 完成npm的安装后,再安装cnpm https://npm.taobao.org/ 切换源为 taobao 源 ...

  4. A Horrible Poem (字符串hash+数论)

    # 10038. 「一本通 2.1 练习 4」A Horrible Poem [题目描述] 给出一个由小写英文字母组成的字符串 $S$,再给出 $q$ 个询问,要求回答 $S$ 某个子串的最短循环节. ...

  5. Nginx启动错误 Failed to read PID from file /run/nginx.pid 的处理方法

    问题产生原因 因为 nginx 启动需要一点点时间,而 systemd 在 nginx 完成启动前就去读取 pid file 造成读取 pid 失败 解决方法 让 systemd 在执行 ExecSt ...

  6. EJS学习(二)之语法规则上

    标签含义 <% %> :'脚本' 标签,用于流程控制,无输出即直接使用JavaScript语言. <%= %>:转义输出数据到模板(输出是转义 HTML 标签)即在后端定义的变 ...

  7. Windows访问VirtualBox的Redis服务器

    一般来讲,我们不愿意在Windows上面安装太多的软件,这样会导致Windows运行太慢. 所以我在windows上面安装了VirtualBox,然后把相关的软件都安装在virtualBox里面,比如 ...

  8. 用ant打包

    Eclipse 内置了 Ant . Ant 是一种类似于批处理程序的软件包,它主要繁琐的工作是编写和调试自动处理脚本(一个 XML 文件),但只要有了这个脚本,我们就可以一键完成所有的设定工作. 本节 ...

  9. 利用nethogs查看哪些进程占用网络带宽

    一.安装nethogs centos6版本安装: 1.安装依赖包 [root@hlsms-fensheng- ~]# yum install ncurses* 已加载插件:fastestmirror, ...

  10. C# 文件操作的一些小点子

    1. 判断指定文件是否存在: bool System.IO.File.Exits(string fliePath);