开始使用免费开始使用

编译一个顺序模型

在本练习中,您将继续对手语 MNIST 数据集中的字母进行分类;不过,这次将采用与上一练习不同的网络结构。层数更少,但节点更多。您还将应用 dropout 来防止过拟合。最后,您会编译模型,使用 adam 优化器和 categorical_crossentropy 损失函数。您还将使用 keras 中的方法来汇总模型结构。注意,keras 已从 tensorflow 为您导入,并且一个顺序式的 keras 模型已定义为 model

本练习是课程的一部分

Python 中的 TensorFlow 入门

查看课程

练习说明

  • 在第一个全连接层中,将节点数设为 16,激活函数设为 sigmoid,并将 input_shape 设为 (784,)。
  • 对第一层的输出以 25% 的比例应用 dropout。
  • 将输出层设为全连接层,包含 4 个节点,并使用 softmax 激活函数。
  • 使用 adam 优化器和 categorical_crossentropy 损失函数编译模型。

交互式实操练习

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

# Define the first dense layer
model.add(keras.layers.Dense(____, ____, ____))

# Apply dropout to the first layer's output
model.add(keras.layers.____(0.25))

# Define the output layer
____

# Compile the model
model.compile('____', loss='____')

# Print a model summary
print(model.summary())
编辑并运行代码