Logistic Regression 逻辑斯蒂回归

5、Logistic Regression 逻辑斯蒂回归

B站视频教程传送门:PyTorch深度学习实践 - 逻辑斯蒂回归

5.1 回归任务

在开始学习逻辑斯蒂回归之前,我们先简单复习一下之前学过的线性回归

并且我们使用的数据集也非常的简单:

当我们使用复杂的数据集时,比如 MNIST Dataset 或 CIFAR-10 dataset,就涉及到了分类的问题。

5.1.1 MNIST Dataset

这是一个手写数字的数据库,来源于 MNIST Dataset

  • 训练集:60000个样本
  • 测试集:10000个样本
  • 类:10(0,1,2,…,9)

可通过以下方式进行下载并使用:

import torchvision
train_set = torchvision.datasets.MNIST(root='../dataset/mnist', train=True, download=True)
test_set = torchvision.datasets.MNIST(root='../dataset/mnist', train=False, download=True
5.1.2 CIFAR-10 Dataset

这是一个各种图像的数据库,来源于 CIFAR-10 Dataset

  • 训练集:50000个样本
  • 测试集:10000个样本
  • 类:10(airplane,automobile,bird,…)

可通过以下方式进行下载并使用:

import torchvision
train_set = torchvision.datasets.CIFAR10(root='../dataset/cifar10', train=True, download=True)
test_set = torchvision.datasets.CIFAR10(root='../dataset/cifar10', train=False, download=True

5.2 Regression vs Classification 回归 vs 分类

回归: y ∈ R 连续的空间

逻辑斯蒂回归: 主要是做分类的,估算 y 属于哪一个类别,不是让 y 等于某一个特定值(因为类别无法进行比较),而是估算其属于每个分类的概率,概率较大的则是分类的结果。

二分类: 只有两个类别的分类问题,且 P(y = 1) + P(y = 0) = 1

想计算概率属于[0, 1],而不是实数,可以使用 sigmod() 函数将实数空间映射到[0, 1]之间。

5.3 Sigmoid functions

5.3.1 Logistic Function [0, 1]

维基百科 - Logistic Function:https://en.wikipedia.org/wiki/Logistic_function

5.3.2 Other Functions [-1, 1]

5.4 Model 模型

Linear Model:

class LinearModel(torch.nn.Module):
    def __init__(self):
        super(LinearModel, self).__init__()
        self.linear = torch.nn.Linear(1, 1)

    def forward(self, x):
        y_pred = self.linear(x)
        return y_pred

Logistic Regression Model:

class LogisticRegressionModel(torch.nn.Module):
    def __init__(self):
        super(LogisticRegressionModel, self).__init__()
        self.linear = torch.nn.Linear(1, 1)

    def forward(self, x):
        y_pred = F.sigmoid(self.linear(x)) # 注意:使用F.sigmoid()会报错,可替换为torch.sigmoid()
        return y_pred
5.5.1 torch.sigmoid()、torch.nn.Sigmoid()、torch.nn.functional.sigmoid()

当导入 torch.nn.functional as F 并使用 F.sigmoid() ,会报错 UserWarning: nn.functional.sigmoid is deprecated. Use torch.sigmoid instead.。表明 nn.functional.sigmoid 已经被弃用,如果需要可以使用 torch.sigmoid

我们来看一下3种 sigmoid() 函数的区别:

1、torch.sigmoid() 函数

2、torch.nn.Sigmoid()

3、torch.nn.functional.sigmoid() 函数

5.5 Loss Function 损失函数

Loss function for (Linear Regression - Binary Classification) 如下图所示:

Mini-Batch Loss function for Binary Classification 如下图所示:

# 1、Linear Model 线性模型
criterion = torch.nn.MSELoss(reduction='sum')

#2、Logistic Regression Model 逻辑斯蒂回归模型
criterion = torch.nn.BCELoss(reduction='sum')

注意1、在函数 MSELossBCELoss 中,参数 size_averagereduce 均已被弃用:

注意2、MSELossBCELoss 的区别:

  • MSELoss Mean Squared Error Loss 均方误差损失

  • BCELoss Binary Cross Entropy Loss 二元交叉熵损失

5.6 Implementation 实施

在具体代码实现之前,我们需要导入所需要的包:

import torch
import numpy as np
import matplotlib.pyplot as plt
from torch import nn
5.6.1 Prepare Dataset 准备数据集
x_data = torch.Tensor([[1.0], [2.0], [3.0]])
y_data = torch.Tensor([[0], [0], [1]])
5.6.2 Design Model 设计模型
class Liang(nn.Module):
    def __init__(self):
        super(Liang, self).__init__()
        self.linear = nn.Linear(1, 1)

    def forward(self, x):
        y_pred = torch.sigmoid(self.linear(x))
        return y_pred


model = Liang()
5.6.3 Construct Loss and Optimizer 构造损失和优化器
criterion = torch.nn.BCELoss(reduction='sum')
optimizer = torch.optim.SGD(model.parameters(), lr=0.01)
5.6.4 Training Cycle 训练周期
for epoch in range(1000):
    y_pred = model(x_data)
    loss = criterion(y_pred, y_data)
    print(epoch, loss.item())

    optimizer.zero_grad()
    loss.backward()
    optimizer.step()
5.6.5 Test Model 测试模型
# Output weight and bias
print('w = ', model.linear.weight.item())
print('b = ', model.linear.bias.item())

# Test
x_test = torch.Tensor([[4.0]])
y_test = model(x_test)
print('y_pred = ', y_test.item())
5.6.6 绘图
x = np.linspace(0, 10, 200)  # 返回0~10等间距的200个数
x_t = torch.Tensor(x).view((200, 1))  # 200行1列的Tensor
y_t = model(x_t)
y = y_t.data.numpy()  # 将torch.Tensor转换为numpy.ndarray

plt.plot(x, y)
plt.plot([0, 10], [0.5, 0.5], color='red')  # 画线,x取0~10,y取0.5
plt.xlabel('Hours')
plt.ylabel('Probability of Pass')
plt.grid()  # 显示网格线(1或True 默认显示;0或False 不显示)
plt.show()
5.6.7 完整代码
import torch
import numpy as np
import matplotlib.pyplot as plt
from torch import nn

# Prepare dataset
x_data = torch.Tensor([[1.0], [2.0], [3.0]])
y_data = torch.Tensor([[0], [0], [1]])


# Design model
class Liang(nn.Module):
    def __init__(self):
        super(Liang, self).__init__()
        self.linear = nn.Linear(1, 1)

    def forward(self, x):
        y_pred = torch.sigmoid(self.linear(x))
        return y_pred


model = Liang()

# Construct loss and optimizer
criterion = torch.nn.BCELoss(reduction='sum')
optimizer = torch.optim.SGD(model.parameters(), lr=0.01)

# Training cycle
for epoch in range(1000):
    y_pred = model(x_data)
    loss = criterion(y_pred, y_data)
    print(epoch, loss.item())

    optimizer.zero_grad()
    loss.backward()
    optimizer.step()

# Output weight and bias
print('w = ', model.linear.weight.item())
print('b = ', model.linear.bias.item())

# Test Model
x_test = torch.Tensor([[4.0]])
y_test = model(x_test)
print('y_pred = ', y_test.item())

# Result
x = np.linspace(0, 10, 200)  # 返回0~10等间距的200个数
x_t = torch.Tensor(x).view((200, 1))  # 200行1列的Tensor
y_t = model(x_t)
y = y_t.data.numpy()  # 将torch.Tensor转换为numpy.ndarray

plt.plot(x, y)
plt.plot([0, 10], [0.5, 0.5], color='red')  # 画线,x取0~10,y取0.5
plt.xlabel('Hours')
plt.ylabel('Probability of Pass')
plt.grid()  # 显示网格线(1或True 默认显示;0或False 不显示)
plt.show()
0 2.7373640537261963
1 2.693269729614258
2 2.651460647583008
...
995 1.0572519302368164
996 1.0567638874053955
997 1.0562764406204224
998 1.055789589881897
999 1.0553032159805298
w =  1.1830791234970093
b =  -2.858741283416748
y_pred =  0.8668714165687561

  • 4
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

_leoatliang

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

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

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

打赏作者

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

抵扣说明:

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

余额充值