多筆範例的低階作法
在這個練習中,你會透過建構多筆範例情境下的第一個稠密隱藏層,進一步培養對低階作法的直覺。我們假設模型已經訓練完成,且第一層的權重 weights1 與偏差 bias1 都已可用。接著,我們會將 borrower_features 張量與變數 weights1 做矩陣相乘。回想一下,borrower_features 張量包含教育程度、婚姻狀態與年齡。最後,我們會對 products1 + bias1 的各元素套用 sigmoid 函式,得到 dense1。
\(products1 = \begin{bmatrix} 3 & 3 & 23 \\ 2 & 1 & 24 \\ 1 & 1 & 49 \\ 1 & 1 & 49 \\ 2 & 1 & 29 \end{bmatrix} \begin{bmatrix} -0.6 & 0.6 \\ 0.8 & -0.3 \\ -0.09 & -0.08 \end{bmatrix}\)
注意,matmul() 與 keras() 已從 tensorflow 匯入。
本練習屬於課程
Python 的 TensorFlow 入門
練習說明
- 以特徵張量與權重做矩陣相乘來計算
products1。 - 使用 sigmoid 啟用函式轉換
products1 + bias1。 - 列印
borrower_features、weights1、bias1與dense1的形狀。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Compute the product of borrower_features and weights1
products1 = ____
# Apply a sigmoid activation function to products1 + bias1
dense1 = ____
# Print the shapes of borrower_features, weights1, bias1, and dense1
print('\n shape of borrower_features: ', borrower_features.shape)
print('\n shape of weights1: ', ____.shape)
print('\n shape of bias1: ', ____.shape)
print('\n shape of dense1: ', ____.shape)