一、目的
用pytorch实现逻辑回归,并理解什么是逻辑回归
二、编程
相比于线性模型,逻辑回归在线性模型后面加入了非线性激活函数,使用了不同的损失函数(交叉熵);
关于逻辑回归可以参考我的这篇文章——001_wz_sf_逻辑回归(Logistic Regression),我们主要看如何使用pytorch实现
import numpy as np
import matplotlib.pyplot as plt
import torch
# 数据集
x_data = torch.Tensor([[1.0], [2.0], [3.0]])
y_data = torch.Tensor([[0], [0], [1]])
# 构造模型实现正向传播T
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 = torch.sigmoid(self.linear(x))
return y_pred
# 实例化模型
model = LogisticRegressionModel()
# 构造损失函数
criterion = torch.nn.BCELoss(reduction="sum")
# 构造优化器
optimizer = torch.optim.SGD(model.parameters(), lr=0.01)
# 开始训练
costs = []
for epoch in range(1000):
# 正向传播
y_pred = model(x_data)
# 计算损失
loss = criterion(y_pred, y_data)
costs.append(loss.item())
print("epoch=", epoch, ",cost=", loss.item())
# 反向传播
optimizer.zero_grad()
loss.backward()
# 更新参数
optimizer.step()
print("w=", model.linear.weight.item())
print("b=", model.linear.bias.item())
# 预测
x_test = torch.Tensor([[4.0]])
print("x=4,y=", model(x_test).item())
# 生成1-10之间的等差数列,200个数
x = np.linspace(1, 10, 200)
x_t = torch.Tensor(x).reshape(200, 1)
y_t = model(x_t)
y = np.array(y_t.item())
plt.plot(x, y)
plt.plot([0, 10], [0.5, 0.5], c="r")
plt.xlabel("hours")
plt.ylabel("probability of pass")
# 画出网络
plt.grid()
plt.show()
epoch= 1 ,cost= 2.7252180576324463
epoch= 2 ,cost= 2.6986937522888184
epoch= 3 ,cost= 2.673819065093994
...
epoch= 997 ,cost= 1.069270372390747
epoch= 998 ,cost= 1.0687689781188965
epoch= 999 ,cost= 1.068267822265625
w= 1.1604176759719849
b= -2.804975748062134
x=4,y= 0.8625572919845581
训练后的预测图