Pytorch 深度学习实践Lecture_6 Logistic Regression

up主 刘二大人

视频链接 刘二大人的个人空间_哔哩哔哩_Bilibili

线性回归

线性模型 预测:连续实数值

线性回归模型 预测: 离散值(分类)
 

仿射模型

线性回归模型的仿射模型是在线性模型基础上添加了激活函数\sigma = \frac{1}{1+e^{-x}}, 可以将预测值映射到(-1, 1)区间内。

Pytorch 深度学习实践Lecture_6 Logistic Regression_第1张图片

损失

线性模型的MSE loss:

        loss = (\hat{y}-y)^{2} = (x*w-y)^2

线性回归模型BCE loss: 

loss = -(y\log{\hat{y}}+(1-y)\log{(1-\hat{y})})

PS:  线性模型loss 计算的是一个数轴上两个实数值的距离;

        线性回归模型loss 计算的是分布的差异, 计算方法有:KL散度,cross-entropy交叉熵(BCE)

预测模型

二分类问题
x(learnning hours) y(pass/fail)
1 0(fail)
2 0(fail)
3 1(pass)
4 ?

torch.Tensor和torch.tensor有什么区别?_EdisonLeejt的博客-CSDN博客

import torch
import matplotlib.pyplot as plt
import torch.nn.functional as F

class LogisticRegressionModel(torch.nn.Module):
    """
    __init_ and forward function have to be implemented
    """

    def __init__(self):
        super(LogisticRegressionModel, self).__init__()
        # torch.nn.Linear(in_features_size: int, out_features_size: int)
        self.linear = torch.nn.Linear(1, 1)

    def forward(self, x):
        y_pred = F.sigmoid(self.linear(x))
        return y_pred

"""
使用torch.tensor 报错: RuntimeError: Found dtype Long but expected Float
x_data = torch.tensor(...)
y_data = torch.tensor(...)
原因是y_pred 是float类型, 计算loss时报错
替换成
x_data= torch.Tensor(...)
y_data= torch.Tensor(...)
"""
x_data = torch.Tensor([[1.0], [2.0], [3.0]])
y_data = torch.Tensor([[0], [0], [1]])
model = LogisticRegressionModel()

"""
criterion = torch.nn.BCELoss(size_average=False)
替换成
criterion = torch.nn.BCELoss(reduction='sum')
"""
criterion = torch.nn.BCELoss(reduction='sum')
# model.parameters(): get all parameters of current model
# lr: learning rate
optimizer = torch.optim.SGD(model.parameters(), lr=0.01)
epoch_list = []
loss_list = []
for epoch in range(1000):
    y_pred = model(x_data)
    loss = criterion(y_pred, y_data)
    print(epoch, loss.item())
    epoch_list.append(epoch)
    loss_list.append(loss.item())

    optimizer.zero_grad()
    loss.backward()
    optimizer.step()

print("w=", model.linear.weight.item())
print("b=", model.linear.bias.item())
plt.plot(epoch_list, loss_list)
plt.xlabel('epoch')
plt.ylabel('Loss')
plt.show()

x_test = torch.Tensor([[4.0]])
y_test = model(x_test)
print('y_pred=', y_test.data)

结果显示

y_pred= tensor([[0.8596]])

Pytorch 深度学习实践Lecture_6 Logistic Regression_第2张图片

你可能感兴趣的:(Pytorch深度学习实践,pytorch)