In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article will explain in detail how the PyTorch implementation of the common optimizer is, and the content of the article is of high quality, so the editor will share it with you for reference. I hope you will have some understanding of the relevant knowledge after reading this article.
Here we mainly talk about the implementation of different common optimizer code and make a simple comparison on a small data set.
Among them, SGD and SGDM, as well as Adam is the optimizer that comes with pytorch, while RAdam is a recently proposed optimizer that is more powerful than Adam, but in general, real bosses are still using SGDM as optimizer.
Import the necessary libraries:
Import torchimport torch.nn as nnimport torch.nn.functional as Fimport torch.optim as optimimport matplotlib.pyplot as pltimport torch.utils.data as Datafrom torch.optim.optimizer import Optimizerimport math
Main program part:
LR = 0.01BATCH_SIZE = 32EPOCH = 12
# fake datasetx = torch.unsqueeze (torch.linspace (- 1,1,300), dim=1) y = x.pow (2) + 0.1 * torch.normal (torch.zeros (* x.size ()
Torch_dataset = Data.TensorDataset (x, y) loader = Data.DataLoader (dataset=torch_dataset, batch_size=BATCH_SIZE, shuffle=True, num_workers=2)
Class Net (nn.Module): def _ init__ (self): super (Net, self). _ _ init__ () self.hidden = nn.Linear (1,20) self.prediction = nn.Linear (20,1)
Def forward (self, x): X = F.relu (self.hidden (x)) x = self.prediction (x) return x
Def main () net_SGD = Net () net_Momentum = Net () net_Adam = Net () net_RAdam = Net () nets = [net_SGD, net_Momentum, net_Adam, net_RAdam] opt_SGD = optim.SGD (net_SGD.parameters (), lr=LR) opt_Momentum = optim.SGD (net_Momentum.parameters (), lr=LR, momentum=0.9) opt_Adam = optim.Adam (net_Adam.parameters (), lr=LR, betas= ) opt_RAdam = RAdam (net_RAdam.parameters (), lr=LR,weight_decay=0) optimizers = [opt_SGD, opt_Momentum, opt_Adam, opt_RAdam] loss_func = nn.MSELoss () losses_his = [[], [] # training for epoch in range (EPOCH): print ('EPOCH:', epoch) for step, (batch_x) Batch_y) in enumerate (loader): Backx = batch_x Backy = batch_y for net, opt, l_his in zip (nets, optimizers, losses_his): out = net (BFLX) loss = loss_func (out) Bachely) opt.zero_grad () loss.backward () opt.step () l_his.append (loss.item ()) labels = ['SGD',' Momentum', 'Adam','RAdam'] for I, l_his in enumerate (losses_his): plt.plot (l_his Label=labels [I]) plt.legend (loc='best') plt.xlabel ('Steps') plt.ylabel (' Loss') plt.ylim ((0,0.2)) plt.show ()
If _ _ name__ = ='_ _ main__': main ()
The following figure is a comparison of the optimizer:
It can be seen that the effect of Adam is very good. Then SGDM second, SGDM is often used by bosses, so although the effect of SGDM does not seem to be as good as Adam, it is still recommended to try the effect of SGDM in the project.
On the common optimizer PyTorch implementation is shared here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.