对权重进行多次更新
现在,您将进行多次更新,以显著改进模型权重,并观察每次更新后预测如何提升。
为保持代码整洁,已预先加载 get_slope() 函数,该函数以 input_data、target 和 weights 作为参数。还提供了同样参数的 get_mse() 函数。input_data、target 和 weights 都已预加载。
该网络没有任何隐藏层,它直接从输入(包含 3 个节点)连接到一个输出节点。请注意,weights 是一个单独的数组。
我们也预先加载了 matplotlib.pyplot,在您完成梯度下降步骤后,将绘制误差历史。
本练习是课程的一部分
Python 深度学习入门
练习说明
- 使用
for循环迭代更新权重:- 使用
get_slope()函数计算斜率。 - 使用学习率
0.01更新权重。 - 使用
get_mse()函数在更新后的权重上计算均方误差(mse)。 - 将
mse追加到mse_hist。
- 使用
- 点击 "Submit Answer" 可可视化
mse_hist。您观察到什么趋势?
交互式实操练习
通过完成这段示例代码来试试这个练习。
n_updates = 20
mse_hist = []
# Iterate over the number of updates
for i in range(n_updates):
# Calculate the slope: slope
slope = ____(____, ____, ____)
# Update the weights: weights
weights = ____ - ____ * ____
# Calculate mse with new weights: mse
mse = ____(____, ____, ____)
# Append the mse to mse_hist
____
# Plot the mse history
plt.plot(mse_hist)
plt.xlabel('Iterations')
plt.ylabel('Mean Squared Error')
plt.show()