学习识别数字
您将基于 digits 数据集 构建一个模型。该数据集是 scikit-learn 预置的示例数据。digits 数据集 由 8x8 像素的手写数字(0 到 9) 组成:
数据集已被划分为 X_train、y_train、X_test 和 y_test,其中 30% 的数据用作测试集。标签已是一键编码向量,因此无需使用 Keras 的 to_categorical() 函数。
现在来构建这个新的 model 吧!
本练习是课程的一部分
Keras 深度学习入门
练习说明
- 添加一个包含 16 个神经元、
relu激活的Dense层,并将 8x8 数字图像的总像素数 作为input_shape。 - 再添加一个具有 10 个输出、
softmax激活的Dense层。 - 使用
adam、categorical_crossentropy和accuracy指标编译模型。 - 在
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(____))