《Python 深度学习》刷书笔记 Chapter 5 Part-1 搭建一个简单的卷积神经网络

文章目录

  • 5-1, 5-2 实例化一个小型的卷积神经网络
  • 5-3 在MNIST图像上训练卷积神经网络
    • 结果对比
    • 卷积神经网络
    • 卷积层的构建
  • 写在最后


5-1, 5-2 实例化一个小型的卷积神经网络


在这一小节中,我们继续使用MNIST手写识别数据库对一个小型的卷积神经网络进行分析研究

from keras import layers
from keras import models

model = models.Sequential()

# 卷积层
# 64 或是 32 表示通道数量
model.add(layers.Conv2D(32, (3, 3), activation = 'relu', input_shape = (28, 28, 1)))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation = 'relu'))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation = 'relu'))

# 卷积层上加上分类器
model.add(layers.Flatten())
model.add(layers.Dense(64, activation = 'relu'))
model.add(layers.Dense(10, activation = 'softmax'))

# 模型一览
print(model.summary())
Model: "sequential_2"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d_4 (Conv2D)            (None, 26, 26, 32)        320       
_________________________________________________________________
max_pooling2d_3 (MaxPooling2 (None, 13, 13, 32)        0         
_________________________________________________________________
conv2d_5 (Conv2D)            (None, 11, 11, 64)        18496     
_________________________________________________________________
max_pooling2d_4 (MaxPooling2 (None, 5, 5, 64)          0         
_________________________________________________________________
conv2d_6 (Conv2D)            (None, 3, 3, 64)          36928     
_________________________________________________________________
flatten_2 (Flatten)          (None, 576)               0         
_________________________________________________________________
dense_3 (Dense)              (None, 64)                36928     
_________________________________________________________________
dense_4 (Dense)              (None, 10)                650       
=================================================================
Total params: 93,322
Trainable params: 93,322
Non-trainable params: 0
_________________________________________________________________
None

5-3 在MNIST图像上训练卷积神经网络


from keras.datasets import mnist
from keras.utils import to_categorical

# 载入数据
(train_images, train_labels), (test_images, test_labels) = mnist.load_data()

# 数据预处理
train_images = train_images.reshape((60000, 28, 28, 1))
train_images = train_images.astype('float32') / 255

test_images = test_images.reshape((10000, 28, 28, 1))
test_images = test_images.astype('float32') / 255

train_labels = to_categorical(train_labels)
test_labels = to_categorical(test_labels)

# 构建优化器
model.compile(optimizer = 'rmsprop', loss = 'categorical_crossentropy',
              metrics = ['accuracy'])

# 训练模型
model.fit(train_images, train_labels, epochs = 5, batch_size = 64)

# 打印训练结果
test_loss, test_acc = model.evaluate(test_images, test_labels)
print(test_acc)
Epoch 1/5
60000/60000 [==============================] - 24s 392us/step - loss: 0.1735 - accuracy: 0.9456
Epoch 2/5
60000/60000 [==============================] - 21s 358us/step - loss: 0.0484 - accuracy: 0.9851
Epoch 3/5
60000/60000 [==============================] - 21s 358us/step - loss: 0.0324 - accuracy: 0.9893
Epoch 4/5
60000/60000 [==============================] - 22s 368us/step - loss: 0.0251 - accuracy: 0.9921
Epoch 5/5
60000/60000 [==============================] - 22s 368us/step - loss: 0.0198 - accuracy: 0.9938
10000/10000 [==============================] - 1s 100us/step
0.9911999702453613

结果对比


对比我们在第二章的结果显示,在同样训练5轮的情况下,在第二章的训练中test_acc: 0.9797000288963318,而卷积层的结果为0.9911999702453613,我们在卷积层所使用到的特征窗口均为3 * 3

卷积神经网络


卷积神经网络具有以下几种性质

  • 平移不变性:在别处学习到的知识在全图像位置都通用
  • 更容易学到模式的空间层次结构

卷积层的构建


以下面这段代码为例,卷积层主要由以下几个参数定义

model.add(layers.Conv2D(32, (3, 3), activation = 'relu', input_shape = (28, 28, 1)))
  1. 从输入中提取的图块尺寸(小窗格大小),一般为3 * 3 或者 5 * 5
  2. 输出的特征图深度,卷积所计算的过滤器的数量,一般为32或者64

写在最后

注:本文代码来自《Python 深度学习》,做成电子笔记的方式上传,仅供学习参考,作者均已运行成功,如有遗漏请练习本文作者

各位看官,都看到这里了,麻烦动动手指头给博主来个点赞8,您的支持作者最大的创作动力哟!
<(^-^)>
才疏学浅,若有纰漏,恳请斧正
本文章仅用于各位同志作为学习交流之用,不作任何商业用途,若涉及版权问题请速与作者联系,望悉知

你可能感兴趣的:(#,《Python,深度学习》,刷书笔记,Python之路,神经网络,python,深度学习,卷积神经网络)