TensorFlow(3)CNN中的函数
tf.nn.conv2d()函数
参数介绍:
tf.nn.conv2d(input, filter, strides, padding, use_cudnn_on_gpu=None, name=None)
input:输入参数,具有这样的shape[batch, in_height, in_width, in_channels],分别是[batch张图片, 每张图片高度为in_height, 每张图片宽度为in_width, 图像通道为in_channels].
filter:滤波器,滤波器的shape为[filter_height, filter_width, in_channels, out_channels],分别对应[滤波器高度, 滤波器宽度, 接受图像的通道数, 卷积后通道数],其中第三个参数 in_channels需要与input中的第四个参数 in_channels一致.
strides:代表步长,其值可以直接默认一个数,也可以是一个四维数如[1,2,1,1],则其意思是水平方向卷积步长为第二个参数2,垂直方向步长为1.
padding:代表填充方式,参数只有两种,SAME和VALID,SAME比VALID的填充方式多了一列,比如一个3*3图像用2*2的滤波器进行卷积,当步长设为2的时候,会缺少一列,则进行第二次卷积的时候,VALID发现余下的窗口不足2*2会直接把第三列去掉,SAME则会填充一列,填充值为0.
use_cudnn_on_gpu:bool类型,是否使用cudnn加速,默认为true.
name:给返回的tensor命名。给输出feature map起名字.
例子:
一张3*3的图片,元素如下:
| * | * | * |
|---|---|---|
| 0 | 3 | 6 |
| 1 | 4 | 7 |
| 2 | 5 | 8 |
卷积核为1个2*2的卷积,如下:
| * | * |
|---|---|
| 0 | 2 |
| 1 | 3 |
TensorFlow代码(padding为SAME):
import tensorflow as tf
import numpy as np
g = tf.Graph()
with g.as_default() as g:
input = tf.Variable(np.array(range(9), dtype=np.float32).reshape(1,3,3,1))
filter = tf.Variable(np.array(range(4), dtype=np.float32).reshape(2,2,1,1))
op = tf.nn.conv2d(input, filter, strides=[1, 1, 1, 1], padding='SAME')
with tf.Session(graph=g) as sess:
sess.run(tf.global_variables_initializer())
a,b,c = sess.run([input, filter, op])
print(a)
print(b)
print(c)
输出:
[[[[ 0.]
[ 1.]
[ 2.]]
[[ 3.]
[ 4.]
[ 5.]]
[[ 6.]
[ 7.]
[ 8.]]]]
[[[[ 0.]]
[[ 1.]]]
[[[ 2.]]
[[ 3.]]]]
[[[[ 19.]
[ 25.]
[ 10.]]
[[ 37.]
[ 43.]
[ 16.]]
[[ 7.]
[ 8.]
[ 0.]]]]
即卷积后的结果为:
| * | * | * |
|---|---|---|
| 19 | 37 | 7 |
| 25 | 43 | 8 |
| 10 | 16 | 0 |
如果padding为VALID,则输出如下:
[[[[ 0.]
[ 1.]
[ 2.]]
[[ 3.]
[ 4.]
[ 5.]]
[[ 6.]
[ 7.]
[ 8.]]]]
[[[[ 0.]]
[[ 1.]]]
[[[ 2.]]
[[ 3.]]]]
[[[[ 19.]
[ 25.]]
[[ 37.]
[ 43.]]]]
即卷积后的结果为:
| * | * |
|---|---|
| 19 | 37 |
| 25 | 43 |
tf.nn.max_pool()函数
tf.nn.max_pool(value, ksize, strides, padding, name=None)
参数是四个,和卷积函数很类似:
value:需要池化的输入,一般池化层接在卷积层后面,所以输入通常是feature map,依然是[batch, height, width, channels]这样的shape.
ksize:池化窗口的大小,取一个四维向量,一般是[1, height, width, 1],因为我们不想在batch和channels上做池化,所以这两个维度设为了1.
strides:和卷积类似,窗口在每一个维度上滑动的步长,一般也是[1, stride,stride, 1].
padding:和卷积类似,可以取'VALID' 或者'SAME'.
返回一个Tensor,类型不变,shape仍然是[batch, height, width, channels]这种形式.
TensorFlow代码:
import tensorflow as tf
import numpy as np
g = tf.Graph()
with g.as_default() as g:
input = tf.Variable(np.array(range(9), dtype=np.float32).reshape(1,3,3,1))
filter = tf.Variable(np.array(range(4), dtype=np.float32).reshape(2,2,1,1))
op = tf.nn.conv2d(input, filter, strides=[1, 1, 1, 1], padding='SAME')
pool = tf.nn.max_pool(op, [1,2,2,1], [1,1,1,1], padding='SAME')
with tf.Session(graph=g) as sess:
sess.run(tf.global_variables_initializer())
PL = sess.run(pool)
print(PL)
输出:
[[[[ 43.]
[ 43.]
[ 16.]]
[[ 43.]
[ 43.]
[ 16.]]
[[ 8.]
[ 8.]
[ 0.]]]]
| * | * | * |
|---|---|---|
| 43 | 43 | 8 |
| 43 | 43 | 8 |
| 16 | 16 | 0 |
tf.nn.avg_pool()
计算方法: 计算非padding的元素的平均值
例子:
import tensorflow as tf
import numpy as np
g = tf.Graph()
with g.as_default() as g:
input = tf.Variable(np.array(range(9), dtype=np.float32).reshape(1,3,3,1))
filter = tf.Variable(np.array(range(4), dtype=np.float32).reshape(2,2,1,1))
op = tf.nn.conv2d(input, filter, strides=[1, 1, 1, 1], padding='SAME')
pool = tf.nn.avg_pool(op, [1,2,2,1], [1,1,1,1], padding='SAME')
with tf.Session(graph=g) as sess:
sess.run(tf.global_variables_initializer())
PL = sess.run(pool)
print(PL)
输出为:
[[[[31. ]
[23.5 ]
[13. ]]
[[23.75]
[16.75]
[ 8. ]]
[[ 7.5 ]
[ 4. ]
[ 0. ]]]]
| * | * | * |
|---|---|---|
| 31 | 23.75 | 7.5 |
| 23.5 | 16.75 | 4. |
| 13. | 8. | 0. |
tf.nn.dropout()
tf.nn.dropout(x, keep_prob, noise_shape=None, seed=None, name=None)
- x:输入参数
- keep_prob:保留比例。 取值 (0,1] 。每一个参数都将按这个比例随机变更
- noise_shape:干扰形状。 此字段默认是None,表示第一个元素的操作都是独立,但是也不一定。比例:数据的形状是shape(x)=[k, l, m, n],而noise_shape=[k, 1, 1, n],则第1和4列是独立保留或删除,第2和3列是要么全部保留,要么全部删除。
- seed:随机数种子
- name: 命名空间
tensorflow中的dropout就是:shape不变,使输入tensor中某些元素按照一定的概率变为0,其它没变0的元素变为原来的1/keep_prob.
dropout层的作用: 防止神经网络的过拟合
例子:
import tensorflow as tf
g = tf.Graph()
with g.as_default() as g:
mat = tf.Variable(tf.ones([10,10]))
dropout_mat = tf.nn.dropout(mat, keep_prob=0.5)
with tf.Session(graph=g) as sess:
sess.run(tf.global_variables_initializer())
output, dropout = sess.run([mat, dropout_mat])
print(output)
print(dropout)
输出:
[[1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]]
[[2. 0. 0. 0. 2. 0. 2. 2. 0. 2.]
[0. 2. 0. 0. 2. 2. 0. 0. 0. 0.]
[2. 2. 2. 0. 0. 2. 0. 2. 0. 0.]
[2. 0. 0. 0. 2. 2. 2. 0. 2. 0.]
[0. 2. 2. 0. 2. 2. 2. 2. 0. 2.]
[2. 0. 0. 0. 2. 0. 0. 2. 0. 2.]
[2. 2. 0. 2. 2. 0. 0. 0. 2. 2.]
[2. 0. 0. 0. 0. 2. 0. 2. 0. 0.]
[2. 2. 0. 0. 0. 0. 0. 2. 0. 0.]
[2. 0. 2. 2. 2. 2. 0. 2. 0. 0.]]
tf.reshape()
shape里最多有一个维度的值可以填写为-1,表示自动计算此维度
TensorFlow(3)CNN中的函数的更多相关文章
- 基于TensorFlow理解CNN中的padding参数
1 TensorFlow中用到padding的地方 在TensorFlow中用到padding的地方主要有tf.nn.conv2d(),tf.nn.max_pool(),tf.nn.avg_pool( ...
- 第三节,TensorFlow 使用CNN实现手写数字识别(卷积函数tf.nn.convd介绍)
上一节,我们已经讲解了使用全连接网络实现手写数字识别,其正确率大概能达到98%,这一节我们使用卷积神经网络来实现手写数字识别, 其准确率可以超过99%,程序主要包括以下几块内容 [1]: 导入数据,即 ...
- CNN中的卷积核及TensorFlow中卷积的各种实现
声明: 1. 我和每一个应该看这篇博文的人一样,都是初学者,都是小菜鸟,我发布博文只是希望加深学习印象并与大家讨论. 2. 我不确定的地方用了"应该"二字 首先,通俗说一下,CNN ...
- tensorflow实现lstm中遇到的函数记录
函数一:initializer=tf.random_uniform_initializer(-0.1, 0.1, seed=123) tf.random_uniform_initializer 参数: ...
- Tensorflow简单CNN实现
觉得有用的话,欢迎一起讨论相互学习~Follow Me 少说废话多写代码~ """转换图像数据格式时需要将它们的颜色空间变为灰度空间,将图像尺寸修改为同一尺寸,并将标签依 ...
- 由浅入深:CNN中卷积层与转置卷积层的关系
欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 本文由forrestlin发表于云+社区专栏 导语:转置卷积层(Transpose Convolution Layer)又称反卷积层或分数卷 ...
- TensorFlow基础笔记(11) conv2D函数
#链接:http://www.jianshu.com/p/a70c1d931395 import tensorflow as tf import tensorflow.contrib.slim as ...
- 2. Tensorflow的数据处理中的Dataset和Iterator
1. Tensorflow高效流水线Pipeline 2. Tensorflow的数据处理中的Dataset和Iterator 3. Tensorflow生成TFRecord 4. Tensorflo ...
- CNN中的卷积理解和实例
卷积操作是使用一个二维卷积核在在批处理的图片中进行扫描,具体的操作是在每一张图片上采用合适的窗口大小在图片的每一个通道上进行扫描. 权衡因素:在不同的通道和不同的卷积核之间进行权衡 在tensorfl ...
随机推荐
- _ZNote_Qt_添加图标方法
简单来说就两步: 将icns图标添加入资源文件,例如picture.icns .pro文件中添加 (图标) ICON = picture.icns 程序中添加(程序窗口上显示) setWindowIc ...
- noip第24课资料
- lightoj 1074
这题怎么说呢,负环上的点都不行 网上也有很多解法 我用dfs的spfa解的 我发现网上别人的代码都是用bfs的spfa写的,我就用的dfs的,快了好多 代码还看的别人的,只有中间的spfa是自己写的 ...
- 在已有数据的表中添加id字段并且自增
各位大牛,小弟在开发过程中,遇到了这样一个问题,由于新功能的增加需要使原有的一张表的结构作出调整,需要添加一个id主键字段,但是因为表里有很多数据了,所以,怎样才能添加这个字段,并且使原有的数据也能够 ...
- 使用Spring+MySql实现读写分离(一)关于windows下安装mysql5.6
前面讲过关于mysql的优化,主要是建表时对于大量数据的表添加索引机制,提高查询效率,以及一些sql语句的简单优化,毕竟我也不是专业的数据库管理员,大牛勿喷. 今天写两章关于javaweb项目中,对于 ...
- windows下安装nodejs以及python2502,2503解决方案
1. 2053和2052为什么会出现出现这个提示的时候,是在程序安装步骤 到达copy new file的时候 进入下一步进行报错,可以推测出应该是软件包在安装的时候,解压缩部署核心文件的时候出错. ...
- jackson 用法总结
1.序列化与反序列化封装 private static final Logger logger = LoggerFactory.getLogger(JsonUtil.class); /** * Obj ...
- Chapter 8 The Simplest Plug-in Solution
This chapter introduces the simplest plug-in solution that are applicable to the four major componen ...
- 优秀后端架构师必会知识:史上最全MySQL大表优化方案总结
本文原作者“ manong”,原创发表于segmentfault,原文链接:segmentfault.com/a/1190000006158186 1.引言 MySQL作为开源技术的代表作之一,是 ...
- 在C语言中不使用任何中间变量如何将a、b的值进行交换(三种方法)——来自一小萌新工程师的复习
今天面试嵌入式,突然遇到这么一道题目,虽然简单,但鉴于我答得不是很好,所以还是分析一下为好. 第一种方法: 通过加减法. #include"stdio.h" int main(vo ...