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如何处理work
一.前言 本文主要讲述下面两部分的内容: 1.将work挂入workqueue的处理过程 2.如何处理挂入workqueue的work 二.用户将一个work挂入workqueue 1.queue_w ...
- USB协议及认知
1.USB的拓扑结构决定了主机控制器就是最高统帅,没有主机控制器的要求设备永远不能主动发数据.所以主机控制器在USB 的世界里扮演着重要的角色,它是幕后操纵者. 2.数据包的发送, 这个过程包含很多信 ...
- Javascript变量名混淆细节
前言 UglifyJS会对JS文件的变量名进行混淆处理.要理解Javascript变量混淆的细节.我们须要回答下面几个问题: 1.遇到一个变量myName,我们怎么知道这个myName变量要不要混淆 ...
- stm32 usart的几种通信模式
一 USART 通用同步异步收发器(USART)提供了一种灵活的方法与使用工业标准NRZ异步串行数据格式的外部设备之间进行全双工数据交换. USART支持同步单向通信和半双工单线通信,也支持LIN(局 ...
- Spring依赖注入构造器注入(通过构造函数注入)
在src目录下建立applicationContext.xml (Spring 管理 bean的配置文件) <?xml version="1.0" encoding=&q ...
- linux安装php环境,yum安装php环境
用yum来管理安装Apache PHP Mysql的基本安装.1. 安装Apahce, PHP, Mysql, 以及php连接mysql库组件.yum -y install httpd php mys ...
- Redis(五):几个常用概念
1.Redis 默认有16384 solts(槽),每个槽可以存储多个hash值. 2.Redis 默认不需要密码: 2.1 注释去掉,设置自己的密码 3.设置密码后需要通过 3.1 -h 主机ip ...
- C 指向指针的指针
#include <stdio.h> int main() { char *cBooks[] = { "C程序设计语言", "C专家编程", &qu ...
- vim:将刚写的单词大写和单词的定义
最近打算把caps lock映射成<esc>键,那按起来多爽,现在的有一个小问题,如何快捷的输入大写字母. 用这个键盘映射搞定. inoremap <c-u> <esc& ...
- nfs missing codepage or helper program, or other error
[root@xxxxx ~]# /bin/mount -t nfs -o nosuid,noexec,nodev,noatime,intr,rsize=,wsize= xxx.xxx.xxx.xxx: ...