#链接: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函数的更多相关文章

  1. TensorFlow基础笔记(11) max_pool2D函数

    # def max_pool2d(inputs, # kernel_size, # stride=2, # padding='VALID', # data_format=DATA_FORMAT_NHW ...

  2. TensorFlow基础笔记(0) 参考资源学习文档

    1 官方文档 https://www.tensorflow.org/api_docs/ 2 极客学院中文文档 http://www.tensorfly.cn/tfdoc/api_docs/python ...

  3. TensorFlow基础笔记(3) cifar10 分类学习

    TensorFlow基础笔记(3) cifar10 分类学习 CIFAR-10 is a common benchmark in machine learning for image recognit ...

  4. TensorFlow基础笔记(14) 网络模型的保存与恢复_mnist数据实例

    http://blog.csdn.net/huachao1001/article/details/78502910 http://blog.csdn.net/u014432647/article/de ...

  5. TensorFlow基础笔记(8) TensorFlow简单人脸识别

    数据材料 这是一个小型的人脸数据库,一共有40个人,每个人有10张照片作为样本数据.这些图片都是黑白照片,意味着这些图片都只有灰度0-255,没有rgb三通道.于是我们需要对这张大图片切分成一个个的小 ...

  6. Tensorflow基础笔记

    1.Keras是一个由Python编写的开源人工神经网络库. 2.深度学习主要应用在三个大的方向,计算机视觉,自然语言处理,强化学习 3.计算机视觉主要有:图片识别,目标检测,语义分割,视频理解(行为 ...

  7. TensorFlow基础1:reduce_sum()函数和reduce_mean()函数

    https://blog.csdn.net/chengshuhao1991/article/details/78545723 在计算损失时,通常会用到reduce_sum()函数来进行求和,但是在使用 ...

  8. TensorFlow基础笔记(15) 编译TensorFlow.so,提供给C++平台调用

    参考 http://blog.csdn.net/rockingdingo/article/details/75452711 https://www.cnblogs.com/hrlnw/p/700764 ...

  9. TensorFlow基础笔记(0) tensorflow的基本数据类型操作

    import numpy as np import tensorflow as tf #build a graph print("build a graph") #生产变量tens ...

随机推荐

  1. Linux vm运行参数 - OOM相关的参数

    一.前言 本文是描述Linux virtual memory运行参数的第二篇,主要是讲OOM相关的参数的.为了理解OOM参数,第二章简单的描述什么是OOM.如果这个名词对你毫无压力,你可以直接进入第三 ...

  2. STM32 GPIO口模式配置

    F103系列 typedef struct { uint16_t GPIO_Pin; /*!< Specifies the GPIO pins to be configured. This pa ...

  3. 温故而知新 gulp.src 指定数组文件夹

    gulp.src语法是基于这个库来实现的,所以详情请看这个API: https://www.gulpjs.com.cn/docs/api/ https://github.com/isaacs/node ...

  4. python标准库介绍——27 random 模块详解

    ==random 模块== "Anyone who considers arithmetical methods of producing random digits is, of cour ...

  5. HTML: < 和 > 是何方神圣

    懂HTML的,都知道 < 表示 <,> 表示 >,那还有什么好写呢? 知道是知道,记不记得住是另外一回事,今天用到这两家伙,又给忘记了,还要特意查了下. 缩写不好记,如果能知道 ...

  6. [hihoCoder] #1081 : 最短路径·一

    时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 万圣节的早上,小Hi和小Ho在经历了一个小时的争论后,终于决定了如何度过这样有意义的一天——他们决定去闯鬼屋! 在鬼屋门口 ...

  7. Interception c# code

    http://www.codetails.com/2012/12/02/intercepting-method-calls-using-il/20121202/ http://blogs.msdn.c ...

  8. 黑客编程教程(八)编写NT服务

    先介绍一下什么是NT服务,实际上就是一个可以在系统启动时自动在一定身份下启动的,伴随着系统长期存在的进程. 一个NT服务有三部分构成: :Service Control Manager(SCM) 每个 ...

  9. javascript原型继承---constructor篇

    很多人对constructor的理解是指向对象的构造函数,今天才发现这种理解是有偏差的... 其实, constructor指向的不是实例化实例的构造函数,而是实例化该对象的构造函数的原型的构造函数 ...

  10. tornado源码分析-模块介绍

    1.Core web framework tornado.web - web框架功能模块,包括RequestHandler和Application两个重要的类 tornado.httpserver - ...