【TensorFlow】TensorFlow函数精讲之tf.nn.conv2d()
博客之星评选,谢谢您的支持!微信、qq五连击投票(无需关注、无需登录)
人工智能博士(投票链接):http://m234140.nofollow.ax.mvote.cn/opage/4fddfa73-b1fa-b047-f666-98f84c9c25c4.html
tf.nn.conv2d是TensorFlow里面实现卷积的函数,是搭建卷积神经网络比较核心的一个方法。
函数格式:
tf.nn.conv2d(input, filter, strides, padding, use_cudnn_on_gpu = Noen, name = None)
参数说明:
第一个参数input:指需要做卷积的输入图像,它要求是一个4维的Tensor,类型为float32和float64之一,shape为[batch, in_height, in_width, in_channels]:
- batch:训练时一个batch的图片数量
- in_height:输入图像的高度
- in_width:输入图像的宽度
- in_channels:输入图像的通道数,灰度图像则为1,彩色图像则为3
第二个参数filter:CNN卷积网络中的卷积核,要求是一个Tensor,类型和input类型相同,shape为[filter_height, filter_width, in_channels, out_channels]:
- filter_height:卷积核的高度
- filter_width:卷积核的宽度
- in_channels:图像的通道数,input的in_channels相同
- out_channels:卷积核的个数
第三个参数strides:不同维度上的步长,是一个长度为4的一维向量,[ 1, strides, strides, 1],第一维和最后一维的数字要求必须是1。因为卷积层的步长只对矩阵的长和宽有效。
第四个参数padding:string类型,表示卷积的形式,是否考虑边界,值为“SAME”和“VALID”,"SAME"是考虑边界,不足的时候用填充周围,"VALID"则不考虑边界。
第五个参数use_cudnn_on_gpu: bool类型,是否使用cudnn加速,默认为true。
该函数返回的就是我们常说的feature map,shape仍然是[batch, height, width, channels]
这种形式。
下边通过例子来说明tf.nn.conv2d()函数的用法:
case1:输入是1张 3*3 大小的图片,图像通道数是5,卷积核是 1*1 大小,数量是1 ,步长是[1,1,1,1],最后得到一个 3*3 的feature map。1张图最后输出就是一个 shape为[1,3,3,1] 的tensor。
#!/usr/bin/env python
# -*- coding:utf-8 -*-import tensorflow as tfInput = tf.Variable(tf.random_normal([1, 3, 3, 5]))
Filter = tf.Variable(tf.random_normal([1, 1, 5, 1]))conv1 = tf.nn.conv2d(Input, Filter, strides=[1, 1, 1, 1], padding='VALID')with tf.Session() as sess:# 初始化变量op_init = tf.global_variables_initializer()sess.run(op_init)print(sess.run(conv1))
运行结果:
case2:输入是1张 3*3 大小的图片,图像通道数是5,卷积核是 2*2大小,数量是1 ,步长是[1,1,1,1],padding 设置为“VALID”,最后得到一个 2*2的feature map。1张图最后输出就是一个 shape为[1,2,2,1] 的tensor。
#!/usr/bin/env python
# -*- coding:utf-8 -*-import tensorflow as tfInput = tf.Variable(tf.random_normal([1, 3, 3, 5]))
Filter = tf.Variable(tf.random_normal([2, 2, 5, 1]))conv2 = tf.nn.conv2d(Input, Filter, strides=[1, 1, 1, 1], padding='VALID')with tf.Session() as sess:# 初始化变量op_init = tf.global_variables_initializer()sess.run(op_init)print(sess.run(conv2))
运行结果:
case3:将case2例子的padding值改为“SAME”,即考虑边界。(输入是1张 3*3 大小的图片,图像通道数是5,卷积核是 2*2大小,数量是1 ,步长是[1,1,1,1],padding 设置为“SAME”,最后得到一个 3*3的feature map。1张图最后输出就是一个 shape为[1,3,3,1] 的tensor。)
#!/usr/bin/env python
# -*- coding:utf-8 -*-import tensorflow as tfInput = tf.Variable(tf.random_normal([1, 3, 3, 5]))
Filter = tf.Variable(tf.random_normal([2, 2, 5, 1]))conv3= tf.nn.conv2d(Input, Filter, strides=[1, 1, 1, 1], padding='SAME')with tf.Session() as sess:# 初始化变量op_init = tf.global_variables_initializer()sess.run(op_init)print(sess.run(conv3))
运行结果为:
case4:输入是2张 3*3 大小的图片,图像通道数是5,卷积核是 2*2大小,数量是1,步长是[1,1,1,1],padding 设置为“SAME”,最后得到2个 3*3的feature map。1张图最后输出就是一个 shape为[2,3,3,1] 的tensor。
#!/usr/bin/env python
# -*- coding:utf-8 -*-import tensorflow as tfInput = tf.Variable(tf.random_normal([2, 3, 3, 5]))
Filter = tf.Variable(tf.random_normal([2, 2, 5, 1]))conv4 = tf.nn.conv2d(Input, Filter, strides=[1, 1, 1, 1], padding='SAME')with tf.Session() as sess:# 初始化变量op_init = tf.global_variables_initializer()sess.run(op_init)print(sess.run(conv4))
运行结果:
case4:输入是4张 3*3 大小的图片,图像通道数是5,卷积核是 2*2大小,数量是4,步长是[1,1,1,1],padding 设置为“SAME”,最后每张图片得到4个 3*3的feature map。1张图最后输出就是一个 shape为[4,3,3,4] 的tensor。
#!/usr/bin/env python
# -*- coding:utf-8 -*-import tensorflow as tfInput = tf.Variable(tf.random_normal([4, 3, 3, 5]))
Filter = tf.Variable(tf.random_normal([2, 2, 5, 4]))conv5 = tf.nn.conv2d(Input, Filter, strides=[1, 1, 1, 1], padding='SAME')with tf.Session() as sess:# 初始化变量op_init = tf.global_variables_initializer()sess.run(op_init)print(sess.run(conv5))
运行结果为:
[[[[ 4.1313605 -4.5319715 4.3040133 -0.13911057][-10.3389225 -2.2463617 -3.033617 2.0877244 ][ -2.3154557 5.4515543 -4.647153 -3.0869713 ]][[ 3.0420232 -0.87613493 4.6381464 0.90558195][ 3.7932742 -2.4520369 -0.7195463 4.9921722 ][ 1.5384624 0.11533099 -2.408429 5.3733883 ]][[ 0.45401835 -2.7483764 0.07065094 0.443908 ][ -6.8117185 2.2884533 -5.3677235 1.5834118 ][ -1.2048031 1.848783 -1.6733127 -1.7782905 ]]][[[ 0.5970616 0.77205956 -3.116805 -2.0577238 ][ -1.9527029 0.34587446 -5.7365794 -7.39325 ][ 2.9361765 -3.504585 4.057106 1.7304868 ]][[ 7.262891 -2.492215 4.7126684 1.7249267 ][ -7.4239445 2.9972248 4.2400084 0.9729483 ][ 1.9529393 3.4738922 0.24985534 -2.922786 ]][[ -0.3828507 0.49657637 0.47466695 0.8126482 ][ -0.3671472 -0.67494106 0.46129555 -0.9638461 ][ 1.6664319 0.885748 0.31974202 -1.9972321 ]]][[[ -1.7906806 -1.0376648 1.7166338 -1.0403266 ][ 2.5069232 4.0962963 -1.6884253 -0.23492575][ -0.3881849 -2.4799151 3.8491216 -2.5564618 ]][[ -3.5605621 1.6819414 -4.8645535 -1.9251393 ][ -1.8077193 -5.6664057 4.750779 1.2238139 ][ 2.346087 -6.2254734 5.1787786 4.3055882 ]][[ 0.21012396 -2.145918 1.358845 -0.25860584][ -2.3559752 4.263964 -0.51586103 -6.9163604 ][ 5.504827 4.0707703 0.11547554 -7.818963 ]]][[[ 3.716207 0.63655686 -1.2434999 -4.472955 ][ 2.067041 2.0510454 4.2357826 -4.159449 ][ 0.63638914 1.9863756 0.42491168 0.13413942]][[ 2.5624473 5.041215 3.689196 -1.1119944 ][ 4.470556 6.4554853 -7.154124 3.396954 ][ -4.9033055 -4.636659 3.7072423 -0.6310719 ]][[ -0.92771626 5.868825 -3.1153893 -3.9012384 ][ 3.4494157 -2.7036839 5.6135087 3.6358144 ][ 1.0283424 -4.246024 0.7325883 0.4899721 ]]]]
【TensorFlow】TensorFlow函数精讲之tf.nn.conv2d()相关推荐
- 【TensorFlow】TensorFlow函数精讲之tf.nn.max_pool()和tf.nn.avg_pool()
tf.nn.max_pool()和tf.nn.avg_pool()是TensorFlow中实现最大池化和平均池化的函数,在卷积神经网络中比较核心的方法. 有些和卷积很相似,可以参考TensorFlow ...
- 【TensorFlow】TensorFlow函数精讲之tf.nn.softmax_cross_entropy_with_logits
tf.nn.softmax_cross_entropy_with_logits()函数是TensorFlow中计算交叉熵常用的函数. 后续版本中,TensorFlow更新为:tf.nn.softmax ...
- 【TensorFlow】TensorFlow函数精讲之 tf.nn.relu()
tf.nn.relu()函数是将大于0的数保持不变,小于0的数置为0,函数如图1所示. ReLU函数是常用的神经网络激活函数之一. 图1 ReLU函数图像 下边为ReLU例子: import tens ...
- TensorFlow基础篇(七)——tf.nn.conv2d()
tf.nn.conv2d是TensorFlow里面实现卷积的函数,是搭建卷积神经网络比较核心的一个方法. 函数格式: tf.nn.conv2d(input, filter, strides, padd ...
- TensorFlow学习笔记(十七)tf.nn.conv2d
在给定的4D input与filter下计算2D卷积输入shape为[batch, height, width, in_channels] TensorFlow的CNN代码中有 tf.nn.conv2 ...
- 【TensorFlow】TensorFlow函数精讲之tf.get_variable()和tf.get_variable_scope()
目录 1.tf.get_variable() 2.tf.variable_scope() 3.tf.variable_scope() 函数嵌套 1.tf.get_variable() tf.get_v ...
- 【TensorFlow】TensorFlow函数精讲之tf.train.ExponentialMovingAverage()
tf.train.ExponentialMovingAverage来实现滑动平均模型. 格式: tf.train.ExponentialMovingAverage(decay,num_step) 参数 ...
- 【TensorFlow】TensorFlow函数精讲之tf.contrib.layers.l1regularizer()-12_regularizer(lambda)
TensorFlow中计算L1正则化和L2正则化的函数: L1正则化:tf.contrib.layers.l1regularizer(lambda)(w)函数,它可以返回一个函数,这个函数可以计算一个 ...
- 【TensorFlow】TensorFlow函数精讲之 tf.random_normal()
tf.trandom_normal()函数是生成正太分布随机值 此函数有别于tf.truncated_normal()正太函数,请参考本博客关于tf.truncated_normal()函数的介绍 ( ...
最新文章
- php生成指定范围随机数两位小数_python学习之随机数函数
- 从城市大脑到世界数字大脑 构建人类协同发展的超级智能平台
- common pool2 mysql_用common-pool自定义资源池
- 运行windows live writer时发生“意外错误”
- 前端学习(706):do-while案例
- linux怎么设置tomcat自动启动,linux添加tomcat服务并设置开机启动
- Makefile变量
- server sql 数据总行数_一种快速统计SQL Server每个表行数的方法
- jboss7 应用详解_【扔掉说明书114】本田 思域 2020款 舒适与娱乐功能详解
- 海思3516ev300+ imx335 原理图,其他PCB、软件资料齐全
- python单位根检验看结果_时间序列的ADF检验(单位根检验)
- ipa包安装到苹果手机中的几种方式——Qt for IOS
- python 二维转一维_Numpy 将二维图像矩阵转换为一维向量的方法
- python 导出excel 可筛选_python中实现excel的高级筛选
- ROS小车打造(12)--Arduino订阅cmd_vel实现差速控制
- 工业互联网·制药设备远程监控运维维护平台
- 使用python对字符串进行md5加密
- WAF是什么?又有什么作用?
- HTTP 重定向状态码是什么意思?
- mysql中授权主机通配的_什么意思_windows和linux虚拟机配置mysql主从
热门文章
- opengl双三次bezier曲面_试驾艾瑞泽5 PLUS:双外观设计,搭L2级全速域驾驶辅助,月销要破万?...
- 深度linux运行卡顿,Deepin很卡怎么办?Deepin卡顿解决方法盘点
- Esxi 6.5u2升级Esxi 6.7
- 表格列展示自动扩展_进步一点点:excel表格常规操作也能很快捷
- oracle里子连接查询,pc端页面滚动到底部加载更多数据......
- 未来计算机硬件的发展有可能使用的技术,未来计算机硬件的发展有可能使用的技术是______。...
- 一些关于图论和二叉树的
- 使用gridlayout布局后,因某些原因又删除,并整理文件夹结构时,Unable to resolve target #39;android-7#39;...
- 安卓下设置系统字体大小影响H5页面布局
- 妈妈再也不用担心别人问我是否真正用过redis了