[英]How to derive the output from Keras model using its layers' weight and biases?
我的 model 試圖預測 y = 2x + 5 之后的線性回歸中的值。因此,我的訓練數據類似於以下內容:
x_train = [0, 1, 2, 3, 4, ...] and y_train = [5, 7, 9, 11, 13, ...]
我的 Keras model 看起來像這樣:
`model = tf.keras.Sequential([
tf.keras.layers.Dense(16, activation='relu', input_dim=1),
tf.keras.layers.Dense(1, activation='linear')
])
model.compile(loss='mean_squared_error', optimizer='adam', metrics=['mse'])
model.fit(x_train, y_train, epochs=1000, batch_size=10)`
在獲得良好的驗證准確度后,我希望查看模型的權重和偏差,以計算給定輸入的 output,因為我希望了解簡單的神經網絡是如何工作的。 我在跑步時達到以下權重和偏差
for l in model.layers: print(l.get_weights())
[array([[-0.10382611, 0.48899287, -0.36912352, -0.11604425, 0.03658517,
0.546786 , -0.0094437 , 0.5393126 , -0.36325318, -0.20389882,
-0.00112574, -0.39811927, -0.25433052, -0.16315842, 0.6172162 ,
-0.47300738]], dtype=float32), array([ 0. , 1.1705374 ,
0. , 0. , -0.41323203, 0.97515434, 0. ,
0.99699414, 0. , 0. ,-0.2316811 , 0. ,
0. , 0. , 1.4638424 , 0. ], dtype=float32)]
[array([[-0.30404267],
[ 0.91265625],
[ 0.3578334 ],
[-0.23462006],
[-0.33843294],
[ 1.080244 ],
[-0.5933689 ],
[ 1.0348322 ],
[ 0.47716653],
[ 0.18852347],
[-0.21219982],
[ 0.45529807],
[ 0.39576346],
[-0.05013525],
[ 0.67550814],
[-0.19761673]], dtype=float32), array([0.7426254], dtype=float32)]
我的印象是,如果我輸入 10 的值,我應該期望值 25 作為 output(或非常接近)。 然而,當我嘗試自己做數學時,我並沒有那么接近。 我目前對這應該如何工作的理解是:
我不明白這應該如何工作?
如果我們觀察每一層的權重和偏差數組,我們可以看到錯誤的結果是由於缺乏訓練造成的。 Model 無法使用這些參數學習模式,但我們可以說它正在改進,因為權重和偏差被認為在增加,這是必需的,因為我們已經有模式背后的邏輯。 如果您的訓練數據很小,我建議您增加迭代次數並更改批量大小。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.