始める無料で始める

重みを複数回更新する

ここでは、重みを何度も更新してモデルを大きく改善し、各更新ごとに予測がどのように良くなるかを確認します。

コードを見やすくするために、input_datatargetweights を引数に取る事前読み込み済みの get_slope() 関数を用意しています。同じ引数を取る get_mse() 関数もあります。input_datatargetweights はすでに読み込まれています。

このネットワークには隠れ層がなく、入力(3 ノード)から出力ノードへ直接つながっています。weights は 1 つの配列である点に注意してください。

また、matplotlib.pyplot も読み込まれており、あなたが勾配降下のステップを実行した後に誤差の履歴がプロットされます。

この演習はコースの一部です

Pythonで学ぶDeep Learning入門

コースを見る

演習の手順

  • for ループを使って重みを反復的に更新します:
    • get_slope() 関数で傾きを計算します。
    • 学習率 0.01 で重みを更新します。
    • 更新後の重みで get_mse() 関数を使って平均二乗誤差(mse)を計算します。
    • msemse_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()
コードを編集して実行