定义模型和损失函数
在本练习中,您将训练一个神经网络来预测信用卡持有人是否会违约。用于训练网络的特征和目标已在 Python shell 中提供,分别为 borrower_features 和 default。您已在上一个练习中定义了权重和偏置。
注意,predictions 层被定义为 $\sigma(layer1*w2+b2)\(,其中 \)\sigma$ 是 sigmoid 激活函数,layer1 是第一层全连接隐藏层的张量节点,w2 是权重张量,b2 是偏置张量。
可训练变量为 w1、b1、w2 和 b2。另外,以下操作已为您导入:keras.activations.relu() 和 keras.layers.Dropout()。
本练习是课程的一部分
Python 中的 TensorFlow 入门
练习说明
- 对第一层应用修正线性单元激活函数。
- 对
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(____, ____)