# 线性回归 相关代码
import random
import torch
from d2l import torch as d2l
import numpy as np
from torch.utils import data
from torch import nn
#线性回归0开始实现
class realize1:
def synthetic_data(self, w, b, num_examples):
# y = xw+b+c(噪声)
# 均值为0, 方差为1, num_examples行, len(w)列的数据
x = torch.normal(0, 1, (num_examples, len(w)))
# print(x.shape)
# 矩阵乘法,可运算高维张量,当为二维时和mm函数相同
y = torch.matmul(x, w) + b
# print(y.shape)
y += torch.normal(0, 0.01, y.shape)
print(y.shape, y[-1])
# 将一维数据变成二维数据
return x, y.reshape((-1, 1))
def data_iter(self, batch_size, features, lables):
num_examples = len(features)
indices = list(range(num_examples))
# 随机打乱
random.shuffle(indices)
for i in range(0, num_examples, batch_size):
# batch_indices = indices[i:min(i+batch_size, num_examples)]
# 和上面一句一样,格式不同
batch_indices = torch.tensor(indices[i:min(i + batch_size, num_examples)])
# print(batch_indices)
# print(features[batch_indices])
yield features[batch_indices], lables[batch_indices]
# 定义线性模型
def linreg(self, x, w, b):
return torch.matmul(x, w) + b
# 均方损失
def squared_loss(self, y_hat, y):
return (y_hat - y.reshape(y_hat.shape)) ** 2 / 2
# 定义优化算法(更新参数w和b) 小批量随机梯度下降
def sgd(self, params, lr, batch_size):
with torch.no_grad():
for param in params:
# print("参数的梯度")
# print(param.grad)
# 样本梯度的均值
param -= lr * param.grad / batch_size
param.grad.zero_()
def run(self):
# 数据生成如下
true_w = torch.tensor([2.0,-3.4])
true_b = 4.2
features, lables = self.synthetic_data(true_w,true_b,1000)
# print(lables.shape, lables[-1])
d2l.set_figsize()
d2l.plt.scatter(features[:, 1].detach().numpy(), lables.detach().numpy(),1)
d2l.plt.show()
batch_size = 10
# data_iter(batch_size, features, lables)
# 初始化模型参数
w = torch.normal(0, 0.01, size=(2,1), requires_grad=True)
b = torch.zeros(1, requires_grad=True)
# 初始参数的梯度
# print(w.grad)
# print(b.grad)
# 学习率
lr = 0.03
# 学习次数
num_epochs = 3
# 模型,选择模型不同进行更改
net = self.linreg
loss = self.squared_loss
for epoch in range(num_epochs):
for x, y in self.data_iter(batch_size, features, lables):
# print("x,y", x, y)
l = loss(net(x,w,b), y)
# print(l)
# print("w,b 梯度", w.grad, b.grad)
l.sum().backward()
# print("w,b 梯度", w.grad, b.grad)
# 使用参数的梯度更新参数
self.sgd([w, b], lr, batch_size)
with torch.no_grad():
train_l = loss(net(features,w,b), lables)
print(f'epoch {epoch+1}, loss {float(train_l.mean()):f}')
class realize2:
#构造pytorch 数据迭代器
def load_array(self,data_arrays, batch_size, is_train=True):
dataset = data.TensorDataset(*data_arrays)
return data.DataLoader(dataset, batch_size, shuffle=is_train)
def run(self):
batch_size = 10
true_w = torch.tensor([2.0,-3.4])
true_b = 4.2
# 人工数据合成函数
num_examples = 1000
features, labels = d2l.synthetic_data(true_w,true_b,num_examples)
# 线性回归等价于全连接层,这里用全连接的函数Linear, 输入二维, 输出一维
# Sequential meas 'list of layers' 将每层加入进去,这里只有一层
net = nn.Sequential(nn.Linear(2,1))
# 初始化w和b. 原来直接用的随机数 net[0]一维
net[0].weight.data.normal_(0, 0.01)
net[0].bias.data.fill_(0)
print(net[0])
# 损失
loss = nn.MSELoss()
trainer = torch.optim.SGD(net.parameters(), lr=0.03)
# 训练模块
num_epochs = 3
for epoch in range(num_epochs):
for x,y in iter(self.load_array((features, labels), batch_size, True)):
l = loss(net(x), y)
# 设置trainer的参数的梯度为0
trainer.zero_grad()
# 反向传播
l.backward()
# 更改w, b
trainer.step()
l = loss(net(features), labels)
print(f'epoch {epoch + 1}, loss {l:f}')
if __name__=='__main__':
r2 = realize2()
r2.run()
r1 = realize1()
r1.run()
跟着李沐学深度学习-线性回归(代码自敲自用)
于 2024-01-11 21:11:54 首次发布