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
#logistic回归
#读取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]
#q前两个数据表示X,Y坐标,最后一个数据表示类别
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]
plt.plot(plot_x0_x,plot_x0_y,'ro',label='x_0')
plt.plot(plot_x1_x,plot_x1_y,'gx',label='x_1')
plt.legend(loc='best')
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__()
self.lr = nn.Linear(2,1) #输入两个属性输出一个类别
self.sm = nn.Sigmoid() #Sigmoid激活函数就是Logistic回归的精髓
def forward(self,x):
x = self.lr(x)
x = self.sm(x)
return x
LR_model = LR()
if torch.cuda.is_available():
LR_model.cuda()
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()
else:
x = x_data
y = y_data
#===========forward==================#
out = LR_model(x)
loss = criterion(out,y)
mask = out.ge(0.5).float() #把输出结果大于0.5的归于1,小于归成0
correct = (mask==y).sum()
acc = correct.item()/x.size(0)
#===========backward==================#
optimizer.zero_grad()
loss.backward()
optimizer.step()
if (epoch+1)%1000== 0: #每迭代一千次,看一下当前loss,acc
print('*'*10)
print('epoch:{},loss is:{},acc is:{}'.format(epoch,loss.item(),acc))
The results are as follows:
4. Draw boundary data in FIG.
#画出分界线
w0,w1 = LR_model.lr.weight[0]
w0 = w0.item() #tensor是标量可以用item直接转换
w1 = w1.item()
b = LR_model.lr.bias[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直线
plt.plot(plot_x0_x,plot_x0_y,'ro',label='x_0')
plt.plot(plot_x1_x,plot_x1_y,'gx',label='x_1')
plt.legend(loc='best')
plt.plot(plot_x,plot_y)
Pytorch version of the problem, the original book code may have a lot of error, Baidu click.