tensorflow-tf.nn.conv2d卷积运算(2)
tf.nn.conv2d( input, filter, strides, padding, use_cudnn_on_gpu=True, data_format='NHWC', dilations=[1, 1, 1, 1], name=None )
计算给定4d输入和过滤核张量的二维卷积。
给定形状[batch,in_height, in_width, in_channels]的输入张量和形状[filter_height, filter_width, in_channels, out_channels]的筛选/核张量,此op执行如下操作:
将过滤核压扁到一个二维矩阵,形状为[filter_height filter_width in_channels, output_channels]。
从输入张量中提取图像块,形成一个形状的虚拟张量[batch, out_height, out_width, filter_height filter_width in_channels]。
对于每个patch,右乘滤波器矩阵和图像patch向量。
output[b, i, j, k] = sum_{di, dj, q} input[b, strides[1] * i + di, strides[2] * j + dj, q] * filter[di, dj, q, k]
必须有strides(步长)[0]=strides[3]= 1。对于相同水平和顶点的最常见情况,stride = [1, stride, stride, 1]。
Args:
input:一个张量。必须是以下类型之一:half,bfloat16, float32, float64。一个四维张量。dimension顺序是根据data_format的值来解释的。
filter: 必须具有与输入相同的类型。形状的4维张量[filter_height, filter_width, in_channels, out_channels]。
strides: int型列表。长度为4的一维张量。每个输入维度的滑动窗口的跨步。顺序是根据data_format的值来解释的。
padding: 来自:“SAME”、“VALID”的字符串。要使用的填充算法的类型。
use_cudnn_on_gpu: 可选bool,默认为True.
data_format: 一个可选的字符串:“NHWC”、“NCHW”。默认为“NHWC”。指定输入和输出数据的数据格式。使用默认格式“NHWC”,数据按以下顺序存储:[批处理、高度、宽度、通道]。或者,格式可以是“NCHW”,数据存储顺序为:[批处理,通道,高度,宽度]。
dilations:int的可选列表。默认为[1,1,1,1]。长度为4的一维张量。每个输入维度的膨胀系数。如果设置为k > 1,则该维度上的每个过滤器元素之间将有k-1跳过单元格。维度顺序由data_format的值决定,详细信息请参阅上面的内容。批次的膨胀和深度尺寸必须为1。
name: 可选 名字
#!/usr/bin/env python2 # -*- coding: utf-8 -*- """ Created on Tue Oct 2 13:23:27 2018 @author: myhaspl @email:myhaspl@myhaspl.com tf.nn.conv2d """ import tensorflow as tf g=tf.Graph() with g.as_default(): x=tf.constant([[ [[1.,2.],[3.,4.],[5.,6.]], [[10.,20.],[30.,40.],[50.,60.]], ]]) kernel=tf.constant([[[[10.],[2.]]]]) y=tf.nn.conv2d(x,kernel,strides=[1,1,1,1],padding="SAME") with tf.Session(graph=g) as sess: print sess.run(x) print sess.run(kernel) print kernel.get_shape() print x.get_shape() print sess.run(y)
[[[[ 1. 2.] [ 3. 4.] [ 5. 6.]] [[10. 20.] [30. 40.] [50. 60.]]]] [[[[10.] [ 2.]]]] (1, 1, 2, 1) (1, 2, 3, 2) [[[[ 14.] [ 38.] 3344 [ 62.]] [[140.] [380.] [620.]]]]
import tensorflow as tf a = tf.constant([1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,1,1,0,0,1,1,0,0],dtype=tf.float32,shape=[1,5,5,1]) b = tf.constant([1,0,1,0,1,0,1,0,1],dtype=tf.float32,shape=[3,3,1,1]) c = tf.nn.conv2d(a,b,strides=[1, 2, 2, 1],padding='VALID') d = tf.nn.conv2d(a,b,strides=[1, 2, 2, 1],padding='SAME') with tf.Session() as sess: print ("c shape:") print (c.shape) print ("c value:") print (sess.run(c)) print ("d shape:") print (d.shape) print ("d value:") print (sess.run(d))
conv2d(
input,
filter,
strides,
padding,
use_cudnn_on_gpu=True,
data_format='NHWC',
name=None
)
c shape: (1, 2, 2, 1) c value: [[[[ 4.] [ 4.]] [[ 2.] [ 4.]]]] d shape: (1, 3, 3, 1) d value: [[[[ 2.] [ 3.] [ 1.]] [[ 1.] [ 4.] [ 3.]] [[ 0.] [ 2.] [ 1.]]]]
padding为VALID,采用丢弃的方式。
padding为SAME,采用的是补全的方式,补0
#!/usr/bin/env python2 # -*- coding: utf-8 -*- """ Created on Tue Oct 2 13:23:27 2018 @author: myhaspl @email:myhaspl@myhaspl.com tf.nn.conv2d """ import tensorflow as tf g=tf.Graph() with g.as_default(): x=tf.constant([ [ [[1.],[2.],[11.]], [[3.],[4.],[22.]], [[5.],[6.],[33.]] ], [ [[10.],[20.],[44.]], [[30.],[40.],[55.]], [[50.],[60.],[66.]] ] ])#2*3*3*1 kernel=tf.constant( [ [[[2.]],[[3.]]] ] )#2*2*1 y=tf.nn.conv2d(x,kernel,strides=[1,1,1,1],padding="SAME") with tf.Session(graph=g) as sess: print x.get_shape() print kernel.get_shape() print sess.run(y) print y.get_shape()
padding="SAME"
$12+23=8$
$22+311=37$
$112+03=22$
(2, 3, 3, 1) (1, 2, 1, 1) [[[[ 8.] [ 37.] [ 22.]] [[ 18.] [ 74.] [ 44.]] [[ 28.] [111.] [ 66.]]] [[[ 80.] [172.] [ 88.]] [[180.] [245.] [110.]] [[280.] [318.] [132.]]]] (2, 3, 3, 1)
- 图示理解卷积运算、逆卷积运算、Tensorflow、tf.nn.conv2d_transpose、Conv2DSlowBackpropInput: Size of out_backprop doesn
- 【TensorFlow】tf.nn.conv2d是怎样实现卷积的?
- 【tensorflow】 tf.nn.conv2d 卷积函数介绍
- 【TensorFlow】tf.nn.conv2d_transpose是怎样实现反卷积的?
- TensorFlow 用 tf.nn.conv2d 实现卷积操作
- 【TensorFlow】tf.nn.conv2d是怎样实现卷积的?
- 【TensorFlow】tf.nn.conv2d是怎样实现卷积的?
- Tensorflow中转置卷积的实现理解(tf.nn.conv2d_transpose)
- 【TensorFlow】tf.nn.conv2d是怎样实现卷积的?
- 【TensorFlow】tf.nn.conv2d是怎样实现卷积的?
- 【TensorFlow】tf.nn.conv2d是怎样实现卷积的?
- [TensorFlow 学习笔记-04]卷积函数之tf.nn.conv2d
- 【TensorFlow】tf.nn.conv2d是怎样实现卷积的?有1*1(1×1)卷积介绍
- tensorflow:tf.nn.conv2d是怎样实现卷积的?
- 【TensorFlow】tf.nn.conv2d是怎样实现卷积的?
- 【TensorFlow】tf.nn.conv2d是怎样实现卷积的?
- TensorFlow--tf.nn.conv2d是怎样实现卷积的?
- 【TensorFlow】tf.nn.conv2d是怎样实现卷积的?
- 【TensorFlow】tf.nn.conv2d是怎样实现卷积的?
- 【TensorFlow】tf.nn.conv2d是怎样实现卷积的?