Fetch weights of the previous layer

84 views Asked by At

Is it possible to fetch the weights of the previous layer, modify them and set again to the next layer. I want to introduce a custom layer in the network which will modify the weights( as per the desired logic ) and then set the modified weight values to the next layer. Similar to what is depicted in the figure below:

enter image description here

I am not sure if this is possible or not. I know that we can dump the snapshot and then use it to set the new weights. I can also converted the weights using the snapshots. But, I dont know how to do this within the network itself ( without taking or using any snapshot).

Thanks

KK

1

There are 1 answers

0
ginge On

tl;dr: Load one model (without compiling) and use the weights you want to initialize a model. Create new weights for the layers you want to change.

Full version:

As per this thread and as explained by fchollet himself, The canonical way to do this is to load your weights into the previous Keras model (you don't need to compile it, so it's instant) and use that model as a query-able datastructure to access the weights.

For a sequential model you can do it like this:

weights = model.layers[5].get_weights()
model.layers[5].set_weights(weights)

See also: another discussion on this topic with fchollet.