简体   繁体   中英

How can I get biases from a trained model in Keras?

I have built a simple neural network,

model = Sequential()
model.add(Dense(20, input_dim=5, activation='sigmoid'))
model.add(Dense(1, activation='sigmoid'))

and I would get its weights by:

summary = model.summary()
W_Input_Hidden = model.layers[0].get_weights()[0]
W_Output_Hidden = model.layers[1].get_weights()[0]

print(summary)
print('INPUT-HIDDEN LAYER WEIGHTS:')
print(W_Input_Hidden)
print('HIDDEN-OUTPUT LAYER WEIGHTS:')
print(W_Output_Hidden)

but, in this way, I only get the weights matrices (5x20 , 1x20) without the biases. How can I get the biases values?

Quite simple, its just the second element in the array returned by get_weights() (For Dense layers):

B_Input_Hidden = model.layers[0].get_weights()[1]
B_Output_Hidden = model.layers[1].get_weights()[1]

Here's a complete working example (implemented with TensorFlow 2 and Keras).

import tensorflow as tf
import numpy as np


def get_model():
    inp = tf.keras.layers.Input(shape=(1,))
    # Use the parameter bias_initializer='random_uniform'
    # in case you want the initial biases different than zero.
    x = tf.keras.layers.Dense(8)(inp)
    out = tf.keras.layers.Dense(1)(x)
    model = tf.keras.models.Model(inputs=inp, outputs=out)
    return model


def main():
    model = get_model()
    model.compile(loss="mse")

    weights = model.layers[1].get_weights()[0]
    biases = model.layers[1].get_weights()[1]

    print("initial weights =", weights)
    print("initial biases =", biases)

    X = np.random.randint(-10, 11, size=(1000, 1))
    y = np.random.randint(0, 2, size=(1000, 1))

    model.fit(X, y)

    weights = model.layers[1].get_weights()[0]
    biases = model.layers[1].get_weights()[1]

    print("learned weights =", weights)

    # Biases are similar because they are all initialized with zeros (by default).
    print("learned biases =", biases)


if __name__ == '__main__':
    main()

YOu can view and output biases and weights using the following code:

for layer in model.layers:
    g=layer.get_config()
    h=layer.get_weights()
    print (g)
    print (h)

if you're looking for weights and bias from the validation dataset, you need to do model.predict on each vector from the dataset.

   for i in range(len(valData)):
        ValResults = model.predict(valData[i])
        B_Input_Hidden = model.layers[0].get_weights()[1]
        B_Output_Hidden = 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