How to change weight after traning nueralNetwork

I use loss.backward() and optimizer.step() to train my NeuralNetwork, but I want to change the data of weights before testing. My target is that, when weight is less than one interval or zero, the weight is set to a fixed value. my code is as following:
for f in neuralnetwork.layer.weight.data:
if neuralnetwork.layer.weight.data <= 0:
neuralnetwork.layer.weight.data = 0
but this code is not correct, how can I change my code?
thanks for tell me how to do! thanks very much

1 Like

You can get the parameters by state_dict = model.state_dit(), and state_dict will hold all the trainable parameters.

You can do whatever changes you want to the content of the dict.

At last you just use model.load_state_dict(state_dict) to load all the updated state_dict back to the model.

4 Likes

thanks,i will try your advise