開始使用免費開始

對權重進行多次更新

你現在要進行多次更新,藉此大幅改善模型的權重,並觀察每次更新後預測如何進步。

為了讓程式碼更乾淨,已預先載入 get_slope() 函式,它接受 input_datatargetweights 作為引數。另外也有 get_mse() 函式,接受相同的引數。input_datatargetweights 都已預先載入。

這個網路沒有任何隱藏層,會直接從輸入(含 3 個節點)連到輸出節點。請注意,weights 是單一陣列。

我們也已預先載入 matplotlib.pyplot,在你完成梯度下降步驟後,會將錯誤歷程繪圖顯示。

本練習屬於課程

Python 深度學習入門

檢視課程

練習說明

  • 使用 for 迴圈反覆更新權重:
    • 使用 get_slope() 函式計算斜率。
    • 使用學習率 0.01 更新權重。
    • 使用 get_mse() 函式,以更新後的權重計算均方誤差(mse)。
    • mse 加入 mse_hist
  • 按下「送出答案」以視覺化 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()
編輯並執行程式碼