モデルの重みを改善する
やりました! 必要な傾きが計算できました。次は、その傾きを使ってモデルを改善します。傾きを重みに加えると、正しい方向に進めます。ただし、その方向に行き過ぎる可能性もあります。そこで、最初は小さなステップ(低めの学習率)で動かし、モデルが改善しているかを確認しましょう。
重みは weights、ターゲットの実際の値は target、入力データは input_data として事前に読み込まれています。初期の重みによる予測は preds に保存されています。
この演習はコースの一部です
Pythonで学ぶDeep Learning入門
演習の手順
- 学習率を
0.01に設定し、元の予測からの誤差を計算します。これはすでに用意されています。 weightsからlearning_rateとslopeの積を引いて、更新後の重みを計算します。weights_updatedとinput_dataを要素ごとに掛けて合計を取り、更新後の予測を計算します。- 新しい予測の誤差を計算し、
error_updatedに保存します。 - 'Submit Answer' を押して、更新後の誤差と元の誤差を比較しましょう!
実践的なインタラクティブ演習
このサンプルコードを完成させて、この演習に挑戦してみましょう。
# Set the learning rate: learning_rate
learning_rate = 0.01
# Calculate the predictions: preds
preds = (weights * input_data).sum()
# Calculate the error: error
error = preds - target
# Calculate the slope: slope
slope = 2 * input_data * error
# Update the weights: weights_updated
weights_updated = ____
# Get updated predictions: preds_updated
preds_updated = ____
# Calculate updated error: error_updated
error_updated = ____
# Print the original error
print(error)
# Print the updated error
print(error_updated)