我已經定義了一個神經網路模型:
model = keras.models.Sequential([
keras.layers.Flatten(input_shape = (15,)), # the input layer
keras.layers.Dense(20, activation = 'relu'), #the hidden
keras.layers.Dense(20, activation = 'relu'), #the hidden
keras.layers.Dense(20, activation = 'relu'), #the hidden
keras.layers.Dense(20, activation = 'relu'), #the hidden
keras.layers.Dense(20, activation = 'relu'), #the hidden
keras.layers.Dense(2) #The output layer
])
這是模型的摘要:
Model: "sequential_7"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
flatten_7 (Flatten) (None, 15) 0
dense_40 (Dense) (None, 20) 320
dense_41 (Dense) (None, 20) 420
dense_42 (Dense) (None, 20) 420
dense_43 (Dense) (None, 20) 420
dense_44 (Dense) (None, 20) 420
dense_45 (Dense) (None, 2) 42
=================================================================
Total params: 2,042
Trainable params: 2,042
Non-trainable params: 0
訓練后,我希望得到以下形式的權重:
16X20, 21X20, .... 21X2 因為神經網路有 const 列。
但是當我實際測量 model.weights 的形狀時,我實際上得到了矩陣
在偶數列和奇數位置的向量:
mats = [np.array(w) for w in model.weights[0::2]]
vecs= [np.array(w) for w in model.weights[1::2]]
print([w.shape for w in mats])
print([w.shape for w in vecs])
[(15, 20), (20, 20), (20, 20), (20, 20), (20, 20), (20, 2)]
[(20,), (20,), (20,), (20,), (20,), (2,)]
我的問題是:這里的向量是否只是與每一層的 const 引數相對應的墊子的零線?
uj5u.com熱心網友回復:
Dense這里的向量是為每一層定義的偏差。嘗試輸出第一層的權重,Dense例如:
import tensorflow as tf
model = tf.keras.models.Sequential([
tf.keras.layers.Flatten(input_shape = (15,)), # the input layer
tf.keras.layers.Dense(20, activation = 'relu'), #the hidden
tf.keras.layers.Dense(20, activation = 'relu'), #the hidden
tf.keras.layers.Dense(20, activation = 'relu'), #the hidden
tf.keras.layers.Dense(20, activation = 'relu'), #the hidden
tf.keras.layers.Dense(20, activation = 'relu'), #the hidden
tf.keras.layers.Dense(2) #The output layer
])
print(model.layers[1].weights)
您應該看到內核權重和偏差。這對應于Dense層的描述:
Dense 實作操作: output = activation(dot(input, kernel) bias)
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/487682.html
