Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get weights and biases from my model?

Tags:

python

keras

I have a simple neural network, I need to get weights and biases from the model. I have tried a few approaches discussed before but I keep getting the out of bounds value error. Not sure how to fix this, or what I'm missing.

Network-

model = keras.Sequential([
    keras.layers.Flatten(input_shape=(28, 28)),
    keras.layers.Dense(128, activation=tf.nn.relu),
    keras.layers.Dense(10, activation=tf.nn.softmax)
])

model.layers[0].get_weights()[1]

Error - IndexError: list index out of range

This is what has been mentioned in a few questions,but I end up getting the out of bounds error for this.

I have another question, the index followed aftermodel.layers[], does it correspond to the layer? For instance model.layers[1] gives the weights corresponding to the second layer, something like that?


2 Answers

I've been there, I have been looking at my old code to see if I could remember how did I solved that issue. What I did was to print the length of the model.layer[index].get_weights()[X] to figure out where keras was saving the weights I needed. In my old code, model.layers[0].get_weights()[1] would return the biases, while model.layers[0].get_weights()[0] would return the actual weights. In any case, take into account that there are layers which weights aren't saved (as they don't have weights), so if asking for model.layers[0].get_weights()[0] doesn't work, try with model.layers[1].get_weights()[1], as I'm not sure about flatten layers, but I do know that dense layers should save their weights.

like image 113
abeagomez Avatar answered Dec 01 '25 11:12

abeagomez


The first layer (index 0) in your model is a Flatten layer, which does not have any weights, that's why you get errors.

To get the Dense layer, which is the second layer, you have to use index 1:

model.layers[1].get_weights()[1]
like image 34
Dr. Snoopy Avatar answered Dec 01 '25 10:12

Dr. Snoopy