使用 TensorFlow 训练神经网络
在上一个练习中,您定义了模型 model(w1, b1, w2, b2, features) 和损失函数 loss_function(w1, b1, w2, b2, features, targets),这两个对象在本练习中均可用。现在您将训练该模型,并在测试集上评估其性能,测试集由 test_features 和 test_targets 组成,已为您提供。可训练变量为 w1、b1、w2 和 b2。此外,以下操作也已为您导入:keras.activations.relu() 和 keras.layers.Dropout()。
本练习是课程的一部分
Python 中的 TensorFlow 入门
练习说明
- 将优化器设置为执行最小化。
- 按它们在
loss_function()的参数中出现的顺序,把四个可训练变量添加到var_list。 - 使用模型和
test_features来预测test_targets的取值。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Train the model
for j in range(100):
# Complete the optimizer
opt.____(lambda: loss_function(w1, b1, w2, b2),
var_list=[____, ____, ____, ____])
# Make predictions with model using test features
model_predictions = model(w1, b1, w2, b2, ____)
# Construct the confusion matrix
confusion_matrix(test_targets, model_predictions)