tf.nn.conv2d (input, filter, strides, padding, use_cudnn_on_gpu=None, data_format=None, name=None)
参数:
input : 输入的要做卷积的图片,要求为一个张量,shape为 [ batch, in_height, in_width, in_channel ],其中batch为图片的数量,in_height 为图片高度,in_width 为图片宽度,in_channel 为图片的通道数,灰度图该值为1,彩色图为3。(也可以用其它值,但是具体含义不是很理解)
filter(对应代码中的self.conv_): 卷积核,要求也是一个张量,shape为 [ filter_height, filter_width, in_channel, out_channels ],其中 filter_height 为卷积核高度,filter_width 为卷积核宽度,in_channel 是图像通道数 ,和 input 的 in_channel 要保持一致,out_channel 是卷积核数量。
strides: 卷积时在图像每一维的步长,这是一个一维的向量,[ 1, strides, strides, 1],第一位和最后一位固定必须是1
padding: string类型,值为“SAME” 和 “VALID”,表示的是卷积的形式,是否考虑边界。"SAME"是考虑边界,不足的时候用0去填充周围,"VALID"则不考虑
use_cudnn_on_gpu: bool类型,是否使用cudnn加速,默认为true
定义模型:
数据维度:output=(input−kernel)+2∗padding+1(代码中conv1:28 = (32-5) + 2*0 +1, conv2: 10 = (14-5) + 2*0 + 1)
class Model(object):
def __init__(self):
# 随机初始化张量参数
self.conv_W1 = tf.Variable(tf.random.normal(shape=(5, 5, 1, 6)))
self.conv_W2 = tf.Variable(tf.random.normal(shape=(5, 5, 6, 16)))
self.fc_W1 = tf.Variable(tf.random.normal(shape=(5*5*16, 120)))
self.fc_W2 = tf.Variable(tf.random.normal(shape=(120, 84)))
self.out_W = tf.Variable(tf.random.normal(shape=(84, 10)))
self.fc_b1 = tf.Variable(tf.zeros(120))
self.fc_b2 = tf.Variable(tf.zeros(84))
self.out_b = tf.Variable(tf.zeros(10))
def __call__(self, x):
x = tf.cast(x, tf.float32) # 转换输入数据类型
# 卷积层 1: Input = 32x32x1. Output = 28x28x6.
conv1 = tf.nn.conv2d(x, self.conv_W1, strides=[1, 1, 1, 1], padding='VALID')
# RELU 激活
conv1 = tf.nn.relu(conv1)
# 池化层 1: Input = 28x28x6. Output = 14x14x6.
pool1 = tf.nn.avg_pool(conv1, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='VALID')
# 卷积层 2: Input = 14x14x6. Output = 10x10x16.
conv2 = tf.nn.conv2d(pool1, self.conv_W2, strides=[1, 1, 1, 1], padding='VALID')
# RELU 激活
conv2 = tf.nn.relu(conv2)
# 池化层 2: Input = 10x10x16. Output = 5x5x16.
pool2 = tf.nn.avg_pool(conv2, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='VALID')
# 展平. Input = 5x5x16. Output = 400.
flatten = tf.reshape(pool2, [-1, 5*5*16])
# 全连接层
fc1 = tf.nn.relu(tf.add(tf.matmul(flatten, self.fc_W1), self.fc_b1))
fc2 = tf.nn.relu(tf.add(tf.matmul(fc1, self.fc_W2), self.fc_b2))
outs = tf.add(tf.matmul(fc2, self.out_W), self.out_b)
return outs
tf.nn.avg_pool(
value,
ksize,
strides,
padding,
data_format='NHWC',
name=None
)
参数:
value:需要做池化的输入图像,输入feaure map,因为池化在卷积后边。shape为[batch, in_height, in_width, in_channels]:
batch:训练时一个batch的图片数量
in_height:输入图像的高度
in_width:输入图像的宽度
in_channels:输入feature map的数量
ksize:类似于卷积的过滤器,池化窗口的大小,是一个长度为4的一维数组,但是这个数组的第一个和最后一数必须为1,即[1, height, width, 1]。这意味着池化层的过滤器是不可以在batch和channels上做池化。实际应用中,使用最多的过滤器尺寸为[1, 2, 2, 1]或者[1, 3, 3, 1]。height:过滤器的高度,width:过滤器的宽度
strides:不同维度上的步长,是一个长度为4的一维向量,[ 1, strides, strides, 1],第一维和最后一维的数字要求必须是1。因为卷积层的步长只对矩阵的长和宽有效。
padding:string类型,是否考虑边界,值为“SAME”和“VALID”,"SAME"是考虑边界,不足的时候用填充周围,"VALID"则不考虑边界。