How to use PyTorch optimizer
This article mainly explains "how to use PyTorch optimizer". Friends who are interested might as well take a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn how to use PyTorch optimizer.
The function of the optimizer is to manage and update the parameter group. Please build a SGD optimizer and add three groups of parameters through the add_param_group method. The learning rates of the three groups of parameters are 0.01,0.02,0.03,0.9,0.8,0.7 respectively. After the optimizer is built, print the key and value of each element in the param_groups attribute in the optimizer (hint: param_groups is list Each of its elements is a dictionary)
Control optimizerimport torchimport torch.optim as optimtorch.manual_seed (1234) W1 = torch.randn ((2,2), requires_grad=True) w2 = torch.randn ((2,2), requires_grad=True) w3 = torch.randn ((2,2), requires_grad=True) w1.grad = torch.ones ((2,2)) print (w1.grad, w2.grad, w3.grad) optimizer = optim.SGD ([W1], lr=1, momentum=0.9) optimizer.add_param_group ({"params": W2, 'lr': 2) ) optimizer.add_param_group ({"params": W3, 'lr': 3,' momentum': 0.7}) print ("optimizer.param_groups is\ n {}" .format (optimizer.param_groups)) optimizer.step () print (w1, w2, w3)
At this point, I believe you have a deeper understanding of "how to use PyTorch optimizer". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!