简体   繁体   中英

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?

You can simply use below code for getting weights and biases of those two dense layers:

for layer in model.layers:
    print(layer.name)
    w, b = layer.get_weights()
    print(w, b)

Code:

input_layer = Input(shape=(3,))
dense_layer_1 = Dense(units=10, activation='sigmoid', name='dense_layer_1')
dense_layer_1_output = dense_layer_1(input_layer)
dense_layer_2 = Dense(units=1, activation='linear',  name='dense_layer_2')
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)

for layer in model.layers[1:]:
    print(layer.name)
    w, b = layer.get_weights()
    print(w, b)

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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM