基于PyTorch实现逻辑回归
逻辑回归是一种常用的分类算法,它可以用于二分类和多分类问题。在本文中,我们将介绍如何使用PyTorch实现逻辑回归,并提供两个示例说明。
示例1:使用鸢尾花数据集实现二分类逻辑回归
以下是一个使用鸢尾花数据集实现二分类逻辑回归的示例代码:
import torch
import torch.nn as nn
import torch.optim as optim
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
# Load iris dataset
iris = load_iris()
X = iris.data
y = iris.target
# Preprocess data
scaler = StandardScaler()
X = scaler.fit_transform(X)
y = (y == 0).astype(int)
# Split data into train and test sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# Define logistic regression model
class LogisticRegression(nn.Module):
def __init__(self):
super(LogisticRegression, self).__init__()
self.linear = nn.Linear(4, 1)
def forward(self, x):
x = self.linear(x)
x = torch.sigmoid(x)
return x
# Train logistic regression model
model = LogisticRegression()
criterion = nn.BCELoss()
optimizer = optim.SGD(model.parameters(), lr=0.1)
for epoch in range(100):
optimizer.zero_grad()
y_pred = model(torch.Tensor(X_train))
loss = criterion(y_pred, torch.Tensor(y_train).view(-1, 1))
loss.backward()
optimizer.step()
# Test logistic regression model
y_pred = model(torch.Tensor(X_test))
y_pred = (y_pred > 0.5).float()
accuracy = (y_pred == torch.Tensor(y_test).view(-1, 1)).float().mean()
print('Accuracy:', accuracy.item())
在这个示例中,我们首先加载鸢尾花数据集,并对数据进行预处理。然后,我们将数据集分成训练集和测试集,并定义一个LogisticRegression类来实现逻辑回归模型。在训练过程中,我们使用二元交叉熵损失函数和随机梯度下降优化器来训练模型。在测试过程中,我们使用训练好的模型对测试集进行预测,并计算模型的准确率。
示例2:使用MNIST数据集实现多分类逻辑回归
以下是一个使用MNIST数据集实现多分类逻辑回归的示例代码:
import torch
import torch.nn as nn
import torch.optim as optim
from torchvision.datasets import MNIST
from torchvision.transforms import ToTensor
from torch.utils.data import DataLoader
from sklearn.preprocessing import StandardScaler
# Load MNIST dataset
train_dataset = MNIST(root='./data', train=True, transform=ToTensor(), download=True)
test_dataset = MNIST(root='./data', train=False, transform=ToTensor(), download=True)
# Preprocess data
scaler = StandardScaler()
X_train = scaler.fit_transform(train_dataset.data.reshape(-1, 784).numpy())
y_train = train_dataset.targets.numpy()
X_test = scaler.transform(test_dataset.data.reshape(-1, 784).numpy())
y_test = test_dataset.targets.numpy()
# Define logistic regression model
class LogisticRegression(nn.Module):
def __init__(self):
super(LogisticRegression, self).__init__()
self.linear = nn.Linear(784, 10)
def forward(self, x):
x = self.linear(x)
x = torch.softmax(x, dim=1)
return x
# Train logistic regression model
model = LogisticRegression()
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(model.parameters(), lr=0.1)
train_loader = DataLoader(train_dataset, batch_size=64, shuffle=True)
test_loader = DataLoader(test_dataset, batch_size=64, shuffle=False)
for epoch in range(10):
for i, (images, labels) in enumerate(train_loader):
optimizer.zero_grad()
images = images.view(-1, 784)
labels = labels
y_pred = model(images)
loss = criterion(y_pred, labels)
loss.backward()
optimizer.step()
# Test logistic regression model
correct = 0
total = 0
with torch.no_grad():
for images, labels in test_loader:
images = images.view(-1, 784)
labels = labels
y_pred = model(images)
_, predicted = torch.max(y_pred.data, 1)
total += labels.size(0)
correct += (predicted == labels).sum().item()
print('Accuracy:', correct / total)
在这个示例中,我们首先加载MNIST数据集,并对数据进行预处理。然后,我们定义一个LogisticRegression类来实现多分类逻辑回归模型。在训练过程中,我们使用交叉熵损失函数和随机梯度下降优化器来训练模型。在测试过程中,我们使用训练好的模型对测试集进行预测,并计算模型的准确率。
总结
在本文中,我们介绍了如何使用PyTorch实现逻辑回归,并提供了两个示例说明。这些技术对于在深度学习中进行分类任务非常有用。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:基于Pytorch实现逻辑回归 - Python技术站