开始使用免费开始使用

构建 CNN 模型

在 Keras 中构建 CNN 模型并不比您在本课程中已经构建的其他模型更难!您只需要使用卷积层。

您将构建一个浅层卷积 model 来对 MNIST 手写数字数据集进行分类。就是您之前用自编码器去噪的那个数据集!图像是 28 x 28 像素,并且由于是黑白图像,只有 1 个通道

现在就来动手构建这个小型卷积模型吧!

本练习是课程的一部分

Keras 深度学习入门

查看课程

练习说明

  • 导入 Conv2DFlatten 层并实例化您的模型。
  • 添加第一层卷积层,包含 32 个大小为 3x3 的滤波器,并设置相应的 3D 元组为 input_shape
  • 添加第二层卷积层,包含 16 个大小为 3x3 的滤波器,并使用 relu 激活函数。
  • 将上一层的输出进行展平,得到一维向量。

交互式实操练习

通过完成这段示例代码来试试这个练习。

# Import the Conv2D and Flatten layers and instantiate model
from tensorflow.keras.____ import ____,____
model = ____

# Add a convolutional layer of 32 filters of size 3x3
model.add(Conv2D(____, kernel_size = ____, input_shape = (____, ____, 1), activation = 'relu'))

# Add a convolutional layer of 16 filters of size 3x3
model.add(____(____, ____ = ____, activation = ____))

# Flatten the previous layer output
model.add(____)

# Add as many outputs as classes with softmax activation
model.add(Dense(10, activation = 'softmax'))
编辑并运行代码