How to get the weights of Keras model?

I've build a simple model of Keras 2.2 and want to get the weights of it. How can I get all weights of the model as keras tensors?

3 votesLP199.00
1 Answers

Keras has implemented some functions for getting or setting weights for every layer. 

  • layer.get_weights(): returns the weights of the layer as a list of Numpy arrays.
  • layer.set_weights(weights): sets the weights of the layer from a list of Numpy arrays.

Using these functions you can write a piece of code to get all layers' weights

for layer in model.layers:
    weights = layer.get_weights() # list of numpy arrays

Or you can get the weights right from the model

from keras.models import Sequential

model = Sequential()
# ...
weights = model.get_weights() # returs a numpy list of weights

Keras model also has get_weights() and set_weights(weights) functions like every layer has.

If you need more take a look at this keras doc.

Couldn't find what you were looking for?and we will find an expert to answer.
How helpful was this page?