開始使用免費開始

用矩陣乘法進行預測

在後續章節,你會學到如何訓練線性迴歸模型。這個過程會得到一個參數向量,將它與輸入資料相乘即可產生預測。在本練習中,你會使用輸入資料 features 和目標向量 bill,它們來自本課程稍後會用到的信用卡資料集。

\(features = \begin{bmatrix} 2 & 24 \\ 2 & 26 \\ 2 & 57 \\ 1 & 37 \end{bmatrix}\), \(bill = \begin{bmatrix} 3913 \\ 2682 \\ 8617 \\ 64400 \end{bmatrix}\), \(params = \begin{bmatrix} 1000 \\ 150 \end{bmatrix}\)

輸入資料矩陣 features 有兩個欄位:教育程度與年齡。目標向量 bill 表示信用卡借款人的帳單金額大小。

由於我們尚未訓練模型,你會先替參數向量 params 輸入一組猜測值。接著使用 matmul(),將 featuresparams 做矩陣乘法,產生預測值 billpred,並與 bill 比較。注意我們已匯入 matmul()constant()

本練習屬於課程

Python 的 TensorFlow 入門

檢視課程

練習說明

  • featuresparamsbill 定義為常數。
  • 以矩陣乘法計算預測向量 billpred:用輸入資料 features 乘上參數 params。務必使用矩陣乘法,而非元素相乘。
  • error 定義為目標 bill 減去預測值 billpred

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

# Define features, params, and bill as constants
features = ____([[2, 24], [2, 26], [2, 57], [1, 37]])
params = ____([[1000], [150]])
bill = ____([[3913], [2682], [8617], [64400]])

# Compute billpred using features and params
billpred = ____

# Compute and print the error
error = ____ - ____
print(error.numpy())
編輯並執行程式碼