開始使用免費開始

編譯模型

現在你要來編譯先前建立的模型。要編譯模型,你需要指定要使用的最佳化器和損失函式。在影片中,Dan 提到 Adam 最佳化器是很不錯的選擇。你可以在這裡閱讀關於它與其他 Keras 最佳化器的更多資訊;如果你想更深入了解,也可以閱讀介紹 Adam 最佳化器的原始論文

在這個練習中,你會使用 Adam 最佳化器與均方誤差(mean squared error)損失函式。開始動手做吧!

本練習屬於課程

Python 深度學習入門

檢視課程

練習說明

  • 使用 model.compile() 編譯模型。optimizer 設為 'adam'loss 設為 'mean_squared_error'

動手互動練習

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

# Import necessary modules
from tensorflow.keras.layers import Dense
from tensorflow.keras.models import Sequential

# Specify the model
n_cols = predictors.shape[1]
model = Sequential()
model.add(Dense(50, activation='relu', input_shape = (n_cols,)))
model.add(Dense(32, activation='relu'))
model.add(Dense(1))

# Compile the model
____

# Verify that model contains information from compiling
print("Loss function: " + model.loss)
編輯並執行程式碼