編譯 sequential 模型
在這個練習中,你將嘗試對 Sign Language MNIST 資料集的字母進行分類;不過,會採用與上一個練習不同的網路架構。層數會較少,但每層的節點數更多。你也會加入 dropout 來防止過度擬合。最後,你會編譯模型,使用 adam 最佳化器與 categorical_crossentropy 損失。你還會使用 keras 中的方法來摘要模型的架構。注意,keras 已經從 tensorflow 匯入,並且已為你定義好一個 sequential 的 keras 模型為 model。
本練習屬於課程
Python 的 TensorFlow 入門
練習說明
- 在第一個 dense 層中,將節點數設為 16,啟用函式設為
sigmoid,input_shape設為 (784,)。 - 以 25% 的比率對第一層的輸出套用 dropout。
- 將輸出層設為 dense、節點數為 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())