MNIST数据集上神经网络性能分析代码
时间: 2023-07-22 20:12:33 浏览: 119
以下是一个使用PyTorch框架实现的MNIST数据集上神经网络性能分析代码,包括了卷积神经网络(CNN)和全连接神经网络(FCN)两种模型结构以及不同的参数配置和优化算法:
```python
import torch
from torch import nn
from torch.optim import Adam
from torchvision.datasets import MNIST
from torchvision.transforms import ToTensor
from torch.utils.data import DataLoader
# 加载MNIST数据集
train_dataset = MNIST(root='data/', train=True, transform=ToTensor(), download=True)
test_dataset = MNIST(root='data/', train=False, transform=ToTensor(), download=True)
# 定义CNN模型
class CNNModel(nn.Module):
def __init__(self):
super(CNNModel, self).__init__()
self.conv1 = nn.Conv2d(1, 32, kernel_size=3)
self.conv2 = nn.Conv2d(32, 64, kernel_size=3)
self.fc1 = nn.Linear(1600, 128)
self.fc2 = nn.Linear(128, 10)
def forward(self, x):
x = nn.functional.relu(self.conv1(x))
x = nn.functional.relu(self.conv2(x))
x = x.view(-1, 1600)
x = nn.functional.relu(self.fc1(x))
x = self.fc2(x)
return x
# 定义FCN模型
class FCNModel(nn.Module):
def __init__(self):
super(FCNModel, self).__init__()
self.fc1 = nn.Linear(28*28, 128)
self.fc2 = nn.Linear(128, 10)
def forward(self, x):
x = x.view(-1, 28*28)
x = nn.functional.relu(self.fc1(x))
x = self.fc2(x)
return x
# 训练和测试函数
def train(model, train_loader, optimizer):
model.train()
for batch_idx, (data, target) in enumerate(train_loader):
optimizer.zero_grad()
output = model(data)
loss = nn.functional.cross_entropy(output, target)
loss.backward()
optimizer.step()
def test(model, test_loader):
model.eval()
test_loss = 0
correct = 0
with torch.no_grad():
for data, target in test_loader:
output = model(data)
test_loss += nn.functional.cross_entropy(output, target, reduction='sum').item()
pred = output.argmax(dim=1, keepdim=True)
correct += pred.eq(target.view_as(pred)).sum().item()
test_loss /= len(test_loader.dataset)
accuracy = 100. * correct / len(test_loader.dataset)
return test_loss, accuracy
# 训练和测试CNN模型
def train_cnn():
model = CNNModel()
optimizer = Adam(model.parameters(), lr=0.01)
train_loader = DataLoader(train_dataset, batch_size=64, shuffle=True)
test_loader = DataLoader(test_dataset, batch_size=64, shuffle=True)
for epoch in range(10):
train(model, train_loader, optimizer)
test_loss, accuracy = test(model, test_loader)
print('CNN Epoch: {}, Test Loss: {:.4f}, Accuracy: {:.2f}%'.format(epoch, test_loss, accuracy))
# 训练和测试FCN模型
def train_fcn():
model = FCNModel()
optimizer = Adam(model.parameters(), lr=0.001)
train_loader = DataLoader(train_dataset, batch_size=64, shuffle=True)
test_loader = DataLoader(test_dataset, batch_size=64, shuffle=True)
for epoch in range(10):
train(model, train_loader, optimizer)
test_loss, accuracy = test(model, test_loader)
print('FCN Epoch: {}, Test Loss: {:.4f}, Accuracy: {:.2f}%'.format(epoch, test_loss, accuracy))
if __name__ == '__main__':
train_cnn()
train_fcn()
```
在这个代码中,我们首先定义了CNN模型和FCN模型,并且分别实现了训练和测试函数。在训练函数中,我们使用交叉熵损失函数和Adam优化算法进行训练,而在测试函数中,我们计算了测试集上的损失和准确率。最后,我们分别调用train_cnn()和train_fcn()函数进行CNN和FCN模型的训练和测试,并输出了在测试集上的表现结果。
需要注意的是,在实现过程中,我们还需要对输入数据进行合适的预处理和数据加载等操作。此外,为了进一步提高模型性能,我们可以尝试使用更复杂的模型结构、更优化的算法和参数配置等策略,同时需要进行充分的实验验证和综合评估。
阅读全文