import torch
import torch.nn as nn
import numpy as np
import matplotlib.pyplot as plt
# 逻辑回归(Logistic Regression)
# LR是线性的二分类模型
# y = f(wx+b) f(x) = 1/(1+e**-x) f(x)也称为Sigmoid函数或者Logistic函数
# LR作用:将输入数据映射到[0,1]
# 过程量分类采用“四舍五入,实现整体二分类
# 线性回归是分析自变量x与标量y之间的关系的方法
# LR是分析自变量x与概率y之间的关系的方法
# LR也可以通过变式生成“对数几率回归模型” ln(y/1-y) = wx+b
# 机器学习步骤:data(采集、清洗、划分、预处理)、model、Loss、optim
# LR案例
# data
sample_num = torch.tensor([100.], requires_grad=True)
mean_value = 1.7
bias = 1
n_data = torch.normal(sample_num, 2)
x0 = torch.normal(mean_value * n_data, 1) + bias # 正态分布生成
y0 = torch.zeros(100)
x1 = torch.normal(-mean_value * n_data, 1) + bias
y1 = torch.ones(100)
train_x = torch.cat((x0, x1), 0) # 拼接变量
train_y = torch.cat((y0, y1), 0)
# model
class LR(nn.Module):
def __init__(self):
super(LR, self).__init__()
self.features = nn.Linear(2, 1)
self.sigmoid = nn.Sigmoid()
def forward(self, x):
x = self.features(x)
x = self.sigmoid(x)
# 实例化LR
LR = LR()
# Loss
loss_fn = nn.BCELoss()
# optim
lr = 0.01 # 学习率
optimizer = torch.optim.SGD(LR.parameters(), lr=lr, momentum=0.9)
# train
for iteration in range(1000):
y_pred = LR(train_x)
loss = loss_fn(y_pred.squeeze(), train_y)
loss.backward()
optimizer.step()
# 绘图
if iteration % 20 == 0:
mask = y_pred.ge(0.5).float().squeeze() # 0.5为阈值进行分类
correct = (mask == train_y).sum() # 正确分类数
acc = correct.item() / train_y.size(0) # 精度
plt.scatter(x0.data.numpy()[:, 0], x0.data.numpy()[:, 1], c='r', label='class 0')
plt.scatter(x1.data.numpy()[:, 0], x1.data.numpy()[:, 1], c='b', label='class 1')
w0, w1 = LR.features.weight[0]
w0, w1 = float(w0.item()), float(w1.item())
plot_b = float(LR.features.bias[0].item())
plot_x = np.arange(-6, 6, 0.1)
plot_y = (-w0 * plot_x - plot_b) / w1
plt.xlim(-5, 7)
plt.xlim(-5, 7)
plt.plot(plot_x, plot_y)
plt.legend()
plt.show()
plt.pause(0.5)
if acc >= 0.99: break
torch学习笔记(6)——逻辑回归模型(自训练)
最新推荐文章于 2023-07-21 12:50:37 发布