TensorFlow基础笔记(11) conv2D函数
#链接:http://www.jianshu.com/p/a70c1d931395
import tensorflow as tf
import tensorflow.contrib.slim as slim # tf.nn.conv2d(input, filter, strides, padding, use_cudnn_on_gpu=None, name=None)
# 除去name参数用以指定该操作的name,与方法有关的一共五个参数:
#
# input:
# 指需要做卷积的输入图像,它要求是一个Tensor,具有[batch, in_height, in_width, in_channels]这样的shape,具体含义是[训练时一个batch的图片数量, 图片高度, 图片宽度, 图像通道数],注意这是一个4维的Tensor,要求类型为float32和float64其中之一
#
# filter:
# 相当于CNN中的卷积核,它要求是一个Tensor,具有[filter_height, filter_width, in_channels, out_channels]这样的shape,具体含义是[卷积核的高度,卷积核的宽度,图像通道数,卷积核个数],要求类型与参数input相同,有一个地方需要注意,第三维in_channels,就是参数input的第四维
#
# strides:卷积时在图像每一维的步长,这是一个一维的向量,长度4
#
# padding:
# string类型的量,只能是”SAME”,”VALID”其中之一,这个值决定了不同的卷积方式(后面会介绍)
# SAME 表示输出的out_height, out_width与输入的in_height, in_width相同
# VALID 表示输出的图像大小小于输入图像大小,输出的大小计算公式如下:
# out_height = round((in_height - floor(filter_height / 2) * 2) / strides_height) floor表示下取整 round表示四舍五入
# use_cudnn_on_gpu:
# bool类型,是否使用cudnn加速,默认为true #而对于tf.contrib.slim.conv2d,其函数定义如下: # convolution(inputs,
# num_outputs,
# kernel_size,
# stride=1,
# padding='SAME',
# data_format=None,
# rate=1,
# activation_fn=nn.relu,
# normalizer_fn=None,
# normalizer_params=None,
# weights_initializer=initializers.xavier_initializer(),
# weights_regularizer=None,
# biases_initializer=init_ops.zeros_initializer(),
# biases_regularizer=None,
# reuse=None,
# variables_collections=None,
# outputs_collections=None,
# trainable=True,
# scope=None):
#
# inputs****同样是****指需要做卷积的输入图像
# num_outputs****指定卷积核的个数(就是filter****的个数)
# kernel_size****用于指定卷积核的维度****(卷积核的宽度,卷积核的高度)
# stride****为卷积时在图像每一维的步长
# padding****为padding****的方式选择,VALID****或者SAME
# data_format****是用于指定输入的****input****的格式
# rate****这个参数不是太理解,而且tf.nn.conv2d****中也没有,对于使用atrous convolution的膨胀率(不是太懂这个atrous convolution)
# activation_fn****用于激活函数的指定,默认的为ReLU函数
# normalizer_fn****用于指定正则化函数
# normalizer_params****用于指定正则化函数的参数
# weights_initializer****用于指定权重的初始化程序
# weights_regularizer****为权重可选的正则化程序
# biases_initializer****用于指定biase****的初始化程序
# biases_regularizer: biases****可选的正则化程序
# reuse****指定是否共享层或者和变量
# variable_collections****指定所有变量的集合列表或者字典
# outputs_collections****指定输出被添加的集合
# trainable:****卷积层的参数是否可被训练
# scope:****共享变量所指的variable_scope input = tf.Variable(tf.round(10 * tf.random_normal([1, 6, 6, 1])))
filter = tf.Variable(tf.round(5 * tf.random_normal([3, 3, 1, 1])))
#op2 = tf.nn.conv2d(input, filter, strides=[1, 1, 1, 1], padding='VALID') conv_SAME = tf.nn.conv2d(input, filter, strides=[1, 1, 1, 1], padding='SAME')
conv_VALID = tf.nn.conv2d(input, filter, strides=[1, 2, 2, 1], padding='VALID')
slim_conv2d_SAME = slim.conv2d(input, 1, [3, 3], [1, 1], weights_initializer=tf.ones_initializer, padding='SAME')
slim_conv2d_VALID = slim.conv2d(input, 1, [3, 3], [2, 2], weights_initializer=tf.ones_initializer, padding='VALID') with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
conv_SAME_value, conv_VALID_value, slim_conv2d_SAME_value, slim_conv2d_VALID_value = \
sess.run([conv_SAME, conv_VALID, slim_conv2d_SAME, slim_conv2d_VALID])
print(conv_SAME_value.shape)
print(conv_VALID_value.shape)
print(slim_conv2d_SAME_value.shape)
print(slim_conv2d_VALID_value.shape) input = tf.Variable(tf.round(10 * tf.random_normal([1, 7, 7, 1])))
filter = tf.Variable(tf.round(5 * tf.random_normal([3, 3, 1, 1])))
#op2 = tf.nn.conv2d(input, filter, strides=[1, 1, 1, 1], padding='VALID') conv_SAME = tf.nn.conv2d(input, filter, strides=[1, 1, 1, 1], padding='SAME')
conv_VALID = tf.nn.conv2d(input, filter, strides=[1, 2, 2, 1], padding='VALID')
slim_conv2d_SAME = slim.conv2d(input, 1, [3, 3], [1, 1], weights_initializer=tf.ones_initializer, padding='SAME')
slim_conv2d_VALID = slim.conv2d(input, 1, [3, 3], [2, 2], weights_initializer=tf.ones_initializer, padding='VALID') with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
conv_SAME_value, conv_VALID_value, slim_conv2d_SAME_value, slim_conv2d_VALID_value = \
sess.run([conv_SAME, conv_VALID, slim_conv2d_SAME, slim_conv2d_VALID])
print(conv_SAME_value.shape)
print(conv_VALID_value.shape)
print(slim_conv2d_SAME_value.shape)
print(slim_conv2d_VALID_value.shape) #输出
# (1, 6, 6, 1)
# (1, 2, 2, 1)
# (1, 6, 6, 1)
# (1, 2, 2, 1) # (1, 7, 7, 1)
# (1, 3, 3, 1)
# (1, 7, 7, 1)
# (1, 3, 3, 1)
#coding=utf-8 #http://blog.csdn.net/mao_xiao_feng/article/details/78004522
# tf.nn.conv2d(input, filter, strides, padding, use_cudnn_on_gpu=None, name=None)
# 除去name参数用以指定该操作的name,与方法有关的一共五个参数:
#
# input:
# 指需要做卷积的输入图像,它要求是一个Tensor,具有[batch, in_height, in_width, in_channels]这样的shape,具体含义是[训练时一个batch的图片数量, 图片高度, 图片宽度, 图像通道数],注意这是一个4维的Tensor,要求类型为float32和float64其中之一
#
# filter:
# 相当于CNN中的卷积核,它要求是一个Tensor,具有[filter_height, filter_width, in_channels, out_channels]这样的shape,具体含义是[卷积核的高度,卷积核的宽度,图像通道数,卷积核个数],要求类型与参数input相同,有一个地方需要注意,第三维in_channels,就是参数input的第四维
#
# strides:卷积时在图像每一维的步长,这是一个一维的向量,长度4
#
# padding:
# string类型的量,只能是”SAME”,”VALID”其中之一,这个值决定了不同的卷积方式(后面会介绍)
#
# use_cudnn_on_gpu:
# bool类型,是否使用cudnn加速,默认为true import tensorflow as tf
#case 2
input = tf.Variable(tf.round(10 * tf.random_normal([1,3,3,2])))
filter = tf.Variable(tf.round(5 * tf.random_normal([1,1,2,1])))
op2 = tf.nn.conv2d(input, filter, strides=[1, 1, 1, 1], padding='VALID')
#对于filter,多个输入通道,变成一个输入通道,是对各个通道上的卷积值进行相加 # case 2
# input: [[[[-14. -11.]
# [ 2. 2.]
# [ 25. 18.]]
#
# [[ 8. 13.]
# [ -7. -7.]
# [ 11. 6.]]
#
# [[ -1. 8.]
# [ 18. 10.]
# [ -2. 19.]]]]
#转换:输入为3*3的2通道数据
#通道1:
#[-14 2 25],
#[8 -7 11],
#[-1 18 -2]
#通道2:
#[-11 2 18],
#[13 -7 6],
#[8 10 19] # filter: [[[[-3.]
# [ 2.]]]] # conv [[[[ 20.]
# [ -2.]
# [-39.]]
#
# [[ 2.]
# [ 7.]
# [-21.]]
#
# [[ 19.]
# [-34.]
# [ 44.]]]] #conv转换
#[20 -2 -39],
#[2 -7 -21],
#[9 -34 44] #计算过程
#[-14 2 25],
#[8 -7 11], * [-3] +
#[-1 18 -2]
#[-11 2 18],
#[13 -7 6], * [2]
#[8 10 19]
#result
#[20 -2 -39],
#[2 -7 -21],
#[9 -34 44] # #case 3
# input = tf.Variable(tf.random_normal([1,3,3,5]))
# filter = tf.Variable(tf.random_normal([3,3,5,1])) # op3 = tf.nn.conv2d(input, filter, strides=[1, 1, 1, 1], padding='VALID')
# #case 4
# input = tf.Variable(tf.random_normal([1,5,5,5]))
# filter = tf.Variable(tf.random_normal([3,3,5,1]))
#
# op4 = tf.nn.conv2d(input, filter, strides=[1, 1, 1, 1], padding='VALID')
# #case 5
# input = tf.Variable(tf.random_normal([1,5,5,5]))
# filter = tf.Variable(tf.random_normal([3,3,5,1]))
#
# op5 = tf.nn.conv2d(input, filter, strides=[1, 1, 1, 1], padding='SAME')
# #case 6
# input = tf.Variable(tf.random_normal([1,5,5,5]))
# filter = tf.Variable(tf.random_normal([3,3,5,7]))
#
# op6 = tf.nn.conv2d(input, filter, strides=[1, 1, 1, 1], padding='SAME')
# #case 7
# input = tf.Variable(tf.random_normal([1,5,5,5]))
# filter = tf.Variable(tf.random_normal([3,3,5,7]))
#
# op7 = tf.nn.conv2d(input, filter, strides=[1, 2, 2, 1], padding='SAME')
# #case 8
# input = tf.Variable(tf.random_normal([10,5,5,5]))
# filter = tf.Variable(tf.random_normal([3,3,5,7]))
#
# op8 = tf.nn.conv2d(input, filter, strides=[1, 2, 2, 1], padding='SAME') init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
print("case 2")
print("input: ", sess.run(input))
print("filter: ", sess.run(filter))
print("conv ", sess.run(op2))
# print("case 3")
# print(sess.run(op3))
# print("case 4")
# print(sess.run(op4))
# print("case 5")
# print(sess.run(op5))
# print("case 6")
# print(sess.run(op6))
# print("case 7")
# print(sess.run(op7))
# print("case 8")
# print(sess.run(op8))
TensorFlow基础笔记(11) conv2D函数的更多相关文章
- TensorFlow基础笔记(11) max_pool2D函数
# def max_pool2d(inputs, # kernel_size, # stride=2, # padding='VALID', # data_format=DATA_FORMAT_NHW ...
- TensorFlow基础笔记(0) 参考资源学习文档
1 官方文档 https://www.tensorflow.org/api_docs/ 2 极客学院中文文档 http://www.tensorfly.cn/tfdoc/api_docs/python ...
- TensorFlow基础笔记(3) cifar10 分类学习
TensorFlow基础笔记(3) cifar10 分类学习 CIFAR-10 is a common benchmark in machine learning for image recognit ...
- TensorFlow基础笔记(14) 网络模型的保存与恢复_mnist数据实例
http://blog.csdn.net/huachao1001/article/details/78502910 http://blog.csdn.net/u014432647/article/de ...
- TensorFlow基础笔记(8) TensorFlow简单人脸识别
数据材料 这是一个小型的人脸数据库,一共有40个人,每个人有10张照片作为样本数据.这些图片都是黑白照片,意味着这些图片都只有灰度0-255,没有rgb三通道.于是我们需要对这张大图片切分成一个个的小 ...
- Tensorflow基础笔记
1.Keras是一个由Python编写的开源人工神经网络库. 2.深度学习主要应用在三个大的方向,计算机视觉,自然语言处理,强化学习 3.计算机视觉主要有:图片识别,目标检测,语义分割,视频理解(行为 ...
- TensorFlow基础1:reduce_sum()函数和reduce_mean()函数
https://blog.csdn.net/chengshuhao1991/article/details/78545723 在计算损失时,通常会用到reduce_sum()函数来进行求和,但是在使用 ...
- TensorFlow基础笔记(15) 编译TensorFlow.so,提供给C++平台调用
参考 http://blog.csdn.net/rockingdingo/article/details/75452711 https://www.cnblogs.com/hrlnw/p/700764 ...
- TensorFlow基础笔记(0) tensorflow的基本数据类型操作
import numpy as np import tensorflow as tf #build a graph print("build a graph") #生产变量tens ...
随机推荐
- Concurrency Managed Workqueue(三)创建workqueue代码分析
一.前言 本文主要以__alloc_workqueue_key函数为主线,描述CMWQ中的创建一个workqueue实例的代码过程. 二.WQ_POWER_EFFICIENT的处理 __alloc_w ...
- kubernetes 二
部署harbor Habor是由VMWare中国团队开源的容器镜像仓库.事实上,Habor是在Docker Registry上进行了相应的企业级扩展,从而获 得了更加广泛的应用,这些新的企业级特性包括 ...
- struts2 页面标签或ognl表达式取值--未完待续
一.加#号取值和不加#号取值的解说 1.s:property 标签——value属性使用事项 1)涉及问题:取值时什么时候该加#,什么时候不加? 2)介绍 <s:property value=& ...
- JavaScript:零星知识
1. 关于document.write() 如果在文档已完成加载后执行 document.write,整个HTML 页面将被覆盖. 2. 对代码行进行折行 您可以在文本字符串中使用反斜杠对代码行进行换 ...
- 【Asp.net之旅】--因自己定义控件注冊而引发的思考
前言 近期在开发远洋的SOA系统平台,开发使用的是.NET平台.对于Asp.net并不困难,但该系统的开发并非全然依靠Asp.net.而是自身封装好的框架.这套框架是远洋地产购买的微软的开发平台,项目 ...
- haproxy 配置mysql的代理
应用场境,是如果mysql服务器没有外网的,需要一个有外网的代理服务器做代理,这时就可以用haproxy做个四层的代理: listen mysql bind mode tcp balance roun ...
- jquery50个代码段
1. 如何创建嵌套的过滤器 ? 1 //允许你减少集合中的匹配元素的过滤器, //只剩下那些与给定的选择器匹配的部分.在这种情况下, //查询删除了任何没(:not)有(:has) //包含class ...
- 使用mybatisplus实现动态路由
1.pom.xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns="h ...
- 如何把高版本的sqlserver 还原到低版本的 sqlserver
本例为sql2012 还原到sql2008. 要实现的功能是把sql2012的数据库备份到sql2008,数据库名字为Test,并且这两个数据库在不同的电脑中. 微软的软件设计方案基本上都是新版本兼容 ...
- ORM框架为什么不流行了
程序员的逻辑是先写sql脚本,然后在编写对应的实体代码. orm框架的逻辑是先写实体代码,然后自动生成脚本,构建数据库,这和程序员的逻辑或习惯刚好相反,所以这类ORM框架渐渐的被淘汰了,例如:Hibe ...