【pytorch】简单的线性回归模型

28 篇文章 0 订阅
21 篇文章 0 订阅

前言

回归是一种能更加多个变量之间的关系进行建模的一种方法,其在机器学习中有着官方运用。线性回归是其中最最最最简单的一种,其假设自变量与因变量之间是线性关系。利用pytorch就可以简单地写出线性回归的代码。

线性回归

首先需要知道线性回归的基本假设:

  1. 自变量和因变量之间是线性关系,并且允许存在些噪声。
  2. 存在的噪声都是比较“正常”的,符号正态分布。
    因此可以用一个简单是式子来表示这个模型。
    y ^ = w T x + b \hat{y} = w^Tx+b y^=wTx+b
    其中w,x均是列向量。
    对于这个简单的问题,可以用数学的方法,直接求出解析解,最常见的就说最小二乘法。
    但是,因为线性回归模型过于简单,才可以这样求出答案,对于其他的模型,是不可能的,因此在这里同样是使用随机梯度下降法。

数据集

这种简单的数据集,并不需要去哪里下载,直接自行生成即可。

def data_maker(w, b, n_size): # y=w*x+b,n个数据
    X = torch.normal(0 , 1 , (n_size , len(w))) # n*len(w)的参数
    y = torch.matmul(X , w) + b
    y = y + torch.normal(0, 0.01 , y.shape)
    return X , y.reshape((-1 , 1))

读取

不失一般性,一般都是读取一个batch的,为了方便,可以利用yield将其写成一个迭代器。

def data_iter(batch_size , x , y):
    n = len(x)
    index = list(range(n))
    random.shuffle(index)
    for i in range(0 , n , batch_size):
        batch_index = torch.tensor(index[i:min(i + batch_size,n)])
        yield x[batch_index], y[batch_index]

模型

为了更好的理解,此处使用自己定义的函数。
但是需要用到pytorch的自动求梯度。

def linreg(X,w,b):
    return torch.matmul(X , w) + b

def loss_function(Y , y):
    return (Y - y.reshape(Y.shape))**2/2

def SGD(params , learning_rate , batch_size):
    with torch.no_grad():
        for param in params:
            param -= learning_rate * param.grad / batch_size
            param.grad.zero_()

训练

这里和普通的模型差不多

#training
for epoch in range(num_epochs):
    for  X ,Y in data_iter(batch_size , x , y):
        l = loss(net(X , w,b), Y)
        l.sum().backward()
        SGD([w,b] , 0.001 , batch_size)
    with torch.no_grad():
        train_loss = loss(net(x , w , b), y)
        print(f'epoch{epoch+1} , loos {float(train_loss.mean())}')

完整代码

#!/usr/bin/env python
# coding: utf-8

# In[1]:


import torch
import random


# In[2]:


def data_maker(w, b, n_size): # y=w*x+b,n个数据
    X = torch.normal(0 , 1 , (n_size , len(w))) # n*len(w)的参数
    y = torch.matmul(X , w) + b
    y = y + torch.normal(0, 0.01 , y.shape)
    return X , y.reshape((-1 , 1))


# In[3]:


W = torch.tensor([4.0])
B = 1
x , y = data_maker(W, B, 1000)


# In[4]:


def data_iter(batch_size , x , y):
    n = len(x)
    index = list(range(n))
    random.shuffle(index)
    for i in range(0 , n , batch_size):
        batch_index = torch.tensor(index[i:min(i + batch_size,n)])
        yield x[batch_index], y[batch_index]


# In[5]:


def linreg(X,w,b):
    return torch.matmul(X , w) + b

def loss_function(Y , y):
    return (Y - y.reshape(Y.shape))**2/2

def SGD(params , learning_rate , batch_size):
    with torch.no_grad():
        for param in params:
            param -= learning_rate * param.grad / batch_size
            param.grad.zero_()


# In[6]:


batch_size = 5
num_epochs = 50
net = linreg
loss = loss_function
w = torch.normal(0,1 , size=(1,1) , requires_grad= True)
b = torch.normal(0,1 , size=(1,1) , requires_grad= True)


# In[7]:


#training
for epoch in range(num_epochs):
    for  X ,Y in data_iter(batch_size , x , y):
        l = loss(net(X , w,b), Y)
        l.sum().backward()
        SGD([w,b] , 0.001 , batch_size)
    with torch.no_grad():
        train_loss = loss(net(x , w , b), y)
        print(f'epoch{epoch+1} , loos {float(train_loss.mean())}')


# In[8]:


print(w , b)


  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值