Is there a way to change the weights of a layer in a neural network, without reconstructing the whole network?
For example,
lenet = NetInitialize@NetChain[{
ConvolutionLayer[20, 5], Ramp, PoolingLayer[2, 2],
ConvolutionLayer[50, 5], Ramp, PoolingLayer[2, 2],
FlattenLayer[], 500, Ramp, 10, SoftmaxLayer[]},
"Output" -> NetDecoder[{"Class", Range[0, 9]}],
"Input" -> NetEncoder[{"Image", {28, 28}, "Grayscale"}]
]
The weights of the second convolution layer can be extracted using NetExtract
NetExtract[lenet, {4, "Weights"}]
But this is extracted convolution layer is now independent of the neural network. How can I change the weights of this layer in the neural network in place? I'm looking something like
lenet[[4]]["Weights"] = myWeights
Keras has a similar functionality that allows we to modify the weights directly like
layer.set_weights([ker,bia])
Being able to change the weights in place is very useful, for instance, in loading pre-trained weights file.
ConvolutionLayer[... "Weights"->w]
. So you could create a new network and initialize all layers to the weights you want $\endgroup$NetReplacePart
looks like a promising way to replace part of an existing network. Haven't tried it, though. $\endgroup$