SoFunction
Updated on 2025-03-04

How to use pytorch pre-training layer

How to use pytorch pre-training layer

Use the network trained elsewhere to use the new network

Loading pre-trained network

1.Originally, a network has been trained. AutoEncoder_FC()

2. First load the network and read its stored parameters

3. Set a parameter set

cnnpre = AutoEncoder_FC()
cnnpre.load_state_dict(('autoencoder_FC.pkl')['state_dict'])
cnnpre_dict =cnnpre.state_dict()

Loading a new network

1. Set up a new network

2. Set up a new network parameter set

cnn= AutoEncoder()
cnn_dict = cnn.state_dict()

Update new network parameters

1. Compare the two parameter sets and retain existing network parameters

2. Update the new network parameter set with the reserved parameters

3. Load the new network parameters into the new network

cnnpre_dict = {k: v for k, v in cnnpre_dict.items() if k in cnn_dict}
cnn_dict.update(cnnpre_dict)
cnn.load_state_dict(cnn_dict)

The above method of using the pytorch pre-training layer is all the content I have shared with you. I hope you can give you a reference and I hope you can support me more.