Pytorch专题实战——逻辑回归(Logistic Regression)
程序员文章站
2022-07-06 10:18:15
...
1.计算流程
1)设计模型: Design model (input, output, forward pass with different layers)
2) 构建损失函数与优化器:Construct loss and optimizer
3) 循环:Training loop
- Forward = compute prediction and loss
- Backward = compute gradients
- Update weights
2.Pytorch搭建线性逻辑模型
2.1.导入必要模块
import torch
import torch.nn as nn
import numpy as np
from sklearn import datasets
from sklearn.preprocessing import StandardScaler #数据标准化模块
from sklearn.model_selection import train_test_split
2.2.数据准备
bc = datasets.load_breast_cancer() #加载数据集
X, y = bc.data, bc.target #取数据和标签
n_samples, n_features = X.shape #样本数量、特征数
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
sc = StandardScaler()
X_train = sc.fit_transform(X_train) #求得训练集X的均值,方差,最大值,最小值并进行标准化
X_test = sc.transform(X_test) #对标签标准化
#将数据转化为torch形式
X_train = torch.from_numpy(X_train.astype(np.float32))
X_test = torch.from_numpy(X_test.astype(np.float32))
y_train = torch.from_numpy(y_train.astype(np.float32))
y_test = torch.from_numpy(y_test.astype(np.float32))
y_train = y_train.view(y_train.shape[0], 1) #将标签多行1列
y_test = y_test.view(y_test.shape[0], 1)
2.3.构建模型
class Model(nn.Module):
def __init__(self, n_input_features):
super(Model, self).__init__()
self.linear = nn.Linear(n_input_features, 1)
def forward(self, x):
y_pred = torch.sigmoid(self.linear(x))
return y_pred
2.4.训练+计算准确率
model = Model(n_features)
num_epochs = 100
learning_rate = 0.01
criterion = nn.BCELoss()
optimizer = torch.optim.SGD(model.parameters(), lr=learning_rate)
for epoch in range(num_epochs):
y_pred = model(X_train)
loss = criterion(y_pred, y_train)
loss.backward()
optimizer.step()
optimizer.zero_grad()
if (epoch+1)%10 == 0:
print(f'epoch:{epoch+1}, loss={loss.item():.4f}')
with torch.no_grad():
y_predicted = model(X_test)
y_predicted_cls = y_predicted.round() #四舍五入
acc = y_predicted_cls.eq(y_test).sum()/float(y_test.shape[0])
print(f'accuracy:{acc.item():.4f}')
推荐阅读
-
opencv3/C++ 机器学习-逻辑回归/Logistic Regression
-
案例:逻辑回归Logistic Regression做乳腺癌预测
-
吴恩达机器学习 8.6 逻辑回归(Logistic Regression)
-
PyTorch线性回归和逻辑回归实战示例
-
Pytorch专题实战——逻辑回归(Logistic Regression)
-
PyTorch:线性回归和逻辑回归实战
-
python 牛顿法实现逻辑回归(Logistic Regression)
-
PyTorch线性回归和逻辑回归实战示例
-
pytorch实战(一)-----逻辑回归
-
python 牛顿法实现逻辑回归(Logistic Regression)