下面是关于pytorch 6 batch_train 批训练的完整攻略。
什么是批训练操作
在深度学习中,一般将训练数据分成一个个的batch,每个batch都可以看做是一个小的数据集。在批训练操作中,模型将对每个batch进行一次前向传播和反向传播,在更新梯度的过程中,使用所有batch的梯度的平均值。这样可以有效地加速训练进程,减小了内存占用和梯度更新的波动。
在pytorch中,批训练操作可以通过使用torch.utils.data.DataLoader
和torch.optim.SGD
两个类实现。
如何进行批训练操作
1. 导入相关库
import torch
import torch.nn as nn
import torch.optim as optim
from torch.utils.data import DataLoader
2. 准备数据
使用torch.utils.data.Dataset
类创建数据集,并使用torch.utils.data.DataLoader
类创建数据加载器。
class MyDataset(torch.utils.data.Dataset):
def __init__(self):
self.x = torch.randn(100, 10)
self.y = torch.randint(2, (100,))
def __getitem__(self, idx):
return self.x[idx], self.y[idx]
def __len__(self):
return len(self.x)
dataset = MyDataset()
loader = DataLoader(dataset=dataset, batch_size=10, shuffle=True)
3. 准备模型和损失函数
class MyModel(nn.Module):
def __init__(self):
super(MyModel, self).__init__()
self.fc1 = nn.Linear(10, 5)
self.fc2 = nn.Linear(5, 2)
def forward(self, x):
x = torch.relu(self.fc1(x))
x = self.fc2(x)
return x
model = MyModel()
criterion = nn.CrossEntropyLoss()
4. 准备优化器
optimizer = optim.SGD(model.parameters(), lr=0.1)
5. 训练模型
for epoch in range(num_epochs):
for i, (inputs, labels) in enumerate(loader):
optimizer.zero_grad()
outputs = model(inputs)
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()
if (i+1) % log_step == 0:
print(f'Epoch [{epoch+1}/{num_epochs}], Step [{i+1}/{len(loader)}], Loss: {loss.item():.4f}')
批训练操作的示例
示例1:使用MNIST数据集进行分类
import torch
import torch.nn as nn
import torch.optim as optim
from torchvision.datasets import MNIST
from torchvision.transforms import transforms
from torch.utils.data import DataLoader
transform = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.1307,), (0.3081,))
])
train_dataset = MNIST(root='./data', train=True, transform=transform, download=True)
train_loader = DataLoader(dataset=train_dataset, batch_size=64, shuffle=True)
test_dataset = MNIST(root='./data', train=False, transform=transform, download=True)
test_loader = DataLoader(dataset=test_dataset, batch_size=64, shuffle=False)
class MyModel(nn.Module):
def __init__(self):
super(MyModel, self).__init__()
self.conv1 = nn.Conv2d(1, 16, kernel_size=5)
self.bn1 = nn.BatchNorm2d(16)
self.conv2 = nn.Conv2d(16, 32, kernel_size=5)
self.bn2 = nn.BatchNorm2d(32)
self.fc1 = nn.Linear(32 * 4 * 4, 10)
def forward(self, x):
x = torch.relu(self.bn1(self.conv1(x)))
x = nn.functional.max_pool2d(x, 2)
x = torch.relu(self.bn2(self.conv2(x)))
x = nn.functional.max_pool2d(x, 2)
x = x.view(-1, 32 * 4 * 4)
x = self.fc1(x)
return x
model = MyModel()
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(model.parameters(), lr=0.01, momentum=0.9)
num_epochs = 10
log_step = 100
for epoch in range(num_epochs):
for i, (inputs, labels) in enumerate(train_loader):
optimizer.zero_grad()
outputs = model(inputs)
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()
if (i+1) % log_step == 0:
print(f'Epoch [{epoch+1}/{num_epochs}], Step [{i+1}/{len(train_loader)}], Loss: {loss.item():.4f}')
# 每个epoch结束后,对模型进行一个测试
with torch.no_grad():
total = 0
correct = 0
for inputs, labels in test_loader:
outputs = model(inputs)
_, predicted = torch.max(outputs.data, 1)
total += labels.size(0)
correct += (predicted == labels).sum().item()
accuracy = 100 * correct / total
print(f'Test Accuracy: {accuracy:.2f}%')
示例2:使用CIFAR10数据集进行分类
import torch
import torch.nn as nn
import torch.optim as optim
from torchvision.datasets import CIFAR10
from torchvision.transforms import transforms
from torch.utils.data import DataLoader
transform_train = transforms.Compose([
transforms.RandomCrop(32, padding=4),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
])
transform_test = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
])
train_dataset = CIFAR10(root='./data', train=True, transform=transform_train, download=True)
train_loader = DataLoader(dataset=train_dataset, batch_size=128, shuffle=True)
test_dataset = CIFAR10(root='./data', train=False, transform=transform_test, download=True)
test_loader = DataLoader(dataset=test_dataset, batch_size=128, shuffle=False)
class MyModel(nn.Module):
def __init__(self):
super(MyModel, self).__init__()
self.conv1 = nn.Conv2d(3, 64, kernel_size=3, padding=1)
self.bn1 = nn.BatchNorm2d(64)
self.conv2 = nn.Conv2d(64, 128, kernel_size=3, padding=1)
self.bn2 = nn.BatchNorm2d(128)
self.conv3 = nn.Conv2d(128, 256, kernel_size=3, padding=1)
self.bn3 = nn.BatchNorm2d(256)
self.conv4 = nn.Conv2d(256, 512, kernel_size=3, padding=1)
self.bn4 = nn.BatchNorm2d(512)
self.fc1 = nn.Linear(512 * 4 * 4, 512)
self.bn5 = nn.BatchNorm1d(512)
self.fc2 = nn.Linear(512, 10)
def forward(self, x):
x = torch.relu(self.bn1(self.conv1(x)))
x = nn.functional.max_pool2d(x, 2)
x = torch.relu(self.bn2(self.conv2(x)))
x = nn.functional.max_pool2d(x, 2)
x = torch.relu(self.bn3(self.conv3(x)))
x = nn.functional.max_pool2d(x, 2)
x = torch.relu(self.bn4(self.conv4(x)))
x = nn.functional.max_pool2d(x, 2)
x = x.view(-1, 512 * 4 * 4)
x = torch.relu(self.bn5(self.fc1(x)))
x = self.fc2(x)
return x
model = MyModel()
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(model.parameters(), lr=0.1, momentum=0.9, weight_decay=1e-4)
num_epochs = 10
log_step = 100
for epoch in range(num_epochs):
for i, (inputs, labels) in enumerate(train_loader):
optimizer.zero_grad()
outputs = model(inputs)
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()
if (i+1) % log_step == 0:
print(f'Epoch [{epoch+1}/{num_epochs}], Step [{i+1}/{len(train_loader)}], Loss: {loss.item():.4f}')
# 每个epoch结束后,对模型进行一个测试
with torch.no_grad():
total = 0
correct = 0
for inputs, labels in test_loader:
outputs = model(inputs)
_, predicted = torch.max(outputs.data, 1)
total += labels.size(0)
correct += (predicted == labels).sum().item()
accuracy = 100 * correct / total
print(f'Test Accuracy: {accuracy:.2f}%')
以上就是关于pytorch 6 batch_train 批训练操作的攻略和两个示例的说明。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:pytorch 6 batch_train 批训练操作 - Python技术站