原文博客:Doi技术团队 链接地址:https://blog.doiduoyi.com/authors/1584446358138 初心:记录优秀的Doi技术团队学习经历
这个是使用PaddlePaddle训练cifar10数据集的一个例子
问题:计算每层网络结构和输入输出尺寸和参数个数。不加BN?更深?每层的尺寸变化?更多结构?
def convolutional_neural_network(img):
print('输入层的shape:', img.shape)
conv_pool_1 = fluid.nets.simple_img_conv_pool(
input=img,
filter_size=5,
num_filters=20,
pool_size=2,
pool_stride=2,
act="relu")
print('第一层卷积池化层输出shape:', conv_pool_1.shape)
conv_pool_1 = fluid.layers.batch_norm(conv_pool_1)
conv_pool_2 = fluid.nets.simple_img_conv_pool(
input=conv_pool_1,
filter_size=5,
num_filters=50,
pool_size=2,
pool_stride=2,
act="relu")
print('第二层卷积池化层输出shape:', conv_pool_2.shape)
conv_pool_2 = fluid.layers.batch_norm(conv_pool_2)
conv_pool_3 = fluid.nets.simple_img_conv_pool(
input=conv_pool_2,
filter_size=5,
num_filters=50,
pool_size=2,
pool_stride=2,
act="relu")
print('第三层卷积池化层输出shape:', conv_pool_3.shape)
prediction = fluid.layers.fc(input=conv_pool_3, size=10, act='softmax')
print('全连接层输出shape:', prediction.shape)
return prediction
答1: 本网络的的输入数据的shape为(128, 3, 32, 32),所以输出如下:
PaddlePaddle的网络输出
输入层的shape: (-1, 3, 32, 32)
第一层卷积池化层输出shape: (-1, 20, 14, 14)
第二层卷积池化层输出shape: (-1, 50, 5, 5)
第三层卷积池化层输出shape: (-1, 50, 1, 1)
全连接层输出shape: (-1, 10)
答2: 在没有使用BN层之前:
加入了BN层之后:
用BN层的训练情况:
不使用BN层的训练情况:
从图中可以看出使用BN层的准确率更高,训练过程中损失值和准确率的幅度更加小。
答3: 因为该网络的第三层卷积池化层输出的宽和高都是1,所以不能再增加卷积池化层,如果再使用卷积池化层,就会报以下的错误。
EnforceNotMet: Due to the settings of padding(0), filter_size(5), dilation(1) and stride(1), the output size is less than 0, please check again. Input_size:1