1.使用PyTorch实现LeNet网络 2.使用本地文件夹的 Fashion MNIST 对LeNet进行训练和测试,优化算法采用SGD或Adam,torch.optim.SGD 或 torch.optim.Adam 3.分别绘制训练和测试的损失函数曲线和分类正确率曲线 4.调节BatchSize、学习率,并依据测试损失曲线的拐点确定最佳模型,保存该模型 5.使用测试集测试所保存模型的性能,以混淆矩阵展示

时间: 2023-06-15 16:06:36 浏览: 70
好的,我来给你讲解一下使用PyTorch实现LeNet网络的具体步骤: 1.导入所需要的库 ```python import torch import torch.nn as nn import torch.optim as optim import torchvision import torchvision.transforms as transforms import matplotlib.pyplot as plt import numpy as np ``` 2.定义LeNet网络 ```python class LeNet(nn.Module): def __init__(self): super(LeNet, self).__init__() self.conv1 = nn.Conv2d(1, 6, 5) self.pool1 = nn.MaxPool2d(2, 2) self.conv2 = nn.Conv2d(6, 16, 5) self.pool2 = nn.MaxPool2d(2, 2) self.fc1 = nn.Linear(16 * 4 * 4, 120) self.fc2 = nn.Linear(120, 84) self.fc3 = nn.Linear(84, 10) def forward(self, x): x = self.pool1(torch.relu(self.conv1(x))) x = self.pool2(torch.relu(self.conv2(x))) x = x.view(-1, 16 * 4 * 4) x = torch.relu(self.fc1(x)) x = torch.relu(self.fc2(x)) x = self.fc3(x) return x ``` 3.加载数据集 ```python batch_size = 64 transform = transforms.Compose( [transforms.ToTensor(), transforms.Normalize((0.5,), (0.5,))]) trainset = torchvision.datasets.FashionMNIST(root='./data', train=True, download=True, transform=transform) trainloader = torch.utils.data.DataLoader(trainset, batch_size=batch_size, shuffle=True, num_workers=2) testset = torchvision.datasets.FashionMNIST(root='./data', train=False, download=True, transform=transform) testloader = torch.utils.data.DataLoader(testset, batch_size=batch_size, shuffle=False, num_workers=2) ``` 4.定义优化器和损失函数 ```python net = LeNet() criterion = nn.CrossEntropyLoss() optimizer = optim.SGD(net.parameters(), lr=0.001, momentum=0.9) ``` 5.训练模型并绘制损失函数曲线和分类正确率曲线 ```python epochs = 10 train_losses = [] train_accs = [] test_losses = [] test_accs = [] for epoch in range(epochs): running_loss = 0.0 running_acc = 0.0 for i, data in enumerate(trainloader, 0): inputs, labels = data optimizer.zero_grad() outputs = net(inputs) loss = criterion(outputs, labels) loss.backward() optimizer.step() running_loss += loss.item() _, predicted = torch.max(outputs.data, 1) running_acc += (predicted == labels).sum().item() train_loss = running_loss / len(trainloader.dataset) train_acc = running_acc / len(trainloader.dataset) train_losses.append(train_loss) train_accs.append(train_acc) test_loss = 0.0 test_acc = 0.0 with torch.no_grad(): for data in testloader: inputs, labels = data outputs = net(inputs) loss = criterion(outputs, labels) test_loss += loss.item() _, predicted = torch.max(outputs.data, 1) test_acc += (predicted == labels).sum().item() test_loss /= len(testloader.dataset) test_acc /= len(testloader.dataset) test_losses.append(test_loss) test_accs.append(test_acc) print('Epoch [%d/%d], Train Loss: %.4f, Train Accuracy: %.4f, Test Loss: %.4f, Test Accuracy: %.4f' % (epoch + 1, epochs, train_loss, train_acc, test_loss, test_acc)) plt.plot(train_losses, label='Training Loss') plt.plot(test_losses, label='Testing Loss') plt.legend() plt.show() plt.plot(train_accs, label='Training Accuracy') plt.plot(test_accs, label='Testing Accuracy') plt.legend() plt.show() ``` 6.保存最佳模型 ```python best_test_loss = min(test_losses) best_epoch = test_losses.index(best_test_loss) print('Best Epoch: %d, Best Test Loss: %.4f' % (best_epoch + 1, best_test_loss)) torch.save(net.state_dict(), 'best_model.pth') ``` 7.使用混淆矩阵展示模型性能 ```python confusion_matrix = torch.zeros(10, 10) with torch.no_grad(): for data in testloader: inputs, labels = data outputs = net(inputs) _, predicted = torch.max(outputs, 1) for i in range(len(labels)): confusion_matrix[labels[i]][predicted[i]] += 1 classes = ('T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat', 'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot') plt.figure(figsize=(10, 10)) plt.imshow(confusion_matrix, interpolation='nearest', cmap=plt.cm.Blues) plt.title('Confusion Matrix') plt.colorbar() tick_marks = np.arange(len(classes)) plt.xticks(tick_marks, classes, rotation=45) plt.yticks(tick_marks, classes) plt.xlabel('Predicted label') plt.ylabel('True label') plt.show() ``` 通过以上步骤,我们就可以完成使用PyTorch实现LeNet网络的训练和测试,并且得到最佳模型,最后使用混淆矩阵展示模型性能。

相关推荐

最新推荐

recommend-type

Pytorch使用MNIST数据集实现CGAN和生成指定的数字方式

今天小编就为大家分享一篇Pytorch使用MNIST数据集实现CGAN和生成指定的数字方式,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
recommend-type

用Pytorch训练CNN(数据集MNIST,使用GPU的方法)

今天小编就为大家分享一篇用Pytorch训练CNN(数据集MNIST,使用GPU的方法),具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
recommend-type

pytorch实现对输入超过三通道的数据进行训练

今天小编就为大家分享一篇pytorch实现对输入超过三通道的数据进行训练,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
recommend-type

使用pytorch实现论文中的unet网络

设计神经网络的一般步骤: 1. 设计框架 2. 设计骨干网络 Unet网络设计的步骤: 1. 设计Unet网络工厂模式 2. 设计编解码结构 3. 设计卷积模块 4. unet实例模块 Unet网络最重要的特征: 1. 编解码结构。 2. 解码结构,...
recommend-type

pytorch实现mnist分类的示例讲解

今天小编就为大家分享一篇pytorch实现mnist分类的示例讲解,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

list根据id查询pid 然后依次获取到所有的子节点数据

可以使用递归的方式来实现根据id查询pid并获取所有子节点数据。具体实现可以参考以下代码: ``` def get_children_nodes(nodes, parent_id): children = [] for node in nodes: if node['pid'] == parent_id: node['children'] = get_children_nodes(nodes, node['id']) children.append(node) return children # 测试数
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。