There is a difference between model definition (its forward
function), and the parameter configuration (what's called model state, and is easily accessible as a dictionary using state_dict
).
You can get a model's state, as you did with your implementation flattenNetwork
. However reverting this operation (i.e. if you only have the weights and layer shapes), for pretty much all models, is not possible.
Now, assuming you do - still - have access to net
. My advice is that work with net.state_dict()
directly, modify it, then load the dictionary of weights back with load_state_dict
. This way, you will avoid having to deal with serializing the model's parameters yourself.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…