計算斜率
現在你要練習計算斜率。當你把均方誤差(mean-squared error)損失函式對預測值作圖時,斜率是 2 * x * (xb-y),也就是 2 * input_data * error。請注意,x 與 b 可能包含多個數值(每個資料點的 x 是向量,而 b 也是向量)。在這種情況下,輸出也會是向量,這正是你要的。
你已經可以撰寫使用單一資料點來計算此斜率的程式碼。你會使用預先定義的權重 weights,以及單一資料點的資料 input_data。你想要預測的實際目標值則存放在 target 中。
本練習屬於課程
Python 深度學習入門
練習說明
- 計算預測值
preds:將weights與input_data相乘並取其總和。 - 計算誤差,亦即
preds減去target。注意這個誤差對應到梯度式中的xb-y。 - 計算損失函式對預測值的斜率。為此,先將
input_data與error相乘,再乘上2。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Calculate the predictions: preds
preds = ____
# Calculate the error: error
error = ____ - ____
# Calculate the slope: slope
slope = ____ * ____ * ____
# Print the slope
print(slope)