重みを複数回更新する
ここでは、重みを何度も更新してモデルを大きく改善し、各更新ごとに予測がどのように良くなるかを確認します。
コードを見やすくするために、input_data、target、weights を引数に取る事前読み込み済みの get_slope() 関数を用意しています。同じ引数を取る get_mse() 関数もあります。input_data、target、weights はすでに読み込まれています。
このネットワークには隠れ層がなく、入力(3 ノード)から出力ノードへ直接つながっています。weights は 1 つの配列である点に注意してください。
また、matplotlib.pyplot も読み込まれており、あなたが勾配降下のステップを実行した後に誤差の履歴がプロットされます。
この演習はコースの一部です
Pythonで学ぶDeep Learning入門
演習の手順
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()