基于pytorch平台,利用MNIST数据集,实现经典的Lenet-5模型,同时加以改变构建自己的卷积模型,以分类的准确度和混淆矩阵为衡量指标,分析两个模型的分类精度。
时间: 2024-05-05 15:19:47 浏览: 142
好的,这是一个关于深度学习的问题,需要一些代码实现和数据分析。我会尽力回答,但是由于文字表述的局限性,可能无法完全准确地解答问题。若有不懂的地方,可以随时问我。
首先,我们需要了解一下Lenet-5模型的结构。它是一个经典的卷积神经网络,用于手写数字识别。它的结构如下所示:
```
Input -> Convolution -> ReLU -> Pooling -> Convolution -> ReLU -> Pooling -> Fully Connected -> ReLU -> Fully Connected
```
其中,Convolution层使用了6个5x5的卷积核,Pooling层使用了2x2的最大池化,Fully Connected层使用了两个隐藏层。
我们可以使用PyTorch框架来实现Lenet-5模型。代码如下:
```python
import torch
import torch.nn as nn
import torch.nn.functional as F
class LeNet5(nn.Module):
def __init__(self):
super(LeNet5, self).__init__()
self.conv1 = nn.Conv2d(in_channels=1, out_channels=6, kernel_size=5)
self.conv2 = nn.Conv2d(in_channels=6, out_channels=16, kernel_size=5)
self.fc1 = nn.Linear(in_features=16*4*4, out_features=120)
self.fc2 = nn.Linear(in_features=120, out_features=84)
self.fc3 = nn.Linear(in_features=84, out_features=10)
def forward(self, x):
x = F.relu(self.conv1(x))
x = F.max_pool2d(x, kernel_size=2)
x = F.relu(self.conv2(x))
x = F.max_pool2d(x, kernel_size=2)
x = x.view(-1, 16*4*4)
x = F.relu(self.fc1(x))
x = F.relu(self.fc2(x))
x = self.fc3(x)
return x
```
接下来,我们需要加载MNIST数据集,并训练模型。代码如下:
```python
import torchvision.datasets as dsets
import torchvision.transforms as transforms
train_dataset = dsets.MNIST(root='./data', train=True, transform=transforms.ToTensor(), download=True)
test_dataset = dsets.MNIST(root='./data', train=False, transform=transforms.ToTensor(), download=True)
train_loader = torch.utils.data.DataLoader(dataset=train_dataset, batch_size=100, shuffle=True)
test_loader = torch.utils.data.DataLoader(dataset=test_dataset, batch_size=100, shuffle=False)
model = LeNet5()
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.SGD(model.parameters(), lr=0.01)
for epoch in range(10):
for i, (images, labels) in enumerate(train_loader):
optimizer.zero_grad()
outputs = model(images)
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()
if (i+1) % 100 == 0:
print('Epoch [{}/{}], Step [{}/{}], Loss: {:.4f}'.format(epoch+1, 10, i+1, len(train_loader), loss.item()))
# 测试模型
model.eval()
with torch.no_grad():
correct = 0
total = 0
for images, labels in test_loader:
outputs = model(images)
_, predicted = torch.max(outputs.data, 1)
total += labels.size(0)
correct += (predicted == labels).sum().item()
print('Test Accuracy of the model on the 10000 test images: {} %'.format(100 * correct / total))
```
接下来,我们需要构建自己的卷积神经网络模型,并与Lenet-5模型进行比较。可以尝试增加卷积层数、卷积核大小、全连接层数等,以提高分类精度。代码如下:
```python
class MyConvNet(nn.Module):
def __init__(self):
super(MyConvNet, self).__init__()
self.conv1 = nn.Conv2d(in_channels=1, out_channels=32, kernel_size=3)
self.conv2 = nn.Conv2d(in_channels=32, out_channels=64, kernel_size=3)
self.conv3 = nn.Conv2d(in_channels=64, out_channels=128, kernel_size=3)
self.fc1 = nn.Linear(in_features=128*2*2, out_features=256)
self.fc2 = nn.Linear(in_features=256, out_features=10)
def forward(self, x):
x = F.relu(self.conv1(x))
x = F.max_pool2d(x, kernel_size=2)
x = F.relu(self.conv2(x))
x = F.max_pool2d(x, kernel_size=2)
x = F.relu(self.conv3(x))
x = x.view(-1, 128*2*2)
x = F.relu(self.fc1(x))
x = self.fc2(x)
return x
```
训练代码与Lenet-5模型相同,这里不再重复。训练完成后,我们可以使用混淆矩阵来分析两个模型的分类精度。混淆矩阵可以展示模型对于每个类别的分类情况,常用于评估多分类问题的性能。代码如下:
```python
from sklearn.metrics import confusion_matrix
import matplotlib.pyplot as plt
import seaborn as sns
# 测试模型
model.eval()
with torch.no_grad():
correct = 0
total = 0
predicted_labels = []
true_labels = []
for images, labels in test_loader:
outputs = model(images)
_, predicted = torch.max(outputs.data, 1)
total += labels.size(0)
correct += (predicted == labels).sum().item()
predicted_labels.extend(predicted.tolist())
true_labels.extend(labels.tolist())
print('Test Accuracy of the model on the 10000 test images: {} %'.format(100 * correct / total))
# 绘制混淆矩阵
cm = confusion_matrix(true_labels, predicted_labels)
plt.figure(figsize=(10,8))
sns.heatmap(cm, annot=True, cmap='Blues', fmt='g', xticklabels=[str(i) for i in range(10)], yticklabels=[str(i) for i in range(10)])
plt.xlabel('Predicted labels')
plt.ylabel('True labels')
plt.show()
```
通过混淆矩阵,我们可以看到每个类别的分类情况,以及哪些类别容易被混淆。根据混淆矩阵,我们可以针对性地改进模型,提高分类精度。
以上是关于如何基于pytorch平台,利用MNIST数据集,实现经典的Lenet-5模型,同时加以改变构建自己的卷积模型,以分类的准确度和混淆矩阵为衡量指标,分析两个模型的分类精度的回答。
阅读全文