您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關(guān)pytorch如何實現(xiàn)線性擬合方式,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
一維線性擬合
數(shù)據(jù)為y=4x+5加上噪音
結(jié)果:
import numpy as np from mpl_toolkits.mplot3d import Axes3D from matplotlib import pyplot as plt from torch.autograd import Variable import torch from torch import nn X = torch.unsqueeze(torch.linspace(-1, 1, 100), dim=1) Y = 4*X + 5 + torch.rand(X.size()) class LinearRegression(nn.Module): def __init__(self): super(LinearRegression, self).__init__() self.linear = nn.Linear(1, 1) # 輸入和輸出的維度都是1 def forward(self, X): out = self.linear(X) return out model = LinearRegression() criterion = nn.MSELoss() optimizer = torch.optim.SGD(model.parameters(), lr=1e-2) num_epochs = 1000 for epoch in range(num_epochs): inputs = Variable(X) target = Variable(Y) # 向前傳播 out = model(inputs) loss = criterion(out, target) # 向后傳播 optimizer.zero_grad() # 注意每次迭代都需要清零 loss.backward() optimizer.step() if (epoch + 1) % 20 == 0: print('Epoch[{}/{}], loss:{:.6f}'.format(epoch + 1, num_epochs, loss.item())) model.eval() predict = model(Variable(X)) predict = predict.data.numpy() plt.plot(X.numpy(), Y.numpy(), 'ro', label='Original Data') plt.plot(X.numpy(), predict, label='Fitting Line') plt.show()
多維:
from itertools import count import torch import torch.autograd import torch.nn.functional as F POLY_DEGREE = 3 def make_features(x): """Builds features i.e. a matrix with columns [x, x^2, x^3].""" x = x.unsqueeze(1) return torch.cat([x ** i for i in range(1, POLY_DEGREE+1)], 1) W_target = torch.randn(POLY_DEGREE, 1) b_target = torch.randn(1) def f(x): return x.mm(W_target) + b_target.item() def get_batch(batch_size=32): random = torch.randn(batch_size) x = make_features(random) y = f(x) return x, y # Define model fc = torch.nn.Linear(W_target.size(0), 1) batch_x, batch_y = get_batch() print(batch_x,batch_y) for batch_idx in count(1): # Get data # Reset gradients fc.zero_grad() # Forward pass output = F.smooth_l1_loss(fc(batch_x), batch_y) loss = output.item() # Backward pass output.backward() # Apply gradients for param in fc.parameters(): param.data.add_(-0.1 * param.grad.data) # Stop criterion if loss < 1e-3: break def poly_desc(W, b): """Creates a string description of a polynomial.""" result = 'y = ' for i, w in enumerate(W): result += '{:+.2f} x^{} '.format(w, len(W) - i) result += '{:+.2f}'.format(b[0]) return result print('Loss: {:.6f} after {} batches'.format(loss, batch_idx)) print('==> Learned function:\t' + poly_desc(fc.weight.view(-1), fc.bias)) print('==> Actual function:\t' + poly_desc(W_target.view(-1), b_target))
關(guān)于“pytorch如何實現(xiàn)線性擬合方式”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。