Noran
Noran

Reputation: 717

Saving model weights in Keras: what is model weights?

I created a deep learning model for image recognition by Keras, and I saved the model weights by model.save_weights('weights.h5'). Also, I loaded it and used the weights again.

I know that model.save_weights() saves the model weights. My question is what is the model weights? Is it the filters weights?

Upvotes: 2

Views: 7432

Answers (1)

today
today

Reputation: 33410

Model weights are all the parameters (including trainable and non-trainable) of the model which are in turn all the parameters used in the layers of the model. And yes, for a convolution layer that would be the filter weights as well as the biases.

Actually, you can see them for each layer: try model.layers[layer_index].get_weights() and you would get the weights of that layer. When you call save_weights() actually it is the output of get_weights() called on each of the layers that is stored in the file.

For example for a convolution layer, get_weights() method would return a list with two elements which corresponds to filter weights and the biases. Here is an example:

model = Sequential()
model.add(Conv2D(5, (3,3), input_shape=(100, 100, 3)))

filters, biases = model.layers[0].get_weights()

>>> filters.shape
(3, 3, 3, 5)  <--- 5 filters of shape (3, 3, 3)

>>> biases.shape
(5,)    <--- one bias parameter for each filter

>>> filters[:, :, :, 0]   # get the first filter's weights
array([[[-0.26788074, -0.20213448,  0.06233829],
    [ 0.08651951,  0.21303588,  0.08127764],
    [ 0.04672694, -0.24589485, -0.12873489]],

   [[ 0.10841686,  0.24839908, -0.07466605],
    [-0.26903206, -0.0341135 ,  0.15083215],
    [-0.07382561, -0.00576964, -0.25354072]],

   [[-0.02937067,  0.22315139, -0.12964793],
    [ 0.23371089,  0.19973844, -0.00728002],
    [-0.2748396 , -0.02097657,  0.22772402]]], dtype=float32)

Upvotes: 5

Related Questions