I am trying to figure out how to target certain model weights withtin my 1000x 1000 feed forward network in keras

>>>weights = loaded_model.layers[3].get_weights()
>>> type(weights)
[0] list
>>> len(weights)
[1] 2
>>> weights[0].shape 
[2] (1000, 1000)
>>> weights[1].shape
[3] (1000,)

I know that weights[1] is the layer biases for each input and that weights[0] is the values for each the weights of each neural connection.

I am trying to get a list of all the connections weights for each neuron. The only problem is that I can't seem to figure out if I would need to pull the nth row of the (1000, 1000) weights matrix or the nth column of the (1000, 1000) weights matrix.

TLDR: is the weights for the input of the nth neuron located at (1000, n) or (n, 1000)

  • $\begingroup$ Do you want all the weights from a given neuron in one layer to the next layer, or all the weights to a given neuron from the previous layer? I'd assume the latter, if you want to match it up with the bias . . . but could you please make this clearer in the question? $\endgroup$ – Neil Slater Jul 7 '19 at 17:27

I solved my problem by simplifying it.

Suppose I am auditing the weights for a layer that has 2 perceptions and the previous layer that has 4 outputs.

>>> weights[0].shape
[1] (4,2)

Since I am trying to get the weights for the input of the second perception, I can expect a 4 dimensional vector. To achieve a 4 dimensional output, I should not be adjusting the number of rows, rather the number of columns.

Which leads me to believe I should be looking for the (1000, nth) column of the weight matrix


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.