使用 dense 層運算
我們已經看過如何在 tensorflow 中用線性代數來定義 dense 層。在本練習中,我們將略過線性代數的細節,交給 keras 自動處理。這讓我們能用比之前「1 個隱藏層、3 個特徵」的網路更少的程式碼,建出下圖這個「2 個隱藏層、10 個特徵」的網路。

要建構這個網路,我們需要定義三個 dense 層。每一層都以前一層為輸入,與權重相乘,並套用啟用函式。請注意,輸入資料已定義為一個 100x10 的張量:borrower_features。另外,keras.layers 模組可供使用。
本練習屬於課程
Python 的 TensorFlow 入門
練習說明
- 將
dense1設為具有 7 個輸出節點且啟用函式為 sigmoid 的 dense 層。 - 將
dense2定義為具有 3 個輸出節點且啟用函式為 sigmoid 的 dense 層。 - 將
predictions定義為具有 1 個輸出節點且啟用函式為 sigmoid 的 dense 層。 - 依序列印
dense1、dense2、predictions的形狀,使用.shape方法。為什麼這些張量各有 100 列?
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Define the first dense layer
dense1 = keras.layers.Dense(____, activation='____')(borrower_features)
# Define a dense layer with 3 output nodes
dense2 = ____
# Define a dense layer with 1 output node
predictions = ____
# Print the shapes of dense1, dense2, and predictions
print('\n shape of dense1: ', dense1.shape)
print('\n shape of dense2: ', ____.shape)
print('\n shape of predictions: ', ____.shape)