PyTorch 实现 Logistic回归 逻辑回归

版权声明:转载请注明出处及原文地址。 https://blog.csdn.net/zl1085372438/article/details/84332107

数据点随机产生(二维平面的点),下附产生训练数据的代码

import numpy as np
import torch
from matplotlib import pyplot as plt
from torch import nn, optim
from torch.autograd import Variable

data = np.load('data.npy')
x = data[:, 0:2]
label = data[:, 2]


class LogisticRegression(nn.Module):
    """ Define Model """

    def __init__(self):
        super().__init__()
        self.linear = nn.Linear(2, 1)
        self.sigmoid = nn.Sigmoid()

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


logistic_model = LogisticRegression()
criterion = nn.BCELoss()
optimizer = optim.SGD(logistic_model.parameters(), lr=1e-3, momentum=0.9)

x = torch.FloatTensor(x)
label = torch.FloatTensor(label)
label = label.reshape(-1, 1)
x = Variable(x)
label = Variable(label)

# lossy=[]
# def plot(print_loss):
#     plt.figure(1)
#     plt.cla()
#     lossy.append(print_loss)
#     plt.plot(lossy, 'bo-', linewidth=2, markersize=4,
#              markeredgecolor='red', markerfacecolor='green')
#     plt.pause(0.0000001)

for epoch in range(50000):
    out = logistic_model(x)
    loss = criterion(out, label)
    print_loss = loss.data.item()
    mask = out.ge(0.5).float()
    correct = (mask == label).sum()
    acc = correct.data.item()/x.size(0)
    optimizer.zero_grad()
    loss.backward()
    optimizer.step()
    if(epoch+1) % 100 == 0:
        print('epoch {} , loss = {} , acc = {}'.format(epoch, print_loss, acc))
        # plot(print_loss)


logistic_model.eval()
predict = logistic_model(x)
mask = predict.ge(0.5).float()

x = x.numpy()
for i, it in enumerate(x):
    px = it[0]
    py = it[1]
    if mask[i] == 1:
        plt.plot(px, py, 'ro')
    else:
        plt.plot(px, py, 'bo')
    if(mask[i] != label[i]):
        plt.plot(px, py, 'g<', markersize=10)

w0, w1 = logistic_model.linear.weight[0]
w0 = w0.data.item()
w1 = w1.data.item()
b = logistic_model.linear.bias.data.item()
print('w0 = {} w1 = {}  b = {}'.format(w0, w1, b))

line_x = np.linspace(0, 1, 100)
line_y = (-w0*line_x-b)/w1
plt.plot(line_x, line_y)
plt.show()

产生训练数据的代码:

import torch
from matplotlib import pyplot as plt
import numpy as np


xx = np.random.rand(100).reshape(-1, 1)
yy = np.random.rand(100).reshape(-1, 1)


# plt.plot(xx, yy, 'ro')
# plt.show()

w0 = 3
w1 = 5
b = -4


def line(x):
    return (-w0*x-b)/w1


label = []
for i,ix in enumerate(xx):
    ly = line(ix)
    if yy[i] > ly:
        label.append(1)
        plt.plot(ix, yy[i], 'ro')
    else:
        label.append(0)
        plt.plot(ix, yy[i], 'bo')

label=np.array(label)
label=label.reshape(-1,1)
total=np.hstack((xx,yy,label))
print(total)
np.save('data',total)
total=torch.tensor(total)
print(total.shape[0])

line_x = np.linspace(0, 1, 50)
line_y = line(line_x)
plt.plot(line_x, line_y)
plt.show()

猜你喜欢

转载自blog.csdn.net/zl1085372438/article/details/84332107