9

I am teaching myself data science and something peculiar has caught my eyes. In a sample DNN tutorial I was working on, I found that the Keras layer.get_weights() function returned empty list for my variables. I've successfully cross validated and used model.fit() function to compute the recall scores.

But as I'm trying to use the get_weights() function on my categorical variables, it returns empty weights for all.

I'm not looking for a solution to code but I am just curious about what would possibly cause this. I've read through the Keras API but it did not provide me with the information I was hoping to see. What could cause the get_weights() function in Keras to return empty list except for of course the weights not being set?

nbro
  • 15,395
  • 32
  • 113
  • 196
Ishiro Kusabi
  • 211
  • 2
  • 6
  • 13

2 Answers2

4

Maybe you are asking for weights before they are created.

Weights are created when the Model is first called on inputs or build() is called with an input_shape.

For example, if you load weights from checkpoint but you don't give an input_shape to the model, then get_weights() will return an empty list.

nbro
  • 15,395
  • 32
  • 113
  • 196
pbon
  • 41
  • 2
2

It might also be, that you are trying to get weights from layers, which don't have any weights. Let's say, that you've defined below model:

input = Input(shape=(4,))
hidden_layer_0 = Dense(4, activation='tanh')(input)
hidden_layer_1 = Dense(4, activation='tanh')(hidden_layer_0)
output = Lambda(lambda t: l2_normalize(100000*t, axis=1))(hidden_layer_1)

model = Model(input, output)

and want to print weights of each layer (after building/training it previously). You can do this as follows:

for layer in model.layers:
    print("===== LAYER: ", layer.name, " =====")
    if layer.get_weights() != []:
        weights = layer.get_weights()[0]
        biases = layer.get_weights()[1]
        print("weights:")
        print(weights)
        print("biases:")
        print(biases)
    else:
        print("weights: ", [])

If you run this code, you will get something like this:

===== LAYER:  input_1  =====
weights:  []
===== LAYER:  dense  =====
weights:
[[-6.86365739e-02  2.24897027e-01  ...  1.90570995e-01]]
biases:
[-0.02512692  -0.00486927  ...  0.04254978]
===== LAYER:  dense_1  =====
weights:
[[-6.86365739e-02  2.24897027e-01  ...  1.90570995e-01]]
biases:
[-0.02512692  0.00933884 ...  0.04254978]
===== LAYER:  lambda  =====
weights:  []

As you can see, first (Input) and the last (Lambda) layers don't have any weights.

brzepkowski
  • 265
  • 1
  • 14