開始使用免費開始

Keras 中的序列式模型

在第 3 章中,我們在 tensorflow 中使用了 keras API 的部分元件來定義神經網路,但尚未充分運用其功能來簡化模型定義與訓練。在本練習中,你將使用 keras 的序列式模型 API 來定義一個可用於分類手語字母影像的神經網路。你也會使用 .summary() 方法列印模型的架構,包括每個層的張量形狀與參數數量。

請注意,影像已從 (28, 28) 重塑為 (784,),以便作為全連接層的輸入。另外,keras 已經從 tensorflow 匯入供你使用。

本練習屬於課程

Python 的 TensorFlow 入門

檢視課程

練習說明

  • 定義一個名為 modelkeras 序列式模型。
  • 將第一個層設為 Dense(),包含 16 個節點並使用 relu 啟用函式。
  • 將第二個層設為 Dense(),包含 8 個節點並使用 relu 啟用函式。
  • 將輸出層設為 4 個節點,並使用 softmax 作為啟用函式。

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

# Define a Keras sequential model
____

# Define the first dense layer
model.add(keras.layers.____(____, activation='____', input_shape=(784,)))

# Define the second dense layer
____

# Define the output layer
model.add(keras.layers.Dense(____))

# Print the model architecture
print(model.summary())
編輯並執行程式碼