開始使用免費開始

定義模型與損失函式

在這個練習中,你將訓練一個神經網路,用來預測信用卡持有人是否會違約。你要用來訓練網路的特徵與目標,已在 Python 互動環境中提供為 borrower_featuresdefault。你已在前一個練習中定義好權重與偏差。

請注意,predictions 層被定義為 $\sigma(layer1*w2+b2)\(,其中 \)\sigma$ 是 sigmoid 啟用函式,layer1 是第一個隱藏全連接層的節點張量,w2 是權重張量,b2 是偏差張量。

可訓練變數為 w1b1w2b2。此外,以下操作已為你匯入:keras.activations.relu()keras.layers.Dropout()

本練習屬於課程

Python 的 TensorFlow 入門

檢視課程

練習說明

  • 對第一層套用修正線性單元(ReLU)啟用函式。
  • layer1 套用 25% 的 dropout。
  • 將目標 targets 與預測值 predictions 傳入交叉熵損失函式。

動手互動練習

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

# Define the model
def model(w1, b1, w2, b2, features = borrower_features):
	# Apply relu activation functions to layer 1
	layer1 = keras.activations.____(matmul(features, w1) + b1)
    # Apply dropout rate of 0.25
	dropout = keras.layers.Dropout(____)(____)
	return keras.activations.sigmoid(matmul(dropout, w2) + b2)

# Define the loss function
def loss_function(w1, b1, w2, b2, features = borrower_features, targets = default):
	predictions = model(w1, b1, w2, b2)
	# Pass targets and predictions to the cross entropy loss
	return keras.losses.binary_crossentropy(____, ____)
編輯並執行程式碼