当前位置:网站首页>Using logistic regression and neural network to deal with complex binary classification problems
Using logistic regression and neural network to deal with complex binary classification problems
2022-07-29 09:05:00 【Salty salty】
import torch
import numpy as np
from torch import nn
from torch.autograd import Variable
import torch.nn.functional as F
import os
os.environ["KMP_DUPLICATE_LIB_OK"]="TRUE"
import matplotlib.pyplot as plt
%matplotlib inline
# mapping _ Decision making _ The border
def plot_decision_boundary(model,x,y):
# Set the minimum and maximum values , And give it some blank
x_min,x_max=x[:,0].min()-1,x[:,0].max()+1
y_min,y_max=x[:,1].min()-1,x[:,1].max()+1
h=0.01
# Generate mesh of points , The distance between the points is h
xx,yy=np.meshgrid(np.arange(x_min,x_max,h),np.arange(y_min,y_max,h))
#Predict the function value for the whole grid
z=model(np.c_[xx.ravel(),yy.ravel()])
z=z.reshape(xx.shape)
# Draw contour lines and training examples
plt.contourf(xx,yy,z,cmap=plt.cm.Spectral)
plt.ylabel('x2')
plt.xlabel('x1')
plt.scatter(x[:,0],x[:,1],c=y.reshape(-1),s=40,cmap=plt.cm.Spectral)
np.random.seed(1)
m=400 # Number of samples
N=int(m/2)# Number of points of each class
D=2 # dimension
x=np.zeros((m,D))
y=np.zeros((m,1),dtype='uint8')#label vector ,0 It means red ,1 It means blue
a=4
for j in range(2):
ix=range(N*j,N*(j+1))
t=np.linspace(j*3.12,(j+1)*3.12,N)+np.random.randn(N)*0.2 # angle
r=a*np.sin(4*t)+np.random.randn(N)*0.2 # radius
x[ix]=np.c_[r*np.sin(t),r*np.cos(t)]
y[ix]=j
plt.scatter(x[:,0],x[:,1],c=y.reshape(-1),s=40,cmap=plt.cm.Spectral)
x = torch.from_numpy(x).float()
y = torch.from_numpy(y).float()
w = nn.Parameter(torch.randn(2, 1))
b = nn.Parameter(torch.zeros(1))
optimizer = torch.optim.SGD([w, b], 1e-1)
def logistic_regression(x):
return torch.mm(x, w) + b
criterion = nn.BCEWithLogitsLoss()
for e in range(100):
out = logistic_regression(Variable(x))
loss = criterion(out,Variable(y))
optimizer.zero_grad()
loss.backward()
optimizer.step()
if(e+1)%20==0:
print('epoch:{},loss:{}'.format(e+1,loss.item()))
def plot_logistic(x):
x = Variable(torch.from_numpy(x).float())
out = F.sigmoid(logistic_regression(x))
out = (out > 0.5) * 1
return out.data.numpy()
plot_decision_boundary(lambda x: plot_logistic(x), x.numpy(), y.numpy())
plt.title('logistic regression')
Can clearly see , For complex binary classification problems , Logistic regression cannot solve , Next, use neural network to solve this problem .
# Define the parameters of the two-layer neural network
w1=nn.Parameter(torch.randn(2,4)*0.01) # The number of neurons in the hidden layer 2
b1=nn.Parameter(torch.zeros(4))
w2=nn.Parameter(torch.randn(4,1)*0.01)
b2=nn.Parameter(torch.zeros(1))
# Defining models
def two_network(x):
x1=torch.mm(x,w1)+b1
x1=torch.tanh(x1)
x2=torch.mm(x1,w2)+b2
return x2
optimizer = torch.optim.SGD([w1,w2,b1,b2],1.)#SGD Stochastic gradient descent . In order to use torch.optim, Need to build optimizer object , This object can maintain the current parameter state and update the parameters based on the calculated gradient
criterion = nn.BCEWithLogitsLoss()
# We train 10000 Time
for e in range(10000):
out =two_network(Variable(x))
loss = criterion(out,Variable(y))
optimizer.zero_grad()
loss.backward()
optimizer.step()
if (e+1)%1000==0:
print('epoch:{},loss:{}'.format(e+1,loss.item()))
def plot_network(x):
x=Variable(torch.from_numpy(x).float())
x1=torch.mm(x,w1)+b1
x1=torch.tanh(x1)
x2=torch.mm(x1,w2)+b2
out=torch.sigmoid(x2)
out=(out>0.5)*1
return out.data.numpy()
We can see that , For complex binary classification problems , Neural network is more suitable .
边栏推荐
- The gold content of PMP certificate has been increased again and included in the scope of Beijing work residence permit
- access数据库可以被远程访问吗
- C language calculates the length of string
- 英语高频后缀
- LeetCode力扣题目总结(题目编号:53、3、141、面试题022、剑指offer链表中环的入口节点、20、19、牛客NC1、103、1143、牛客127)
- ML.NET相关资源整理
- 7.2-function-overloading
- QT learning: use non TS files such as json/xml to realize multilingual internationalization
- Tesseract图文识别--简单
- GBase 8s数据库有哪些备份恢复方式
猜你喜欢
Tesseract text recognition -- simple
QT version of Snake game project
The gold content of PMP certificate has been increased again and included in the scope of Beijing work residence permit
mysql怎么换成中文
(视频+图文)机器学习入门系列-第2章 线性回归
2022 question bank and answers of operation certificate examination for main principals of hazardous chemical business units
C# 使用数据库对ListView控件数据绑定
2022年山东省安全员C证上岗证题库及答案
四元数与其在Unity中的简单应用
Leetcode question brushing (6)
随机推荐
smart-webcomponents 14.2.0 Crack
MySQL 错误总结
Leetcode deduction topic summary (topic No.: 53, 3, 141, interview question 022, the entry node of the link in the sword finger offer chain, 20, 19, Niuke NC1, 103, 1143, Niuke 127)
Use disco diffusion to generate AI artwork in moment pool cloud
2022电工(初级)考题模拟考试平台操作
分组背包
Redis series 3: highly available master-slave architecture
Leetcode: interview question 08.14. Boolean operation
ADB common command list
Leetcode question brushing (6)
BI data analysis practitioners learn financial knowledge from scratch? What introductory books are recommended
2022年山东省安全员C证上岗证题库及答案
Information system project manager must recite the quality grade of the core examination site (53)
Restful style details
Opencv cvcircle function
What is the key to fast multi tag user profile analysis?
LeetCode力扣题目总结(题目编号:53、3、141、面试题022、剑指offer链表中环的入口节点、20、19、牛客NC1、103、1143、牛客127)
2022 electrician (elementary) test question simulation test platform operation
工业测控设备内生信息安全技术研究综述
C language macro define command exercise