Transfer part layers of weights/biases/optimizer to another neural network

Let’s say I have two cov neural networks (network A and network B) where the architectures (network A has 19 layers and network B has 20 layers) are almost identical.

How could I transfer the first five layers of weights/biases/optimizer from network A to network B?

I need some help with coding. Thanks.

I think the cleanest approach would be to create a mapping between the layer names of modelA and modelB and load each state_dict of these layers separately as described here.
This approach would need an explicit definition of which layers should be loaded and would not rely on e.g. using the strict=False argument, which might yield unexpected results if the output is ignored.