[英]How to get weights from keras model?
I'm trying to build a 2 layered neural network for MNIST dataset and I want to get weights from my model.我正在尝试为 MNIST 数据集构建一个 2 层神经网络,我想从我的模型中获取权重。
I found a similar question her on SO and I tried this,我在 SO 上发现了一个类似的问题,我试过了,
model.get_weights()
But It returned 11 values when I check the len(model.get_weights())
Isn't it suppose to return 3 weights?但是当我检查len(model.get_weights())
时它返回了 11 个值是不是应该返回 3 个权重? I have even disabled bias.我什至禁用了偏见。
model = Sequential()
model.add(Flatten(input_shape = (28, 28)))
model.add(Dense(512, activation='relu', kernel_initializer='he_normal', use_bias=False,))
model.add(BatchNormalization())
model.add(Dropout(0.3))
model.add(Dense(128, activation='relu', kernel_initializer='he_normal', use_bias=False,))
model.add(BatchNormalization())
model.add(Dropout(0.1))
model.add(Dense(10, activation='relu', kernel_initializer='he_normal', use_bias=False,))
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
result = model.fit(x_train, y_train, validation_split=0.25, epochs=10,
batch_size=128, verbose=1)
To get the weights of a particular layer , you could retrieve this layer by using its name and call get_weights
on it (as shubham-panchal said in its comment ).要获取特定层的权重,您可以使用其名称检索该层并对其调用get_weights
(如shubham-panchal在其评论中所说)。
For example:例如:
model.get_layer('dense').get_weights()
or或者
model.get_layer('dense_2').get_weights()
You could go though the layers
of your model and retrieve its name and weights:您可以遍历模型的各个layers
并检索其名称和权重:
{layer.name: layer.get_weights() for layer in model.layers}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.