在這個例子中我們用亂數產生100 組線性的data,並將資料群0-89 的項目用來訓練,而剩下的90-99 用來測試,畫出來的data 如下。
這邊用torch.linspace()來產生0-100 間的均勻間隔,利用torch.randn(100)產生100組平均值為0,標準差為1的常態分部隨機變數。
import torch
import numpy as np
from torch.autograd import Variable
from matplotlib import pyplot as pltx = Variable(torch.linspace(0,100).type(torch.FloatTensor))
rand = Variable(torch.randn(100))*10
y = 3*x+rand
x_train = x[0:89] #0-89
x_test = x [90:99] #90-99
y_train = y[0:89] #0-89
y_test = y[90:99] #90-99plt.figure(figsize=(10,7))
xplot,=plt.plot(x_train.data.numpy(),y_train.data.numpy(),'o')
plt.show()
因這邊定義為回歸分布,我們可以定義函數為y=ax+b,透過 loss = torch.mean((predictions-y_train)**2) 來求算損失函數。經過1000次迭代運算後參數a,b持續調整他的值。 a.data.add_(-learn_rate*a.grad.data) ,這邊定義a 的值每次會加上-learning*a.grad
# 定義自動微分變數
a = Variable(torch.rand(1), requires_grad=True)
b = Variable(torch.rand(1), requires_grad=True)
learning_rate = 0.0001
梯度需要每次都清除,避免重複累積
for i in range(1000):
predictions = a.expand_as(x_train)*x_train+b.expand_as(x_train)
loss = torch.mean((predictions-y_train)**2)
loss.backward()
if i %50 ==0 :
print('loss:',loss)
a.data.add_(-learn_rate*a.grad.data)
b.data.add_(-learn_rate*b.grad.data)
a.grad.data.zero_()
b.grad.data.zero_()
再將圖畫出來,即為這次這條回歸線,求出來的a約為3.006,是不是很接近3呢?
x_data = x_train.data.numpy()
plt.figure(figsize=(10,7))
#plt.plot(x_train.data.numpy(),y_train.data.numpy(),'*')
xplot,=plt.plot(x_data,y_train.data.numpy(),'o')
yplot,=plt.plot(x_data,a.data.numpy()*x_data+b.data.numpy(),'-')
str1 = str(a.data.numpy()[0])+'x'+str(b.data.numpy()[0])
plt.xlabel('X')
plt.ylabel('Y')
plt.legend([xplot,yplot],['Data',str1])
plt.show()
GPU
import torch
from torch.autograd import Variable
import matplotlib.pyplot as plt
x = Variable(torch.linspace(0,100).type(torch.FloatTensor))
x = x.cuda()
rand = Variable(torch.randn(100)).cuda()*10
y = 3*x + rand
print(y.data)
x_train = x[:-10]
y_train = y[:-10]
x_test = x[-10 :]
y_test = y[-10 :]
a = Variable(torch.rand(1).cuda(),requires_grad = True)
b = Variable(torch.rand(1).cuda(),requires_grad = True)
learning_rate = 0.0001
for i in range(1000):
prediction = a.expand_as(x_train)*x_train +b.expand_as(x_train)
loss = torch.mean((prediction-y_train)**2)
print(f'loss:{loss}')
loss.backward()
a.data.add_(-learning_rate * a.grad.data)
b.data.add_(-learning_rate * b.grad.data)
a.grad.data.zero_()
b.grad.data.zero_()
# a.add_(-learning_rate * a.grad.data)
# b.add_(-learning_rate * b.grad)
# a.grad.data.zero_()
# b.grad.data.zero_()
print(a)
print(b)
plt.figure(figsize=(10,8))
x_plot, = plt.plot(x_train.cpu(),y_train.cpu(),'o')
y_plot, = plt.plot(x_train.cpu(),a.data.cpu()*x_train.cpu()+b.data.cpu(),'o')
plt.xlabel('X')
plt.ylabel('Y')
plt.show()