当前位置:   article > 正文

深度学习笔记(1)——pytorch实现线性回归_pytorch线性回归代码

pytorch线性回归代码

pytorch实现一元线性,回归代码如下:

# pytorch实现一元线性回归
import numpy as np
import matplotlib.pyplot as plt
import torch
from torch import nn, optim
from torch.autograd import Variable

# 定义数据集
x_data = np.random.rand(100)
noise = np.random.normal(0, 0.01, x_data.shape)  # 噪音
y_data = x_data * 2 + noise + 1
plt.scatter(x_data, y_data)
plt.rcParams['font.sans-serif'] = 'SimHei'  # 设置中文
plt.title('线性回归')
plt.xlabel('x')
plt.ylabel('y')
plt.xlim([0, 1.0])
plt.ylim([0, 3.0])
plt.grid()
plt.show()

x_tensor = torch.FloatTensor(x_data.reshape(-1, 1))
y_tensor = torch.FloatTensor(y_data.reshape(-1, 1))
# print(x_tensor)
# print(y_tensor)
inputs = Variable(x_tensor)
target = Variable(y_tensor)
print(inputs)
print(target)


# 构建神经网络模型
class LinearRegression(nn.Module):
    def __init__(self):
        super(LinearRegression, self).__init__()
        self.fc = nn.Linear(1, 1)

    def forward(self, x):
        return self.fc(x)


model = LinearRegression()
gpu = torch.device('cuda')
mse_loss = nn.MSELoss()  # 均方差损失函数
optimizer = optim.SGD(model.parameters(), lr=0.01)  # 随机梯度下降法


def train():
    for i in range(20001):
        out = model(inputs)  # 计算模型输出结果
        loss = mse_loss(out, target)  # 损失函数
        optimizer.zero_grad()  # 梯度清零
        loss.backward()  # 计算梯度
        optimizer.step()  # 修改权值

        if i % 100 == 0:
            print(i, loss.item(), sep='\t')


train()

# 查看模型效果
y_predict = model(inputs)
plt.scatter(x_data, y_data)
plt.plot(x_data, y_predict.data.numpy(), 'r-', lw=3)
plt.title('神经网络拟合线性回归')
plt.xlabel('x')
plt.ylabel('y')
plt.xlim([0, 1.0])
plt.ylim([0, 3.0])
plt.grid()
plt.show()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72

效果如图:
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
pytorch实现多元线性回归(以波士顿房价数据集为例),代码如下:

# pytorch多元线性回归
import torch
from torch import nn, optim
from torch.autograd import Variable
from sklearn.datasets import load_boston

# 定义数据集
boston_dataset = load_boston()
print(boston_dataset['data'].shape)  # (506,13)
print(boston_dataset['target'].shape)  # (506,)
x_data = boston_dataset['data']
y_data = boston_dataset['target']

x_tensor = torch.FloatTensor(x_data)
y_tensor = torch.FloatTensor(y_data.reshape(-1, 1))
inputs = Variable(x_tensor)
target = Variable(y_tensor)


# 构建神经网络模型
class LinearRegression(nn.Module):
    def __init__(self):
        super(LinearRegression, self).__init__()
        self.layer1 = nn.Linear(13, 1)

    def forward(self, x):
        return self.layer1(x)


model = LinearRegression()
gpu = torch.device('cuda')
mse_loss = nn.MSELoss()
optimizer = optim.Adam(model.parameters(), lr=0.003)


def train():
    for i in range(1001):
        out = model(inputs)  # 计算模型输出结果
        loss = mse_loss(out, target)  # 损失函数
        optimizer.zero_grad()  # 梯度清零
        loss.backward()  # 计算权值
        optimizer.step()  # 修改权值

        if i % 20 == 0:
            print(i, loss.item(), sep='\t')


train()  # 训练


for parameter in model.parameters():
    print(parameter)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52

效果如下:
在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/103729
推荐阅读
相关标签
  

闽ICP备14008679号