Logistic Regression 逻辑斯蒂回归

简介: 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、在函数 MSELoss 或 BCELoss 中,参数 size_average 和 reduce 均已被弃用:

注意2、MSELoss 和 BCELoss 的区别:

  • 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

目录
相关文章
什么时候使用PUT?什么时候使用POST?具体使用场景是什么?
什么时候使用PUT?什么时候使用POST?具体使用场景是什么?
1949 0
|
机器学习/深度学习 自然语言处理 算法
文本分析-使用jieba库进行中文分词和去除停用词(附案例实战)
文本分析-使用jieba库进行中文分词和去除停用词(附案例实战)
11992 145
匹配中文字符的正则表达式: [/u4e00-/u9fa5]
原文:匹配中文字符的正则表达式: [/u4e00-/u9fa5] 这里是几个主要非英文语系字符范围(google上找到的): 2E80~33FFh:中日韩符号区。收容康熙字典部首、中日韩辅助部首、注音符号、日本假名、韩文音符,中日韩的符号、标点、带圈或带括符文数字、月份,以及日本的假名组合、单位、年号、月份、日期、时间等。
3296 0
|
传感器 人工智能 API
仅100多元,他给视障人群装上AI“眼睛”
上海两名开发者为验证AI助盲实效,亲手打造百元AI眼镜,蒙眼实测过马路、识盲道,并开源项目鼓励更多人参与。技术导航,人心照亮。
2337 6
仅100多元,他给视障人群装上AI“眼睛”
|
7月前
|
安全 Java 编译器
【Java基础】面向对象三大特性、重载 vs 重写、接口 vs 抽象类(附《面试背诵版》+《自测题》)
本文系统梳理Java面向对象核心知识:详解封装、继承、多态三大特性及实现要点;清晰对比重载(编译时多态)与重写(运行时多态)的语法与语义差异;深入剖析抽象类(is-a、代码复用)与接口(can-do、多实现)的本质区别;涵盖面试高频考点、典型易错点及速记口诀,助你高效掌握并应对技术面试。
|
存储 弹性计算 固态存储
阿里云服务器按量付费与包年包月收费标准,云服务器最新活动价格参考
阿里云的价格一直是用户最为关注的,阿里云这两年也多次做了降价策略,阿里云根据用户的实际需求,针对云服务器收费模式推出按量付费与包年包月两种收费模式,云服务器价格表2025最新版,轻量应用服务器38元一年起,ECS云服务器2核2G3M带宽99元1年、2核4G5M带宽199元一年,新品通用算力型u2i实例4核4G1120.44元/1年起、4核8G1170.26元/1年起,九代云服务器计算型c9i实例8核16G6037.01元/1年起、通用型g9i实例8核32G7551.94元/1年起、内存型r9i实例8核64G9937.12元/1年起。叠加阿里云推出的各种优惠券还可享受满减优惠。
|
算法 Go 计算机视觉
YOLOv8结构解读
YOLOv8是Ultralytics团队开发的最新目标检测模型,基于YOLO系列的高效单阶段架构,进一步优化了精度与速度。相比YOLOv5,YOLOv8在Backbone中移除了Focus模块,优化了CSP结构;在Neck部分增强了特征融合能力;Head部分采用Anchor-Free与解耦头设计,提升检测精度。此外,YOLOv8引入了更先进的损失函数、自对抗训练和动态标签分配等策略,显著提高了模型性能与鲁棒性。
3783 0
|
机器学习/深度学习 Linux PyTorch
Dataset and DataLoader 加载数据集
Dataset and DataLoader 加载数据集
560 0
|
并行计算 异构计算
卸载原有的cuda,更新cuda
本文提供了一个更新CUDA版本的详细指南,包括如何查看当前CUDA版本、检查可安装的CUDA版本、卸载旧版本CUDA以及安装新版本的CUDA。
18197 3
卸载原有的cuda,更新cuda