tf.nn.conv2d

conv2d(
    input,
    filter,
    strides,
    padding,
    use_cudnn_on_gpu=True,
    data_format='NHWC',
    name=None
)
参数名必选类型说明
inputtensor是一个 4 维的 tensor,即 [ batch, in_height, in_width, in_channels ](若 input 是图像,[ 训练时一个 batch 的图片数量, 图片高度, 图片宽度, 图像通道数 ])
filtertensor是一个 4 维的 tensor,即 [ filter_height, filter_width, in_channels, out_channels ](若 input 是图像,[ 卷积核的高度,卷积核的宽度,图像通道数,卷积核个数 ]),filter 的 in_channels 必须和 input 的 in_channels 相等
strides列表长度为 4 的 list,卷积时候在 input 上每一维的步长,一般 strides[0] = strides[3] = 1
paddingstring只能为 " VALID "," SAME " 中之一,这个值决定了不同的卷积方式。VALID 丢弃方式;SAME:补全方式
use_cudnn_on_gpubool是否使用 cudnn 加速,默认为 true
data_formatstring只能是 " NHWC ", " NCHW ",默认 " NHWC "
namestring运算名称

 

示例代码:conv2d.py

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))

 运行结果

c shape:
(1, 2, 2, 1) # (5-3)/2+1=2  (input_size-kernel_size)/strides+1
c value:
[[[[4.]
   [4.]]

  [[2.]
   [4.]]]]
d shape:   
(1, 3, 3, 1)   # (7-3)/2+1=3
d value:
[[[[2.]
   [3.]
   [1.]]

  [[1.]
   [4.]
   [3.]]

  [[0.]
   [2.]
   [1.]]]]

卷积后数据体大小的计算:

padding='VALID': (input_size - kernel_size)/strides+1,即 (输入大小-卷积核大小)/步长+1

padding='SAME': input_size = input_size+2, (input_size - kernel_size)/strides+1

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值