当前位置:网站首页>Torch learning notes (6) -- logistic regression model (self training)
Torch learning notes (6) -- logistic regression model (self training)
2022-07-03 18:22:00 【ZRX_ GIS】
import torch
import torch.nn as nn
import numpy as np
import matplotlib.pyplot as plt
# Logical regression (Logistic Regression)
# LR It is a linear binary classification model
# y = f(wx+b) f(x) = 1/(1+e**-x) f(x) Also known as Sigmoid Function or Logistic function
# LR effect : Map input data to [0,1]
# Process quantity classification adopts “ rounding , Realize overall two classification
# Linear regression is the analysis of independent variables x And scalar y The method of the relationship between
# LR Is the analysis independent variable x And probability y The method of the relationship between
# LR It can also be generated by variant “ Logarithmic probability regression model ” ln(y/1-y) = wx+b
# Machine learning steps :data( collection 、 cleaning 、 Divide 、 Preprocessing )、model、Loss、optim
# LR Case study
# data
sample_num = torch.tensor([100.], requires_grad=True)
mean_value = 1.7
bias = 1
n_data = torch.normal(sample_num, 2)
x0 = torch.normal(mean_value * n_data, 1) + bias # Normal distribution generates
y0 = torch.zeros(100)
x1 = torch.normal(-mean_value * n_data, 1) + bias
y1 = torch.ones(100)
train_x = torch.cat((x0, x1), 0) # Splicing variables
train_y = torch.cat((y0, y1), 0)
# model
class LR(nn.Module):
def __init__(self):
super(LR, self).__init__()
self.features = nn.Linear(2, 1)
self.sigmoid = nn.Sigmoid()
def forward(self, x):
x = self.features(x)
x = self.sigmoid(x)
# Instantiation LR
LR = LR()
# Loss
loss_fn = nn.BCELoss()
# optim
lr = 0.01 # Learning rate
optimizer = torch.optim.SGD(LR.parameters(), lr=lr, momentum=0.9)
# train
for iteration in range(1000):
y_pred = LR(train_x)
loss = loss_fn(y_pred.squeeze(), train_y)
loss.backward()
optimizer.step()
# mapping
if iteration % 20 == 0:
mask = y_pred.ge(0.5).float().squeeze() # 0.5 Classify thresholds
correct = (mask == train_y).sum() # Number of correct classifications
acc = correct.item() / train_y.size(0) # precision
plt.scatter(x0.data.numpy()[:, 0], x0.data.numpy()[:, 1], c='r', label='class 0')
plt.scatter(x1.data.numpy()[:, 0], x1.data.numpy()[:, 1], c='b', label='class 1')
w0, w1 = LR.features.weight[0]
w0, w1 = float(w0.item()), float(w1.item())
plot_b = float(LR.features.bias[0].item())
plot_x = np.arange(-6, 6, 0.1)
plot_y = (-w0 * plot_x - plot_b) / w1
plt.xlim(-5, 7)
plt.xlim(-5, 7)
plt.plot(plot_x, plot_y)
plt.legend()
plt.show()
plt.pause(0.5)
if acc >= 0.99: break
边栏推荐
- Recent learning experience
- Theoretical description of linear equations and summary of methods for solving linear equations by eigen
- [Yu Yue education] family education SPOC class 2 reference materials of Shanghai Normal University
- 4. Load balancing and dynamic static separation
- win32:堆破坏的dump文件分析
- Bidding procurement scheme management of Oracle project management system
- [Godot] add menu button
- BFS - topology sort
- Have you learned the correct expression posture of programmers on Valentine's day?
- After the festival, a large number of people change careers. Is it still time to be 30? Listen to the experience of the past people
猜你喜欢
What London Silver Trading software supports multiple languages
English grammar_ Adjective / adverb Level 3 - multiple expression
Redis core technology and practice - learning notes (VII) sentinel mechanism
What is SQL get connection
(9) Opencv Canny edge detection
AcWing 271. Teacher Yang's photographic arrangement [multidimensional DP]
2022-2028 global solid phase extraction column industry research and trend analysis report
Theoretical description of linear equations and summary of methods for solving linear equations by eigen
2022-2028 global plasmid DNA cdmo industry research and trend analysis report
An academic paper sharing and approval system based on PHP for computer graduation design
随机推荐
2022-2028 global petroleum pipe joint industry research and trend analysis report
Postfix 技巧和故障排除命令
图像24位深度转8位深度
PHP MySQL order by keyword
An academic paper sharing and approval system based on PHP for computer graduation design
Fedora 21 安装 LAMP 主机服务器
[combinatorics] generating function (generating function application scenario | using generating function to solve recursive equation)
Solve the problem of inaccurate network traffic monitored by ZABBIX with SNMP
As soon as we enter "remote", we will never regret, and several people will be happy and several people will be sad| Community essay solicitation
网格图中递增路径的数目[dfs逆向路径+记忆dfs]
Closure and closure function
Analysis of the reasons why enterprises build their own software development teams to use software manpower outsourcing services at the same time
What London Silver Trading software supports multiple languages
[combinatorics] generating function (property summary | important generating function)*
Should I be laid off at the age of 40? IBM is suspected of age discrimination, calling its old employees "dinosaurs" and planning to dismiss, but the employees can't refute it
SSL / bio pour OpenSSL Get FD
SQL injection -day16
Module 9 operation
English语法_形容词/副词3级 - 倍数表达
[combinatorics] exponential generating function (example 2 of solving multiple set permutation with exponential generating function)