关于dnn层的理论问题,使用keras进行Batchnormal化

时间:2017-08-17 09:09:50

标签: keras cudnn batch-normalization

在使用keras的特定情况下,使用batchnormal化来理解DNN的模型时遇到了一些麻烦。有人可以解释一下我建立的这个模型中每一层的结构和内容吗?

modelbatch = Sequential()
modelbatch.add(Dense(512, input_dim=1120))
modelbatch.add(BatchNormalization())
modelbatch.add(Activation('relu'))
modelbatch.add(Dropout(0.5))

modelbatch.add(Dense(256))
modelbatch.add(BatchNormalization())
modelbatch.add(Activation('relu'))
modelbatch.add(Dropout(0.5))

modelbatch.add(Dense(num_classes))
modelbatch.add(BatchNormalization())
modelbatch.add(Activation('softmax'))
# Compile model
modelbatch.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
# Train the model
start = time.time()
model_info = modelbatch.fit(X_2, y_2, batch_size=500, \
                         epochs=20, verbose=2, validation_data=(X_test, y_test))
end = time.time()

我认为,这是我模型的所有层次:

print(modelbatch.layers[0].get_weights()[0].shape)
(1120, 512)
print(modelbatch.layers[0].get_weights()[1].shape)
(512,)
print(modelbatch.layers[1].get_weights()[0].shape)
(512,)
print(modelbatch.layers[1].get_weights()[1].shape)
(512,)
print(modelbatch.layers[1].get_weights()[2].shape)
(512,)
print(modelbatch.layers[1].get_weights()[3].shape)
(512,)
print(modelbatch.layers[4].get_weights()[0].shape)
(512, 256)
print(modelbatch.layers[4].get_weights()[1].shape)
(256,)
print(modelbatch.layers[5].get_weights()[0].shape)
(256,)
print(modelbatch.layers[5].get_weights()[1].shape)
(256,)
print(modelbatch.layers[5].get_weights()[2].shape)
(256,)
print(modelbatch.layers[5].get_weights()[3].shape)
(256,)
print(modelbatch.layers[8].get_weights()[0].shape)
(256, 38)
print(modelbatch.layers[8].get_weights()[1].shape)
(38,)
print(modelbatch.layers[9].get_weights()[0].shape)
(38,)
print(modelbatch.layers[9].get_weights()[1].shape)
(38,)
print(modelbatch.layers[9].get_weights()[2].shape)
(38,)
print(modelbatch.layers[9].get_weights()[3].shape)
(38,)

我将非常感谢您的帮助,谢谢您。

1 个答案:

答案 0 :(得分:0)

让我们来看看你的模特:

您的输入图层的尺寸为1120,连接到那个,在您拥有批量规范化图层后,您的第一个隐藏图层包含512个神经元。之后你的激活功能,然后你的辍学层。请注意,您可以使用命令model.summary()来可视化模型

理论上,您可以(并且应该)将这些图层视为应用以下转换的一个图层:批量标准化,激活和丢失。在实践中,每个层都在Keras中单独实现,因为您获得了实现的模块化:不是编码所有可能的方式来设计层,用户可以选择添加到层批处理规范或丢失。要查看模块化实现,我建议您查看http://cs231n.stanford.edu/slides/2017/cs231n_2017_lecture4.pdf,一般情况下http://cs231n.stanford.edu/syllabus.html,如果您想获得更深入的知识。

对于批量标准化层,您可以注意到4个参数:两个可调参数:gamma和beta,以及两个由数据设置的参数(平均值和标准偏差)。要了解它是什么,请查看斯坦福大学课程,您也可以在原始论文中找到有关批量标准化https://arxiv.org/abs/1502.03167的内容。通过在每一层标准化数据来提高学习速度和提高准确性只是一种技巧,就像在输入数据的预处理步骤中那样。

根据我的说法,你可以推断你模型的其余部分。

N-B:我不会在softmax之前的最后一步使用batchnormalization层。

更清楚吗?