Pytorch Logistic Regression

    Refer to "Getting Started with the deep learning Pytorch"
    Logistic regression achieve binary classification

    Data download link: the Data (extraction code: q8gd)

1. First read data data.txt

import matplotlib.pyplot as plt
with open('data.txt','r') as f:
    data_list = f.readlines()
    data_list = [i.split('\n')[0] for i in data_list]
    data_list = [i.split(',') for i in data_list]
    data = [(float(i[0]),float(i[1]),float(i[2])) for i in data_list]

Data.txt open, the first two are horizontal and vertical coordinates data, the last category was 0,1. This data has two properties a category. Then the data according to different categories drawn.

x0 = list(filter(lambda x: x[-1]==0.,data)) #filter去除不符合条件的,保留符合条件的数据
x1 = list(filter(lambda x: x[-1]==1.,data))#数据按类别筛选
plot_x0_x = [i[0] for i in x0]
plot_x0_y = [i[1] for i in x0]
plot_x1_x = [i[0] for i in x1]
plot_x1_y = [i[1] for i in x1]



 Pytorch operation object is Tensor, this type of data is to be converted Tensor.

import torch
import numpy as np
from torch import nn

# #转换数据
np_data = np.array(data, dtype='float32') # 转换成 numpy array
x_data = torch.from_numpy(np_data[:, 0:2]) # 转换成 Tensor, 大小是 [100, 2]
y_data = torch.from_numpy(np_data[:, -1]).unsqueeze(1) # 转换成 Tensor,大小是 [100, 1]

2. construct a classification model

class LR(nn.Module):
    def __init__(self):
        super(LR,self).__init__() = nn.Linear(2,1)   #输入两个属性输出一个类别 = nn.Sigmoid()     #Sigmoid激活函数就是Logistic回归的精髓
    def forward(self,x):
        x =
        x =
        return x
LR_model = LR()
if torch.cuda.is_available():
criterion = nn.BCELoss()   #BCE是二分类损失函数 交叉熵
optimizer = torch.optim.SGD(LR_model.parameters(),lr=1e-3,momentum=0.9)

 3. The training model, if support CUDA, make models and data are running on CUDA, with .cuda () can bind.

for epoch in range(18000):
    if torch.cuda.is_available():
        x = x_data.cuda()
        y = y_data.cuda()
        x = x_data
        y = y_data
    out = LR_model(x)
    loss = criterion(out,y)
    mask =  #把输出结果大于0.5的归于1,小于归成0
    correct = (mask==y).sum()
    acc = correct.item()/x.size(0)
    if (epoch+1)%1000== 0:  #每迭代一千次,看一下当前loss,acc
        print('epoch:{},loss is:{},acc is:{}'.format(epoch,loss.item(),acc))

The results are as follows:


4. Draw boundary data in FIG.

w0,w1 =[0]  
w0 = w0.item() #tensor是标量可以用item直接转换
w1 = w1.item()
b =[0]
b = b.item()

plot_x = np.arange(30,100,0.1)#从之前的图大致看出x轴范围
plot_y = (-w0*plot_x-b)/w1  #w1y+w0 x+b=0直线


Pytorch version of the problem, the original book code may have a lot of error, Baidu click.

Published 10 original articles · won praise 10 · views 7510

Guess you like