Pytorch实现线性回归的两种方式

一、纯手写方式

既要实现线性回归,那么大致的思路是:在每一个循环的epoch里面取出真实的X和Y的值,然后使用X计算预测的Y_hat,使用平方损失函数或者其他的损失函数计算损失值,然后loss反向传播更新参数。

代码如下:

import torch
import random
# 首先导入包并定义真实的参数值
true_w = torch.tensor([2.4,3.5])
true_b = 1
# 定义合成数据的方法,需要注意的是为了更逼真一点,我们在y值后面添加了一个噪声
def synthetic_data(w,b):
    X = torch.normal(0,1,(1000,len(w)))
    y = torch.matmul(X,w) + b
    y += torch.normal(0,0.01,y.shape)
    return X,y.reshape((-1,1))
features,labels = synthetic_data(true_w,true_b)

# 定义完真实数据之后需要实现一个取数据的迭代器
def data_iter(batch_size,features,labels):
    num_examples = len(features)
    indices = list(range(num_examples))
    random.shuffle(indices)
    for i in range(0,num_examples,batch_size):
        batch_indices = torch.tensor(indices[i:min(i+batch_size,num_examples)])
    yield features[batch_indices],labels[batch_indices]

batch_size = 10

# 定义线性模型
def linreg(X,w,b):
    return torch.matmul(X,w) + b
# 定义误差计算公式
def square_loss(y_hat,y):
    return (y_hat-y.reshape(y_hat.shape))**2
# 定义更新的方式,使用SGD
def sgd(params,lr,batch_size):
    with torch.no_grad():
        for param in params:
            param -= lr*param.grad / batch_size
            param.grad.zero_()
# 开始训练,随机设置w和b
w = torch.normal(0,0.01,size=(2,1),requires_grad=True)
b = torch.zeros(1,requires_grad=True)
# 设置训练的轮数以及学习率
epochs = 5
batch_size = 10
lr = 0.1
for epoch in range(epochs):
    for X,y in data_iter(batch_size,features,labels):
        y_hat = linreg(X,w,b)
        loss = square_loss(y_hat,y)
        loss.sum().backward()
        sgd([w,b],lr,batch_size)
    with torch.no_grad():
        train_l = square_loss(linreg(features,w,b),labels)
        print(f"epoch {epoch+1},loss {float(train_l.mean())}")

最终运行结果:在这里插入图片描述
如果想进一步减小误差可以增加训练的次数也可以适当的增加学习率。

二、简洁写法
所谓的优雅一点的写法就是使用torch封装好的库,直接调用相关的库就行了
代码如下

# 导入库
import numpy as np
import torch
from torch.utils import data
# 真实参数的值
w_true = torch.tensor([2,-3.4])
b_true = 4
# 合成数据
def synthetic_data(w,b,num_examples):
    X = torch.normal(0,1,(num_examples,len(w)))
    y = torch.matmul(X,w) + b
    y += torch.normal(0,0.001,y.shape)
    return X,y.reshape(-1,1)
features,labels = synthetic_data(w_true,b_true,1000)
# 使用data生成数据迭代器
def load_array(data_arrays,batch_size,is_train=True):
    dataset = data.TensorDataset(*data_arrays)
    return data.DataLoader(dataset,batch_size,shuffle=is_train)

batch_size = 10
data_iter = load_array((features,labels),batch_size)

next(iter(data_iter))

# 网络模型,使用Sequential将模型放在容器里面
from torch import nn
net = nn.Sequential(nn.Linear(2,1))

# 容器里面的第一个就是线性模型层
# 初始化网络参数
net[0].weight.data.normal_(0,0.01)
net[0].bias.data.fill_(0)

# 使用均方误差
loss = nn.MSELoss()

# SGD优化方法
trainer = torch.optim.SGD(net.parameters(),lr = 0.03)

num_epochs = 3
for epoch in range(num_epochs):
    for X,y in data_iter:
        l = loss(net(X),y)
        trainer.zero_grad()
        l.backward()
        trainer.step()
    
    l = loss(net(features),labels)
    print(f"epoch{epoch+1},loss{l:f}")

总结:计算误差, 将优化器梯度置为0(因为pytorch梯度是默认累加的,因此每次需要将他手动置为0),误差反向传播,优化器更新参数

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
PyTorch是一个基于Python的科学计算包,主要针对两类人群:第一类是使用NumPy进行科学计算的人群,第二类是使用GPU进行科学计算的人群。 PyTorch可以用于实现各种机器学习算法,其中包括线性回归线性回归是一种最简单的机器学习算法,用于预测连续值的输出。 下面是一个使用PyTorch实现线性回归的示例代码: ``` import torch import torch.nn as nn import numpy as np import matplotlib.pyplot as plt # 定义数据集 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) # 定义模型 class LinearRegression(nn.Module): def __init__(self): super(LinearRegression, self).__init__() self.linear = nn.Linear(1, 1) def forward(self, x): out = self.linear(x) return out model = LinearRegression() # 定义损失函数和优化器 criterion = nn.MSELoss() optimizer = torch.optim.SGD(model.parameters(), lr=0.01) # 训练模型 num_epochs = 1000 for epoch in range(num_epochs): inputs = torch.from_numpy(x_train) targets = torch.from_numpy(y_train) # 前向传播 outputs = model(inputs) loss = criterion(outputs, targets) # 反向传播和优化 optimizer.zero_grad() loss.backward() optimizer.step() if (epoch+1) % 100 == 0: print ('Epoch [{}/{}], Loss: {:.4f}'.format(epoch+1, num_epochs, loss.item())) # 可视化结果 predicted = model(torch.from_numpy(x_train)).detach().numpy() plt.plot(x_train, y_train, 'ro', label='Original data') plt.plot(x_train, predicted, label='Fitted line') plt.legend() plt.show() ``` 这个示例代码中,我们首先定义了一个数据集,包括15个样本,每个样本包括一个输入特征和一个输出标签。然后,我们定义了一个线性回归模型,并定义了损失函数和优化器。接下来,我们训练模型,使用随机梯度下降算法来更新模型参数,直到达到预定的训练轮数。最后,我们可视化了模型的拟合结果。 在这个示例代码中,我们使用了PyTorch的自动微分功能来计算梯度,这使得训练模型变得非常容易。同时,PyTorch还提供了许多高级功能,如自定义损失函数和优化器,使得我们可以进一步优化模型的性能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

leoliyao

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值