開始使用免費開始

改進模型權重

太好了!你剛算出了所需的斜率。現在要用這些斜率來改進模型。若把斜率加到權重上,方向是對的;不過,有可能走得太遠。所以你應該先用較小的學習率,在該方向上小步前進,並確認模型是否在變好。

權重已預先載入為 weights,目標的實際值為 target,輸入資料為 input_data。以初始權重得到的預測已存成 preds

本練習屬於課程

Python 深度學習入門

檢視課程

練習說明

  • 將學習率設為 0.01,並計算原始預測的誤差。這已替你完成。
  • 透過從 weights 減去 learning_rateslope 的乘積,計算更新後的權重。
  • weights_updatedinput_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)
編輯並執行程式碼