I want to manually assign the training parameters of the [Pytorch] model

If you read the article and have any suggestions, please feel free to contact us. It will be encouraging if you like it.

When you want to manually assign the training parameters of the model

When using Pytorch and want to constrain the value of the parameter, it can be realized by the following method.

Concrete example

--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.)

code

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)

point

  1. Use torch.clamp
  2. Use model.load_state_dict

Commentary

--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

I want to manually assign the training parameters of the [Pytorch] model
I want to customize the appearance of zabbix
I want to grep the execution result of strace
I want to use PyTorch to generate something like the lyrics of Japari Park
I want to fully understand the basics of Bokeh
I want to increase the security of ssh connections
I want to use only the normalization process of SudachiPy
I want to get the operation information of yahoo route
I made a function to check the model of DCGAN
I want to judge the authenticity of the elements of numpy array
I want to know the features of Python and pip
Keras I want to get the output of any layer !!
I want to know the legend of the IT technology world
I want to get the name of the function / method being executed
I want to read the html version of "OpenCV-Python Tutorials" OpenCV 3.1 version
I want to output the beginning of the next month with Python
I summarized how to change the boot parameters of GRUB and GRUB2
I want to check the position of my face with OpenCV!
I want to know the population of each country in the world.
I want to handle the rhyme part1
I want to handle the rhyme part3
I want to display the progress bar
I want to handle the rhyme part2
I want to handle the rhyme part5
I want to handle the rhyme part4
[Note] I want to completely preprocess the data of the Titanic issue-Age version-
I don't want to admit it ... The dynamical representation of Neural Networks
(Python Selenium) I want to check the settings of the download destination of WebDriver
I want to batch convert the result of "string" .split () in Python
I want to explain the abstract class (ABCmeta) of Python in detail.
I want to sort a list in the order of other lists
I want to express my feelings with the lyrics of Mr. Children
I want to analyze the emotions of people who want to meet and tremble
I want to use the Qore SDK to predict the success of NBA players
I want to leave an arbitrary command in the command history of Shell
I want to stop the automatic deletion of the tmp area with RHEL7
Python: I want to measure the processing time of a function neatly
I tried to predict the behavior of the new coronavirus with the SEIR model.
I want to handle the rhyme part7 (BOW)
I tried to touch the API of ebay
I tried to correct the keystone of the image
I want to get League of Legends data ③
I want to get League of Legends data ②
I want to get League of Legends data ①
I want to use the activation function Mish
I want to display the progress in Python!
I tried to predict the price of ETF
I tried to vectorize the lyrics of Hinatazaka46!
I want to get the path of the directory where the running file is stored.
I want to visualize the transfer status of the 2020 J League, what should I do?
The story of IPv6 address that I want to keep at a minimum
I want to use Python in the environment of pyenv + pipenv on Windows 10
I want to set a life cycle in the task definition of ECS
I want to add silence to the beginning of a wav file for 1 second
I want to see a list of WebDAV files in the Requests module
I want to crop the image along the contour instead of the rectangle [python OpenCV]
I want to store the result of% time, %% time, etc. in an object (variable)
I tried to create a model with the sample of Amazon SageMaker Autopilot
I want to see the file name from DataLoader
I tried to make something like a chatbot with the Seq2Seq model of TensorFlow
I want to detect images of cats from Instagram