用了PyTorch框架来实现AlexNet网络模型,并使用minist数据集进行训练和测试
时间: 2024-01-22 09:18:15 浏览: 100
很好,这是一个不错的练习。下面是一个简单的PyTorch实现AlexNet的代码,以及使用mnist数据集进行训练和测试的代码:
```python
import torch
import torch.nn as nn
class AlexNet(nn.Module):
def __init__(self, num_classes=10):
super(AlexNet, self).__init__()
self.features = nn.Sequential(
nn.Conv2d(1, 64, kernel_size=11, stride=4, padding=2),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=3, stride=2),
nn.Conv2d(64, 192, kernel_size=5, padding=2),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=3, stride=2),
nn.Conv2d(192, 384, kernel_size=3, padding=1),
nn.ReLU(inplace=True),
nn.Conv2d(384, 256, kernel_size=3, padding=1),
nn.ReLU(inplace=True),
nn.Conv2d(256, 256, kernel_size=3, padding=1),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=3, stride=2),
)
self.avgpool = nn.AdaptiveAvgPool2d((6, 6))
self.classifier = nn.Sequential(
nn.Dropout(),
nn.Linear(256 * 6 * 6, 4096),
nn.ReLU(inplace=True),
nn.Dropout(),
nn.Linear(4096, 4096),
nn.ReLU(inplace=True),
nn.Linear(4096, num_classes),
)
def forward(self, x):
x = self.features(x)
x = self.avgpool(x)
x = torch.flatten(x, 1)
x = self.classifier(x)
return x
# define the device for the model
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
# define the model
model = AlexNet(num_classes=10).to(device)
# define the loss function and optimizer
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.SGD(model.parameters(), lr=0.01, momentum=0.9)
# load the mnist dataset
train_dataset = torchvision.datasets.MNIST(root='./data', train=True, transform=transforms.ToTensor(), download=True)
test_dataset = torchvision.datasets.MNIST(root='./data', train=False, transform=transforms.ToTensor(), download=True)
# create data loaders
train_loader = torch.utils.data.DataLoader(train_dataset, batch_size=64, shuffle=True)
test_loader = torch.utils.data.DataLoader(test_dataset, batch_size=64, shuffle=True)
# train the model
for epoch in range(10):
for i, (images, labels) in enumerate(train_loader):
# move the images and labels to the device
images = images.to(device)
labels = labels.to(device)
# zero the parameter gradients
optimizer.zero_grad()
# forward pass
outputs = model(images)
loss = criterion(outputs, labels)
# backward pass and optimize
loss.backward()
optimizer.step()
# print statistics
if (i+1) % 100 == 0:
print(f'Epoch [{epoch+1}/{10}], Step [{i+1}/{len(train_loader)}], Loss: {loss.item():.4f}')
# evaluate the model
model.eval()
correct = 0
total = 0
with torch.no_grad():
for images, labels in test_loader:
# move the images and labels to the device
images = images.to(device)
labels = labels.to(device)
# forward pass
outputs = model(images)
_, predicted = torch.max(outputs.data, 1)
# calculate accuracy
total += labels.size(0)
correct += (predicted == labels).sum().item()
print(f'Accuracy: {100 * correct / total}%')
```
在这个示例中,我们使用了PyTorch实现了AlexNet模型,并使用mnist数据集进行训练和测试。我们首先定义了AlexNet模型,然后定义了训练所需的损失函数和优化器。接下来,我们使用torchvision加载mnist数据集,并创建了数据加载器。在训练循环中,我们将每个批次的图像和标签移动到设备上,并执行正向传递、反向传递和优化步骤。在测试循环中,我们将模型设置为评估模式,并计算测试集上的精度。
阅读全文