深度学习新手求助!!!!有关逻辑回归模型的问题

我在训练PyTorch的逻辑回归模型的时候,遇到了一个问题,权重的训练结果是让人满意的,但是偏执的训练结果是很差的,我不知道为什么会有这种事情发生,请大佬们指点一下新手

代码中用到的数据是data.txt文件,文件内容为:
34.62365962451697,78.0246928153624,0
30.28671076822607,43.89499752400101,0
35.84740876993872,72.90219802708364,0
60.18259938620976,86.30855209546826,1
79.0327360507101,75.3443764369103,1
45,083.27747668339,56.3163717815305,0
61.10666453684766,96.51142588489624,1
75.02474556738889,46.55401354116538,1
76.09878670226257,87.42056971926803,1
84.43281996120035,43.53339331072109,1
95.86155507093572,38.22527805795094,0
75.01365838958247,30.60326323428011,0
82.30705337399482,76.48196330235604,1
69.36458875970939,97.71869196188608,1
39.53833914367223,76.03681085115882,0
53.9710521485623,89.20735013750205,1
69.07014406283025,52.74046973016765,1
67.94685547711617,46.67857410673128,0
30,50,0
30,43,0
32,12,0
35,48,0
55.5,33,0
57,40,0
75,30,0
100,60,1
100,70,1
100,80,1
70,80,1
80,80,1
90,45,1
91,44,1
30,100,1
32,99,1
以上就是训练数据
下面是代码:

import torch
import torch.nn as nn
import matplotlib.pyplot as plt
from torch.autograd import Variable

'二分类Logistic'
'文本读取,并切割'
with open('data.txt', 'r') as f:
        data_list = f.readlines()
        data_list = [i.strip().split(',') for i in data_list]
        data = [(float(i[0]), float(i[1]), float(i[2])) for i in data_list]
        x_data = torch.tensor([i[0] for i in data])
        y_data = torch.tensor([i[1] for i in data])
        x_y_data = torch.tensor([[i[0], i[1]] for i in data])
        label = torch.tensor([i[2] for i in data])
'绘图'
x0 = list(filter(lambda x: x[-1] == 0.0, data))
x1 = list(filter(lambda x: x[-1] == 1.0, data))
plot_x0_0 = [i[0] for i in x0]
plot_x0_1 = [i[1] for i in x0]
plot_x1_0 = [i[0] for i in x1]
plot_x1_1 = [i[1] for i in x1]

plt.plot(plot_x0_0, plot_x0_1, 'ro', label='x_0')
plt.plot(plot_x1_0, plot_x1_1, 'bo', label='x_1')
plt.legend(loc='best')

'构造模型'


class LogisticRegression(nn.Module):
        def __init__(self):
                super(LogisticRegression, self).__init__()
                self.lr = nn.Linear(2, 1)
                self.sm = nn.Sigmoid()

        def forward(self, x):
                return self.sm(self.lr(x))


logistic_model = LogisticRegression()
if torch.cuda.is_available():
logistic_model.cuda()
'构造损失函数和优化器'
criterion = nn.BCELoss() # nn.BCELoss是二分类的损失函数
optimizer = torch.optim.SGD(logistic_model.parameters(), lr=0.001, momentum=0.9)
'训练模型'
for epoch in range(20000):
        if torch.cuda.is_available():
                x_y = Variable(x_y_data).cuda()
                Label = Variable(label.unsqueeze(1)).cuda()
        else:
                x_y = Variable(x_y_data)
                Label = Variable(label.unsqueeze(1))
        'forward'
        out = logistic_model(x_y)
        loss = criterion(out, Label)
        print_loss = loss.data.item()
        mask = out.ge(0.5).float()
        correct = (mask == Label).sum()
        acc = correct.item() / x_y.size(0)
        'backward'
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()
        if (epoch + 1) % 100 == 0:
                print('*' * 10)
                print('epoch {}'.format(epoch + 1))
                print('loss is {:.4f}'.format(print_loss))
                print('acc is {:.4f}'.format(acc))

‘画出训练后的拟合直线’
w0, w1 = logistic_model.lr.weight[0]
w0 = w0.item()
w1 = w1.item()
b = logistic_model.lr.bias.item()
print("w0 =", w0, "w1 =", w1, "b =", b)
plot_x = [i * 0.1 for i in range(300, 1001)]
plot_y = [(-w0 * i - b) / w1 for i in plot_x]
plt.plot(plot_x, plot_y)
plt.show()

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值