Skip to content

Extracting first-layer weights from a multi-layer Keras NN and transferring them to a single layer NN

I trained a 3-hidden layer NN (3-HL) using Keras (with good results, and I wanted to extract the weights from its first layer (inputs to its first-hidden layer) and use them in a single-hidden layer NN (inputs to its single hidden layer), to train. The 3-HL model summary along with its extracted (hopefully first layer) weight dimensions is as follows:

enter image description here

enter image description here

enter image description here

And here are the same info for the single-hidden layer which I aim to train using the extracted first-layer weights from the above model:

enter image description here

enter image description here

enter image description here

As you can see. both weights seem to have similar shape and size and dimensions. But when I put this line to set the single-hidden layer model weights, I get a valueError as follows:

enter image description here

enter image description here

Any idea how I could solve this?

Answer

Instead of getting weights and the biases separately you should get them together and input that into set_weights() function. Try following:

model.layers[0].set_weights(transfered_model.layers[0].get_weights())