Search code examples
pythonkeraskeras-layer

How to get the values of weights and biases of keras layer?


To make my question more clear, here I wrote a piece of code:

from keras.layers import Input, Dense
from keras.models import Model
import numpy as np

features = np.random.normal(0, 1, (1000, 3))
labels = np.sum(features, axis=1)
print(features.shape, labels.shape)

input_layer = Input(shape=(3,))
dense_layer_1 = Dense(units=10, activation='sigmoid')
dense_layer_1_output = dense_layer_1(input_layer)
dense_layer_2 = Dense(units=1, activation='linear')
dense_layer_2_output = dense_layer_2(dense_layer_1_output)

model = Model(input_layer, dense_layer_2_output)
model.compile(optimizer='adam', loss='mean_squared_error')
model.fit(features, labels, batch_size=32, epochs=20, verbose=2, validation_split=.2)

My question is: how to fetch and print the value of weights and biases of these two Dense layers?


Solution

  • As mentioned here

    If you want to get weights and biases of all layers, you can simply use:

    for layer in model.layers: print(layer.get_config(), layer.get_weights())
    

    If you want the weights directly returned as numpy arrays, you can use:

    first_layer_weights = model.layers[0].get_weights()[0]
    first_layer_biases  = model.layers[0].get_weights()[1]
    second_layer_weights = model.layers[1].get_weights()[0]
    second_layer_biases  = model.layers[1].get_weights()[1]