Update a CNN model's weights manually

If I want to update a CNN model’s weights manually, could it be done by simply changing and modifying the model.state_dict()'s weight values?

Yes, you can just edit the weights directly. Load the model state dict into a new dict, change the values there and then load that state dict. But you have to be careful doing this during training. But if you do this for a trained model and you want to modify the weights just for inference its safer to do that.

thanks. what might be the recommended functions to be used for changing the state_dict() weight values? do I have to set the param.requires_grad = False in order to update the weight values in state_dict()?

You can just create a new ordered dict and copy the state_dict() to that. You can then edit the new dict and then load it to the model using the usual load_state_dict() function.

No, you don’t have to set param.requires_grad = False explicitly. But if you are only changing the weights for inference and not training, then you can just do a model.eval() first.