In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly shows you "pytorch how to achieve polynomial regression", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "pytorch how to achieve polynomial regression" this article.
Pytorch realizes polynomial regression, for your reference, the details are as follows
Although the univariate linear regression model can fit a straight line, the accuracy is still poor, and the fitted straight line can not pass through every point. Polynomial regression fitting is needed for complex fitting tasks to improve the accuracy. Polynomial regression fitting is to improve the degree of the feature, the degree of linear regression to make one, in fact, we can use the second, third, fourth or even higher degree of fitting. As the increasing complexity of the model will bring the risk of overfitting, it is necessary to reduce overfitting by regularization loss and improve the generalization ability of the model. I hope you can do it yourself and master pytorch through some small training (there are some codes that observe the data format in the case, which you can comment out by yourself)
# compared with the univariate linear regression model, polynomial regression can improve the fitting accuracy. However, it should be noted that the fitting risk # polynomial regression equation f (x) =-1.13x-2.14x ^ 2 + 3.12x ^ 3-0.01x ^ 4 + 0.512import torchimport matplotlib.pyplot as pltimport numpy as np# data preparation (test data) x = torch.linspace (- 2L2) print (x.shape) y =-1.13x-2.14*torch.pow (x Magne2) + 3.15*torch.pow (x Magne3)-0.01*torch.pow (x 4) + 0.512plt.scatter (x.data.numpy (), y.data.numpy ()) plt.show () # now the input dimension is 4 dimensions # in order to splice the input data You need to write auxiliary data and enter the scalar x to make it a matrix. Use torch.cat concatenation def features (x): # to generate matrix # [x, x ^ 2, x ^ 3, x ^ 4] x = x.unsqueeze (1) print (x.shape) return torch.cat ([x * * i for i in range (1Power5)], 1) result = features (x) print (result.shape) # the target formula is used to calculate the standard output # target formula corresponding to the input feature as follows: x_weight = torch.Tensor Unsqueeze (1) b = torch.Tensor ([0.512]) # get the standard output def target (x): return x.mm (x_weight) + b.item () # create a new function to randomly generate input data and output data Used to generate training data def get_batch_data (batch_size): # generate batch_size random x batch_x = torch.randn (batch_size) # generate a matrix features_x = features (batch_x) target_y = target (features_x) return features_x for each x Target_y# create model class PolynomialRegression (torch.nn.Module): def _ _ init__ (self): super (PolynomialRegression, self). _ _ init__ () # input four-dimensional output one-dimensional self.poly = torch.nn.Linear (4meme 1) def forward (self X): return self.poly (x) # start training model epochs = 10000batch_size = 32model = PolynomialRegression () criterion = torch.nn.MSELoss () optimizer = torch.optim.SGD (model.parameters (), 0.001) for epoch in range (epochs): print ("{} / {}" .format (epoch+1,epochs)) batch_x,batch_y = get_batch_data (batch_size) out = model (batch_x) loss = criterion (out) Batch_y) optimizer.zero_grad () loss.backward () # Update gradient optimizer.step () if (epoch% 100 = 0): print ("Epoch: [{} / {}] Loss: {: .6f} ".format (epoch,epochs,loss.item ()) if (epoch% 1000 = = 0): predict = model (features (x)) print (x.shape) print (predict.shape) print (predict.squeeze (1) .shape) plt.plot (x.data.numpy (), predict.squeeze (1). Data.numpy ()," r ") loss = criterion (predict) Y) plt.title ("Loss: {: .4f}" .format (loss.item ()) plt.xlabel ("X") plt.ylabel ("Y") plt.scatter (xMagy) plt.show ()
Fitting result:
These are all the contents of the article "how to achieve polynomial regression in pytorch". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.