本代码介绍了迁移学习,也就是在一个数据集上训练完成后,在另一个数据集上,部分网络层配置为不可训练,仅对部分网络进行训练
本代码的神经网络结构为:
______________________________________________________________________
Layer (type) Output Shape Param #
======================================================================
conv2d_1 (Conv2D) (None, 26, 26, 32) 320
______________________________________________________________________
activation_1 (Activation) (None, 26, 26, 32) 0
______________________________________________________________________
conv2d_2 (Conv2D) (None, 24, 24, 32) 9248
______________________________________________________________________
activation_2 (Activation) (None, 24, 24, 32) 0
______________________________________________________________________
max_pooling2d_1 (MaxPooling2D) (None, 12, 12, 32) 0
______________________________________________________________________
dropout_1 (Dropout) (None, 12, 12, 32) 0
______________________________________________________________________
flatten_1 (Flatten) (None, 4608) 0
______________________________________________________________________
dense_1 (Dense) (None, 128) 589952
______________________________________________________________________
activation_3 (Activation) (None, 128) 0
______________________________________________________________________
dropout_2 (Dropout) (None, 128) 0
______________________________________________________________________
dense_2 (Dense) (None, 5) 645
______________________________________________________________________
activation_4 (Activation) (None, 5) 0
======================================================================
Total params: 600,165
Trainable params: 600,165
Non-trainable params: 0
______________________________________________________________________
代码是先训练mnist中的 0~4 的数据集,对其进行识别,训练出一个常规的卷积神经网络的分类,
然后将该神经网络中的卷积层的可训练属性配置为FALSE,也就是不可训练,然后采用 5~9 的数据集进行训练,也就是只训练全连接层,然后发现效果不错,训练速度可以大大提升
比如在我电脑上,6G显存,在训练 0~4 的数据集时,大致训练了 11 秒,而在训练 5~9 时,虽然数据集大小一样,训练次数一致,但仅用了大概 5 秒的时间,而且识别率可以达到 99% 左右