Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How tensorflow model.weights works?

I have defined a nueral network model:

model = keras.models.Sequential([
    keras.layers.Flatten(input_shape = (15,)), # the input layer
    keras.layers.Dense(20, activation = 'relu'), #the hidden
    keras.layers.Dense(20, activation = 'relu'), #the hidden
    keras.layers.Dense(20, activation = 'relu'), #the hidden
    keras.layers.Dense(20, activation = 'relu'), #the hidden
    keras.layers.Dense(20, activation = 'relu'), #the hidden
    keras.layers.Dense(2) #The output layer
])

This the summery of the model:

Model: "sequential_7"
_________________________________________________________________
 Layer (type)                Output Shape              Param #   
=================================================================
 flatten_7 (Flatten)         (None, 15)                0         
                                                                 
 dense_40 (Dense)            (None, 20)                320       
                                                                 
 dense_41 (Dense)            (None, 20)                420       
                                                                 
 dense_42 (Dense)            (None, 20)                420       
                                                                 
 dense_43 (Dense)            (None, 20)                420       
                                                                 
 dense_44 (Dense)            (None, 20)                420       
                                                                 
 dense_45 (Dense)            (None, 2)                 42        
                                                                 
=================================================================
Total params: 2,042
Trainable params: 2,042
Non-trainable params: 0

After I trained it I expected to get weights of the form:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

16X20, 21X20, …. 21X2 becose the const colum the neural network has.

But when I actually measure the shapes of model.weights I actually get matrixes

at the even colums and and vectors at the odd places:

mats = [np.array(w) for w in model.weights[0::2]]
vecs= [np.array(w) for w in model.weights[1::2]]
print([w.shape for w in mats])
print([w.shape for w in vecs])


[(15, 20), (20, 20), (20, 20), (20, 20), (20, 20), (20, 2)]
[(20,), (20,), (20,), (20,), (20,), (2,)]

My question is: Are the vectors here are just the zero lines of the mats that correspond to the const parameter at each layer?

>Solution :

The vectors here are the biases that are defined for each Dense layer. Try outputting the weights of the first Dense layer for example:

import tensorflow as tf

model = tf.keras.models.Sequential([
    tf.keras.layers.Flatten(input_shape = (15,)), # the input layer
    tf.keras.layers.Dense(20, activation = 'relu'), #the hidden
    tf.keras.layers.Dense(20, activation = 'relu'), #the hidden
    tf.keras.layers.Dense(20, activation = 'relu'), #the hidden
    tf.keras.layers.Dense(20, activation = 'relu'), #the hidden
    tf.keras.layers.Dense(20, activation = 'relu'), #the hidden
    tf.keras.layers.Dense(2) #The output layer
])

print(model.layers[1].weights)

You should see the kernel weights and the biases. This corresponds to the description of the Dense layer:

Dense implements the operation: output = activation(dot(input, kernel) + bias)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading