美文网首页
class6- Logistic Regression

class6- Logistic Regression

作者: 学了忘了学 | 来源:发表于2022-10-06 12:49 被阅读0次

回顾:Linear Regression

Linear Regression

Regression vs Classification

首先,我们处理二分类问题。由于分成两类,我们便让其中一类标签为0,另一类为1。我们需要一个函数,对于输入的每一组数据都能映射成0~1之间的数。并且如果函数值大于0.5,就判定属于1,否则属于0。

map

Sigmoid functions

有很多Sigmoid functions针对不同的场景进行选择。


Sigmoid functions

Logistic Regression Model

Logistic Regression Model

这里通过sigmoid函数将每一个y hat都转换成0-1之间的数。

损失函数

损失函数

对于二分类问题,只有0和1两个选项,那么:

  • 当y=0时,loss = -log(1-yhat),yhat趋近于0时,loss越小
  • 当y=1时,loss =-ylog(yhat),yhat趋近于1时,loss越小

Mini-Batch Loss function for Binary Classification

BCEloss

Implementation of Logistic Regression


对比于线性回归,只增加了sigmoid函数。

代码实现

import torch
# import torch.nn.functional as F
 
# prepare dataset
x_data = torch.Tensor([[1.0], [2.0], [3.0]])
y_data = torch.Tensor([[0], [0], [1]])
 
#design model using class
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))
        y_pred = torch.sigmoid(self.linear(x))
        return y_pred
model = LogisticRegressionModel()
 
# construct loss and optimizer
# 默认情况下,loss会基于element平均,如果size_average=False的话,loss会被累加。
criterion = torch.nn.BCELoss(size_average = False) 
optimizer = torch.optim.SGD(model.parameters(), lr = 0.01)
 
# training cycle forward, backward, update
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()
 
print('w = ', model.linear.weight.item())
print('b = ', model.linear.bias.item())
 
x_test = torch.Tensor([[4.0]])
y_test = model(x_test)
print('y_pred = ', y_test.data)

测试

import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0, 10, 200)
x_t = torch.Tensor(x).view((200, 1))
y_t = model(x_t)
y = y_t.data.numpy()
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()
结果

参考

PyTorch 深度学习实践 第6讲_错错莫的博客-CSDN博客

相关文章

网友评论

      本文标题:class6- Logistic Regression

      本文链接:https://www.haomeiwen.com/subject/befbartx.html