您好,登錄后才能下訂單哦!
本篇內(nèi)容介紹了“pytorch實(shí)現(xiàn)線性回歸和多元回歸的方法”的有關(guān)知識(shí),在實(shí)際案例的操作過(guò)程中,不少人都會(huì)遇到這樣的困境,接下來(lái)就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!
最近在學(xué)習(xí)pytorch,現(xiàn)在把學(xué)習(xí)的代碼放在這里,下面是github鏈接
直接附上github代碼
# 實(shí)現(xiàn)一個(gè)線性回歸 # 所有的層結(jié)構(gòu)和損失函數(shù)都來(lái)自于 torch.nn # torch.optim 是一個(gè)實(shí)現(xiàn)各種優(yōu)化算法的包,調(diào)用的時(shí)候必須是需要優(yōu)化的參數(shù)傳入,這些參數(shù)都必須是Variable x_train = np.array([[3.3],[4.4],[5.5],[6.71],[6.93],[4.168],[9.779],[6.182],[7.59],[2.167],[7.042],[10.791],[5.313],[7.997],[3.1]],dtype=np.float32) y_train = np.array([[1.7],[2.76],[2.09],[3.19],[1.694],[1.573],[3.366],[2.596],[2.53],[1.221],[2.827],[3.465],[1.65],[2.904],[1.3]],dtype=np.float32) # 首先我們需要將array轉(zhuǎn)化成tensor,因?yàn)閜ytorch處理的單元是Tensor x_train = torch.from_numpy(x_train) y_train = torch.from_numpy(y_train) # def a simple network class LinearRegression(nn.Module): def __init__(self): super(LinearRegression,self).__init__() self.linear = nn.Linear(1, 1) # input and output is 2_dimension def forward(self, x): out = self.linear(x) return out if torch.cuda.is_available(): model = LinearRegression().cuda() #model = model.cuda() else: model = LinearRegression() #model = model.cuda() # 定義loss function 和 optimize func criterion = nn.MSELoss() # 均方誤差作為優(yōu)化函數(shù) optimizer = torch.optim.SGD(model.parameters(),lr=1e-3) num_epochs = 30000 for epoch in range(num_epochs): if torch.cuda.is_available(): inputs = Variable(x_train).cuda() outputs = Variable(y_train).cuda() else: inputs = Variable(x_train) outputs = Variable(y_train) # forward out = model(inputs) loss = criterion(out,outputs) # backword optimizer.zero_grad() # 每次做反向傳播之前都要進(jìn)行歸零梯度。不然梯度會(huì)累加在一起,造成不收斂的結(jié)果 loss.backward() optimizer.step() if (epoch +1)%20==0: print('Epoch[{}/{}], loss: {:.6f}'.format(epoch+1,num_epochs,loss.data)) model.eval() # 將模型變成測(cè)試模式 predict = model(Variable(x_train).cuda()) predict = predict.data.cpu().numpy() plt.plot(x_train.numpy(),y_train.numpy(),'ro',label = 'original data') plt.plot(x_train.numpy(),predict,label = 'Fitting line') plt.show()
結(jié)果如圖所示:
多元回歸:
# _*_encoding=utf-8_*_ # pytorch 里面最基本的操作對(duì)象是Tensor,pytorch 的tensor可以和numpy的ndarray相互轉(zhuǎn)化。 # 實(shí)現(xiàn)一個(gè)線性回歸 # 所有的層結(jié)構(gòu)和損失函數(shù)都來(lái)自于 torch.nn # torch.optim 是一個(gè)實(shí)現(xiàn)各種優(yōu)化算法的包,調(diào)用的時(shí)候必須是需要優(yōu)化的參數(shù)傳入,這些參數(shù)都必須是Variable # 實(shí)現(xiàn) y = b + w1 *x + w2 *x**2 +w3*x**3 import os os.environ['CUDA_DEVICE_ORDER']="PCI_BUS_ID" os.environ['CUDA_VISIBLE_DEVICES']='0' import torch import numpy as np from torch.autograd import Variable import matplotlib.pyplot as plt from torch import nn # pre_processing def make_feature(x): x = x.unsqueeze(1) # unsquenze 是為了添加維度1的,0表示第一維度,1表示第二維度,將tensor大小由3變?yōu)椋?,1) return torch.cat([x ** i for i in range(1, 4)], 1) # 定義好真實(shí)的數(shù)據(jù) def f(x): W_output = torch.Tensor([0.5, 3, 2.4]).unsqueeze(1) b_output = torch.Tensor([0.9]) return x.mm(W_output)+b_output[0] # 外積,矩陣乘法 # 批量處理數(shù)據(jù) def get_batch(batch_size =32): random = torch.randn(batch_size) x = make_feature(random) y = f(x) if torch.cuda.is_available(): return Variable(x).cuda(),Variable(y).cuda() else: return Variable(x),Variable(y) # def model class poly_model(nn.Module): def __init__(self): super(poly_model,self).__init__() self.poly = nn.Linear(3,1) def forward(self,input): output = self.poly(input) return output if torch.cuda.is_available(): print("sdf") model = poly_model().cuda() else: model = poly_model() # 定義損失函數(shù)和優(yōu)化器 criterion = nn.MSELoss() optimizer = torch.optim.SGD(model.parameters(), lr=1e-3) epoch = 0 while True: batch_x, batch_y = get_batch() #print(batch_x) output = model(batch_x) loss = criterion(output,batch_y) print_loss = loss.data optimizer.zero_grad() loss.backward() optimizer.step() epoch = epoch +1 if print_loss < 1e-3: print(print_loss) break model.eval() print("Epoch = {}".format(epoch)) batch_x, batch_y = get_batch() predict = model(batch_x) a = predict - batch_y y = torch.sum(a) print('y = ',y) predict = predict.data.cpu().numpy() plt.plot(batch_x.cpu().numpy(),batch_y.cpu().numpy(),'ro',label = 'Original data') plt.plot(batch_x.cpu().numpy(),predict,'b', ls='--',label = 'Fitting line') plt.show()
“pytorch實(shí)現(xiàn)線性回歸和多元回歸的方法”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。