import numpy as np
import torch
from torch.autograd import Variable
from torch import nn, optim
import matplotlib.pyplot as plt
# 設置字體爲中文
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
# 構造成次方矩陣
def make_fertures(x):
x = x.unsqueeze(1)
return torch.cat([x ** i for i in range(1, 4)], 1)
# y = 0.9+0.5*x+3*x*x+2.4x*x*x
W_target = torch.FloatTensor([0.5, 3, 2.4]).unsqueeze(1)
b_target = torch.FloatTensor([0.9])
# 計算x*w+b
def f(x):
return x.mm(W_target) + b_target.item()
def get_batch(batch_size=32):
random = torch.randn(batch_size)
random = np.sort(random)
random = torch.Tensor(random)
x = make_fertures(random)
y = f(x)
if (torch.cuda.is_available()):
return Variable(x).cuda(), Variable(y).cuda()
else:
return Variable(x), Variable(y)
# 多項式模型
class poly_model(nn.Module):
def __init__(self):
super(poly_model, self).__init__()
self.poly = nn.Linear(3, 1) # 輸入時3維,輸出是1維
def forward(self, x):
out = self.poly(x)
return out
if torch.cuda.is_available():
model = poly_model().cuda()
else:
model = poly_model()
# 均方誤差,隨機梯度下降
criterion = nn.MSELoss()
optimizer = optim.SGD(model.parameters(), lr=1e-3)
epoch = 0 # 統計訓練次數
ctn = []
lo = []
while True:
batch_x, batch_y = get_batch()
output = model(batch_x)
loss = criterion(output, batch_y)
print_loss = loss.item()
optimizer.zero_grad()
loss.backward()
optimizer.step()
ctn.append(epoch)
lo.append(print_loss)
epoch += 1
if (print_loss < 1e-3):
break
print("Loss: {:.6f} after {} batches".format(loss.item(), epoch))
print(
"==> Learned function: y = {:.2f} + {:.2f}*x + {:.2f}*x^2 + {:.2f}*x^3".format(model.poly.bias[0], model.poly.weight[0][0],
model.poly.weight[0][1],
model.poly.weight[0][2]))
print("==> Actual function: y = {:.2f} + {:.2f}*x + {:.2f}*x^2 + {:.2f}*x^3".format(b_target[0], W_target[0][0],
W_target[1][0], W_target[2][0]))
# 1.可視化真實數據
predict = model(batch_x)
x = batch_x.numpy()[:, 0] # x~1 x~2 x~3
plt.plot(x, batch_y.numpy(), 'ro')
plt.title(label='可視化真實數據')
plt.show()
# 2.可視化擬合函數
predict = predict.data.numpy()
plt.plot(x, predict, 'b')
plt.plot(x, batch_y.numpy(), 'ro')
plt.title(label='可視化擬合函數')
plt.show()
# 3.可視化訓練次數和損失
plt.plot(ctn,lo)
plt.xlabel('訓練次數')
plt.ylabel('損失值')
plt.title(label='訓練次數與損失關係')
plt.show()
實驗結果:
注意:批量產生數據後,進行一個排序,否則可視化時,不是按照x軸從小到大繪製,出現很多折線。對應代碼:
random = np.sort(random)
random = torch.Tensor(random)