準備進行批次訓練
在以批次方式訓練線性模型之前,必須先定義變數、損失函式,以及最佳化操作。在本練習中,你將準備訓練一個模型,使用 size_batch(以平方英尺為單位的一批地坪面積)來預測 price_batch(一批房價)。與前一個單元不同的是,我們會使用 pandas 載入批次資料,將其轉為 numpy 陣列,然後分步使用它來最小化損失函式。
Variable()、keras(),以及 float32 已為你匯入。請注意,不要在模型或損失函式中設定預設引數值,因為我們會在訓練過程中以批次方式產生資料。
本練習屬於課程
Python 的 TensorFlow 入門
練習說明
- 定義
intercept,初始值為 10.0,資料型別為 32 位元浮點數。 - 定義模型,使用
intercept、slope和features回傳預測值。 - 定義一個名為
loss_function()的函式,依序以intercept、slope、targets、features作為引數。不要設定任何預設引數值。 - 使用
targets與predictions定義均方誤差損失函式。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Define the intercept and slope
intercept = ___
slope = Variable(0.5, float32)
# Define the model
def linear_regression(intercept, slope, features):
# Define the predicted values
return ____
# Define the loss function
def ____:
# Define the predicted values
predictions = linear_regression(____, ____, features)
# Define the MSE loss
return keras.losses.____(____, ____)