设计神经网络对 minist 数据集进行分类使模型在测试集上的准确率最高分析的时候要求 Loss-acc 图混淆矩阵分析每一类的识别准确率。并打印输出每一轮的loseacc以及准确率

首先,我们需要导入必要的库和加载数据集。这里我们使用PyTorch来构建和训练神经网络模型。

import torch
import torch.nn as nn
import torch.optim as optim
from torch.utils.data import DataLoader
from torchvision.datasets import MNIST
from torchvision.transforms import ToTensor
from torchsummary import summary

# 加载数据集
train_dataset = MNIST(root='./data', train=True, download=True, transform=ToTensor())
test_dataset = MNIST(root='./data', train=False, download=True, transform=ToTensor())

# 创建数据加载器
train_loader = DataLoader(train_dataset, batch_size=64, shuffle=True)
test_loader = DataLoader(test_dataset, batch_size=64, shuffle=False)

接下来,我们定义一个简单的神经网络模型,包含两个卷积层和两个全连接层。

class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        
        self.conv1 = nn.Conv2d(1, 32, kernel_size=3, stride=1, padding=1)
        self.conv2 = nn.Conv2d(32, 64, kernel_size=3, stride=1, padding=1)
        self.fc1 = nn.Linear(7*7*64, 128)
        self.fc2 = nn.Linear(128, 10)

    def forward(self, x):
        x = torch.relu(self.conv1(x))
        x = torch.relu(self.conv2(x))
        x = x.view(x.size(0), -1)
        x = torch.relu(self.fc1(x))
        x = self.fc2(x)
        return x

# 创建模型实例
model = Net()

# 打印模型结构
summary(model, (1, 28, 28))

接下来,我们定义损失函数和优化器。

criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(model.parameters(), lr=0.001)

然后,我们可以进行模型的训练和测试,并记录每一轮的损失和准确率。

device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model.to(device)

num_epochs = 10
train_losses = []
train_accs = []
test_accs = []

for epoch in range(num_epochs):
    # 训练模型
    model.train()
    train_loss = 0.0
    train_correct = 0
    for images, labels in train_loader:
        images, labels = images.to(device), labels.to(device)
        optimizer.zero_grad()
        outputs = model(images)
        loss = criterion(outputs, labels)
        loss.backward()
        optimizer.step()
        train_loss += loss.item()
        _, predicted = torch.max(outputs.data, 1)
        train_correct += (predicted == labels).sum().item()
    train_loss /= len(train_loader)
    train_acc = train_correct / len(train_dataset)
    train_losses.append(train_loss)
    train_accs.append(train_acc)
    
    # 测试模型
    model.eval()
    test_correct = 0
    with torch.no_grad():
        for images, labels in test_loader:
            images, labels = images.to(device), labels.to(device)
            outputs = model(images)
            _, predicted = torch.max(outputs.data, 1)
            test_correct += (predicted == labels).sum().item()
    test_acc = test_correct / len(test_dataset)
    test_accs.append(test_acc)
    
    # 打印每一轮的损失和准确率
    print(f"Epoch [{epoch+1}/{num_epochs}], Loss: {train_loss:.4f}, Train Acc: {train_acc:.4f}, Test Acc: {test_acc:.4f}")

最后,我们可以绘制Loss-acc图和混淆矩阵,并分析每一类的识别准确率。

import matplotlib.pyplot as plt
import numpy as np
from sklearn.metrics import confusion_matrix

# 绘制Loss-acc图
plt.figure(figsize=(10, 4))
plt.subplot(1, 2, 1)
plt.plot(range(1, num_epochs+1), train_losses, label='Train Loss')
plt.xlabel('Epoch')
plt.ylabel('Loss')
plt.legend()

plt.subplot(1, 2, 2)
plt.plot(range(1, num_epochs+1), train_accs, label='Train Acc')
plt.plot(range(1, num_epochs+1), test_accs, label='Test Acc')
plt.xlabel('Epoch')
plt.ylabel('Accuracy')
plt.legend()

plt.tight_layout()
plt.show()

# 计算混淆矩阵
model.eval()
predictions = []
targets = []
with torch.no_grad():
    for images, labels in test_loader:
        images, labels = images.to(device), labels.to(device)
        outputs = model(images)
        _, predicted = torch.max(outputs.data, 1)
        predictions.extend(predicted.cpu().numpy())
        targets.extend(labels.cpu().numpy())

cm = confusion_matrix(targets, predictions)

# 绘制混淆矩阵
plt.figure(figsize=(10, 8))
plt.imshow(cm, interpolation='nearest', cmap=plt.cm.Blues)
plt.title("Confusion Matrix")
plt.colorbar()
tick_marks = np.arange(10)
plt.xticks(tick_marks, [str(i) for i in range(10)], rotation=45)
plt.yticks(tick_marks, [str(i) for i in range(10)])
plt.xlabel('Predicted Label')
plt.ylabel('True Label')

# 分析每一类的识别准确率
class_accs = cm.diagonal() / cm.sum(axis=1)
for i in range(10):
    plt.text(i, i, f'{class_accs[i]*100:.2f}%', ha='center', va='center', color='red')

plt.tight_layout()
plt.show()

通过运行上述代码,我们可以得到Loss-acc图和混淆矩阵,并分析每一类的识别准确率。

标签: 科技


原文地址: https://cveoy.top/t/topic/jg6U 著作权归作者所有。请勿转载和采集!