Pytorch 深度学习实践Lecture

Pytorch 深度学习实践Lecture,第1张

up主 刘二大人

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

线性回归

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

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

仿射模型

线性回归模型的仿射模型是在线性模型基础上添加了激活函数, 可以将预测值映射到(-1, 1)区间内。

损失

线性模型的MSE loss:

        

线性回归模型BCE loss: 

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

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

预测模型
二分类问题
x(learnning hours)y(pass/fail)
10(fail)
20(fail)
31(pass)
4?

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

import torch
import matplotlib.pyplot as plt

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):
        """
        import torch.nn.functional as F
        y_pred = F.sigmoid(self.linear(x))
        替换成
        y_pred = torch.sigmoid(self.linear(x))
        """
        y_pred = torch.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 是torch.FloatTensor类型, 导致计算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)
    print(y_pred)
    loss = criterion(y_pred, y_data)
    print(epoch, loss)
    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]])

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/733707.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-27
下一篇 2022-04-27

发表评论

登录后才能评论

评论列表(0条)

保存