《Pytorch深度学习实践》ch5-Logistic回归
------B站《刘二大人》
1.Classification
- 经典的分类数据集:MNIST(0 - 9)
- 导入数据集:(路径,训练集/测试集,是否下载)
import torchvision
train_set = torchvision.datasets.MINIST(root='../dataset/mnist', train=True, download=True)
test_set = torchvision.datasets.MINIST(root='../dataset/mnist', train=False, download=True)
2.Sigmoid functions
- 由于分类问题就是求概率的最大值,所以利用 S 函数将数值全部映射到 [0,1] 区间;
- 最著名的就是这个 Logistic 函数:
- 其它的 S 函数:
3.Logistic Regression Model
- 就是在原函数基础上加一个 Sigmoid:
4.Loss and BCE
- BCE:Binary Cross Entropy,二元交叉熵损失:
5.Implemetation
- 导包:
import torch
import torch.nn.functional as F
- 数据集:y 变为 {0,1},二分类
# 数据集
x_data = torch.Tensor([[1.0], [2.0], [3.0]])
y_data = torch.Tensor([[0], [0], [1]])
- 模型:F.sigmoid()函数
# 模型
class LogisticRegressionModel(torch.nn.Module): # 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))return y_predmodel = LogisticRegressionModel() # 实例化
- 损失和优化器:BCELoss
# 损失函数和优化器
criterion = torch.nn.BCELoss(reduction = 'sum') # 计算损失,参数为(y_pred, y)optimizer = torch.optim.SGD(model.parameters(), lr = 0.01) # 进行更新
- 训练:
# 训练
for epoch in range(1000):y_pred = model(x_data)loss = criterion(y_pred, y_data) # 1.前馈print(epoch, loss)optimizer.zero_grad() # 梯度清零loss.backward() # 2.反馈optimizer.step() # 3.更新
6.Result
import numpy as np
import matplotlib.pyplot as pltx = np.linspace(0, 10, 200)
x_t = torch.Tensor(x).view((200,1)) # 将x数组转换为PyTorch张量,并将其形状调整为列向量(200x1)
y_t = model(x_t)
y = y_t.data.numpy() # 将输出张量y_t转换为NumPy数组yplt.plot(x, y)
plt.plot([0, 10], [0.5, 0.5], c='r') # 绘制一条从x=0到x=10的红色水平线,y值为0.5
plt.xlabel('Hours')
plt.ylabel('Probability of Pass')
plt.grid()
plt.show()
- 绘图如下: