开始使用免费开始使用

学习识别数字

您将基于 digits 数据集 构建一个模型。该数据集是 scikit-learn 预置的示例数据。digits 数据集8x8 像素的手写数字(0 到 9) 组成:

目标是根据图像区分 10 个可能的数字,因此这是一个多类别分类问题。

数据集已被划分为 X_trainy_trainX_testy_test,其中 30% 的数据用作测试集。标签已是一键编码向量,因此无需使用 Keras 的 to_categorical() 函数。

现在来构建这个新的 model 吧!

本练习是课程的一部分

Keras 深度学习入门

查看课程

练习说明

  • 添加一个包含 16 个神经元、relu 激活的 Dense 层,并将 8x8 数字图像的总像素数 作为 input_shape
  • 再添加一个具有 10 个输出、softmax 激活的 Dense 层。
  • 使用 adamcategorical_crossentropyaccuracy 指标编译模型。
  • X_train 上进行预测,确保模型可以正常工作。

交互式实操练习

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

# Instantiate a Sequential model
model = Sequential()

# Input and hidden layer with input_shape, 16 neurons, and relu 
model.add(Dense(____, input_shape = (____,), activation = ____))

# Output layer with 10 neurons (one per digit) and softmax
model.____(____)

# Compile your model
model.____(optimizer = ____, loss = ____, metrics = [____])

# Test if your model is well assembled by predicting before training
print(model.predict(____))
编辑并运行代码