全連接層的線性代數
在 tensorflow 中有兩種方式可以定義全連接(dense)層。第一種是使用低階、線性代數層級的運算;第二種是使用高階的 keras 操作。本練習將用第一種方法,來建構下圖所示的網路。
輸入層包含 3 個特徵──教育程度、婚姻狀況與年齡──已提供為 borrower_features。隱藏層有 2 個節點,輸出層有 1 個節點。
對於每一層,你會將前一層的輸出作為輸入,初始化一組權重,計算輸入與權重的乘積,然後套用啟用函式。請注意,Variable()、ones()、matmul(),以及 keras() 都已從 tensorflow 匯入。
本練習屬於課程
Python 的 TensorFlow 入門
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Initialize bias1
bias1 = Variable(1.0)
# Initialize weights1 as 3x2 variable of ones
weights1 = ____(ones((____, ____)))
# Perform matrix multiplication of borrower_features and weights1
product1 = ____
# Apply sigmoid activation function to product1 + bias1
dense1 = keras.activations.____(____ + ____)
# Print shape of dense1
print("\n dense1's output shape: {}".format(dense1.shape))