tf.nn.conv2d() 函数详解

本文详细介绍使用TensorFlow实现卷积神经网络CNN的过程,包括如何定义卷积核、执行卷积操作,以及理解输入输出张量的形状变化。通过具体代码实例,读者可以深入掌握CNN在图像处理中的应用。
import tensorflow as tf
expand = tf.Variable(tf.truncated_normal([2,7,3,1]))
W = tf.Variable(tf.truncated_normal([2,3,1,8], stddev=0.1, seed=1), name='W')
conv = tf.nn.conv2d(
    expand,
    W,
    strides=[1, 1, 1, 1],  # 不同维度上的步长
    padding="VALID",  # 填充的方法,SAME或VALID,SAME表示添加全0填充,VALID表示不添加
    name="conv")
print(expand)  # shape = (2, 7, 3, 1)
print(W)       # shape = (2, 3, 1, 8)
print(conv)    # shape = (2, 6, 1, 8)
输入:

expand
[batch_size, height_1, width_1, channels]

batch_size 一个batch中样例的个数      2
height_1, 图片的高                                 7
width_1, 图片的宽                                  3
channels 通道数,也就是当前层的深度 1

W
[height_2, width_2, channels, output]

height_2, 过滤器filter的高                                                      2
width_2, 过滤器filter的宽                                                       3
channels, 通道数,和上面保持一致,也就是当前层的深度  1
output 输出的深度                                                                 8

输出:

conv
[batch_size, height_3, width_3, output]

batch_size, 一个batch中样例的个数,同上           2
height_3, 卷积结果的高度                                      6 = height_1 - height_2 + 1 = 7-2+1
width_3, 卷积结果的宽度                                       1 = width_1 - width_2 +1 = 3-3+1
output 输出的深度                                                  8

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值