当前位置:网站首页>Visualization of two-dimensional feature logistic regression prediction results
Visualization of two-dimensional feature logistic regression prediction results
2022-06-12 09:04:00 【Little wish】
Problem description
The following 2D features , Each sample is a positive sample ( Red ) Or negative sample ( Blue ), Implement the binary classification model 
Single layer neural network
[ w 1 w 2 ] [ x 1 x 2 ] + [ b ] = [ y ] \left[\begin{matrix} w_1 & w_2 \end{matrix}\right] \left[ \begin{matrix} x_1\\ x_2 \end{matrix}\right] + \left[ \begin{matrix} b \end{matrix}\right] = \left[ \begin{matrix} y \end{matrix}\right] [w1w2][x1x2]+[b]=[y]
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):
out = self.lr(x)
out = self.sm(out)
return out
We want to see the training effect of the model , Need to put L i n e a r Linear Linear Layer Visualization
w 1 ∗ x 1 + w 2 ∗ x 2 + b = y w_1*x_1+w_2*x_2+b=y w1∗x1+w2∗x2+b=y
y = 0 y=0 y=0 when , x 1 x_1 x1 and x 2 x_2 x2 The relationship is as follows :
x 2 = − w 1 ∗ x 1 − b w 2 x_2=\frac{-w_1*x_1-b}{w2} x2=w2−w1∗x1−b
take x 1 x_1 x1 stay [ 30 , 100 ] [30,100] [30,100] Between , Using neural networks L i n e a r Linear Linear Layer parameters will x 2 x_2 x2 Find the value of , Draw the training results .
def vis_one_layer(logistic_model):
w1, w2 = logistic_model.lr.weight.data.numpy()[0]
b = logistic_model.lr.bias.data.numpy()[0]
plot_x = np.arange(30, 100, 0.1)
plot_y = (-w1 * plot_x - b) / w2
plt.plot(plot_x, plot_y)
plt.show()

Multilayer neural network
If the neural network has more than one layer , It is impossible to substitute x 1 x_1 x1 and x 2 x_2 x2 The relationship between , How to visualize the prediction results of neural networks ?
class MyLogistic(nn.Module):
def __init__(self, input_size):
super().__init__()
self.hidden_1 = nn.Linear(input_size, 64)
self.hidden_2 = nn.Linear(64, 32)
self.hidden_3 = nn.Linear(32, 16)
self.output = nn.Linear(16, 1)
self.relu_1 = nn.ReLU()
self.relu_2 = nn.ReLU()
self.relu_3 = nn.ReLU()
self.sigmoid = nn.Sigmoid()
def forward(self, x):
x = self.hidden_1(x)
x = self.relu_1(x)
x = self.hidden_2(x)
x = self.relu_2(x)
x = self.hidden_3(x)
x = self.relu_3(x)
x = self.output(x)
x = self.sigmoid(x)
return x
The idea is to use a series of points to determine the region of the neural network to divide the binary classification problem , Just because the predicted result is not linear , So use the model to predict the positive and negative for the points in the region , Then visualize with different colors , Use p l t . c o n t o u r f plt.contourf plt.contourf Draw outline lines and fill .
def vis_result(x_data, y_data, model):
x_min, x_max = x_data[:, 0].min() - 1, x_data[:, 0].max() + 1
y_min, y_max = x_data[:, 1].min() - 1, x_data[:, 1].max() + 1
print(x_min, x_max, y_min, y_max)
h = 0.1
xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h))
z = model(torch.from_numpy(np.c_[xx.ravel(), yy.ravel()]).float())
z = z.reshape(xx.shape).detach().numpy()
print(z)
z = [z[i] >= 0.5 for i in range(len(z))]
z = np.array(z)
plt.contourf(xx, yy, z, alpha=0.3)
for i in range(len(y_data)):
if y_data[i] == 1:
plt.scatter(x_data[i][0], x_data[i][1], c='r')
else:
plt.scatter(x_data[i][0], x_data[i][1], c='b')
plt.show()

边栏推荐
- Flink CheckPoint : Exceeded checkpoint tolerable failure threshold
- IP, DNS, domain name, URL, hosts
- [advanced pointer 2] array parameter transfer & pointer parameter transfer & function pointer & function pointer array & callback function
- Sword finger offer II 016 Longest substring without repeating characters - sliding window
- 解决当打开Unity时 提示项目已经打开,而自己之前并没有打开过(可能之前异常关闭)的问题
- Node sample background setup
- 网页中加载二次元3D虚拟主播源码(1:项目介绍和源码)
- Machine learning notes - circular neural network memo list
- [character set 6] wide string and multi byte character conversion
- (十三)文本渲染Text
猜你喜欢
![[data storage] storage of floating point data in memory](/img/c4/a67735858ce5d58bd504b087a2d123.png)
[data storage] storage of floating point data in memory

Flink CheckPoint : Exceeded checkpoint tolerable failure threshold

Detailed explanation of iSCSI (V) -- actual operation of iSCSI client configuration

Background attribute compound writing

Analysis of 43 cases of MATLAB neural network: Chapter 8 prediction of GRNN Network - Freight Volume Prediction Based on generalized regression neural network

Application method of new version UI of idea + use method of non test qualification and related introduction

《MATLAB 神经网络43个案例分析》:第7章 RBF网络的回归--非线性函数回归的实现

Binlog in mysql:

Close asymmetric key

Load the source code of 2D 3D virtual anchor in the web page (1: project introduction and source code)
随机推荐
mySql学习记录——二、mySql建表命令
consul 配置相关
Background color translucent
第四章-第一个程序
[character set 7] what are the wide character codes and multi byte codes of Chinese characters
解压缩zip文件的工具类
RuntimeError:Input and parameter tensors are not at the same device, found input tensor at cuda:0 an
机器学习笔记 - 循环神经网络备忘清单
【字符集九】gbk拷贝到Unicode会乱码?
Composition of box model
剑指 Offer II 016. 不含重复字符的最长子字符串-滑动窗口
解决当打开Unity时 提示项目已经打开,而自己之前并没有打开过(可能之前异常关闭)的问题
2022 melting welding and thermal cutting test questions and answers
Background position - mixed units
mySql学习记录——三、mysql查询语句
《MATLAB 神经网络43个案例分析》:第8章 GRNN网络的预测----基于广义回归神经网络的货运量预测
第五章-[bx]和Loop指令
(js)三位用逗号隔开,保留两位小数(or 四舍五入取整)
第七章-更灵活定位内存地址
Application method of new version UI of idea + use method of non test qualification and related introduction