If you read the article and have any suggestions, please feel free to contact us. It will be encouraging if you like it.
When using Pytorch and want to constrain the value of the parameter, it can be realized by the following method.
--Specifically, if you want to constrain the value of each element of the parameter matrix or vector, such as $ w> 0 $, you can do it as follows.
--In the code below, all the minimum values of the parameters in the model are set to min = 1e-4
.
--What we are doing with for
is usingkeys ()
to access all the parameters in order and apply a function called clamp
. (It's np.clip
in numpy
.)
state_dict = model.state_dict()#Calling parameters in the model
for k in state_dict.keys():
state_dict[k] = torch.clamp(state_dict[k], min=1e-4)
model.load_state_dict(state_dict)
torch.clamp
model.load_state_dict
--For example, if you want to limit the minimum value to 0, you can use the function torch.clamp
to limit the value. You can limit the range of values with torch.clamp (input, min = 0, max = 10)
.
--Since it is difficult to directly assign a value to a model parameter, there seems to be a method called model.load_state_dict
.
Recommended Posts