(刘二大人)PyTorch深度学习实践-Logistic回归实现

1.代码实现(lr=0.01)

import numpy as np
import torch
import torch.nn.functional as F
from torch.utils.tensorboard import SummaryWriter

#使用tensorboard进行记录
writer = SummaryWriter(log_dir='../LEDR')

#准备数据集
x_data = torch.Tensor([[1.0],[2.0],[3.0]])
y_data = torch.Tensor([[0],[0],[1]])#二分类问题,预测的是种类数

#搭建网络
class LogisticModel(torch.nn.Module):
    def __init__(self):
        super(LogisticModel, self).__init__()
        self.linear = torch.nn.Linear(1,1)

    def forward(self,x):
        y_pred = F.sigmoid(self.linear(x))#在线性回归后再使用一个逻辑回归函数
        return y_pred

model = LogisticModel()

#损失和优化函数
criterion = torch.nn.BCELoss(size_average=False)
optimizer = torch.optim.SGD(params=model.parameters(),lr = 0.01)

#进行训练
for epoch in range(1000):
    y_pred = model.forward(x_data)
    #计算、打印损失
    l = criterion(y_pred,y_data)
    writer.add_scalar('LogisticLoss1',l.item(),epoch)
    print("Epoch:",epoch,l.item())

    #梯度归零、反馈、更新
    optimizer.zero_grad()
    l.backward()
    optimizer.step()

writer.close()

#进行数据测试
x_test = torch.Tensor([4.0])
y_hat = model(x_test)
print("\ty_hat:",y_hat.item())




1.1 部分结果展示

Epoch: 992 1.0064479112625122
Epoch: 993 1.0060151815414429
Epoch: 994 1.0055828094482422
Epoch: 995 1.0051509141921997
Epoch: 996 1.0047192573547363
Epoch: 997 1.0042884349822998
Epoch: 998 1.0038578510284424
Epoch: 999 1.0034277439117432
    y_hat: 0.883488655090332

1.2图像展示(当lr=0.01时函数收敛不好,损失较高)

(刘二大人)PyTorch深度学习实践-Logistic回归实现_第1张图片

2.当 lr = 0.1时结果更加理想

2.1 部分结果展示

Epoch: 992 0.25120747089385986
Epoch: 993 0.25100672245025635
Epoch: 994 0.2508062720298767
Epoch: 995 0.2506064176559448
Epoch: 996 0.2504065930843353
Epoch: 997 0.25020724534988403
Epoch: 998 0.25000816583633423
Epoch: 999 0.24980932474136353
    y_hat: 0.9982137680053711

2.2 图像展示,收敛更好

(刘二大人)PyTorch深度学习实践-Logistic回归实现_第2张图片

 3.当 lr = 0.1时并且加入动量时,结果好!(要合理使用动量)

3.1 修改部分

optimizer = torch.optim.SGD(params=model.parameters(),lr = 0.1,momentum=0.9)

3.1 部分结果展示

Epoch: 992 0.028468873351812363
Epoch: 993 0.028441112488508224
Epoch: 994 0.02841341122984886
Epoch: 995 0.028385648503899574
Epoch: 996 0.028358127921819687
Epoch: 997 0.028330430388450623
Epoch: 998 0.02830297127366066
Epoch: 999 0.028275514021515846
    y_hat: 0.9999977350234985

3.2 优秀图像展示

(刘二大人)PyTorch深度学习实践-Logistic回归实现_第3张图片

 

你可能感兴趣的:(PyTorch,深度学习,pytorch,回归)