MNIST数据集下载地址:tensorflow-tutorial-samples/mnist/data_set at master · geektutu/tensorflow-tutorial-samples · GitHub
数据集存放和dataset的参数设置:
完整的MNIST分类代码:
import torch import torch.nn as nn import torch.optim as optim from torchvision import datasets, transforms from torch.utils.data import DataLoader from torch.nn import Sequential # 构建卷积神经网络 class Simple_CNN(nn.Module): # 从父类 nn.Module 继承 def __init__(self): # 相当于 C++ 的构造函数 # super() 函数是用于调用父类(超类)的一个方法,是用来解决多重继承问题的 super(Simple_CNN, self).__init__() # 第一层卷积层。Sequential(意为序列) 括号内表示要进行的操作 self.conv1 = Sequential( nn.Conv2d(in_channels=1, out_channels=64, kernel_size=3, stride=1, padding=1), nn.BatchNorm2d(64), nn.ReLU(), nn.MaxPool2d(kernel_size=2, stride=2) ) # 第二卷积层 self.conv2 = Sequential( nn.Conv2d(in_channels=64, out_channels=128, kernel_size=3, stride=1, padding=1), nn.BatchNorm2d(128), nn.ReLU(), nn.MaxPool2d(kernel_size=2, stride=2) ) # 全连接层(Dense,密集连接层) self.dense = Sequential( nn.Linear(7 * 7 * 128, 1024), nn.ReLU(), nn.Dropout(p=0.5), nn.Linear(1024, 10) ) def forward(self, x): # 正向传播 x1 = self.conv1(x) x2 = self.conv2(x1) x = x2.view(-1, 7 * 7 * 128) x = self.dense(x) return x # 训练模型 def train(model, device, train_loader, optimizer, criterion, epochs): model.train() for epoch in range(epochs): for data, target in train_loader: data, target = data.to(device), target.to(device) optimizer.zero_grad() output = model(data) loss = criterion(output, target) loss.backward() optimizer.step() print(f'Epoch {epoch+1}, Loss: {loss.item()}') # 测试模型 def test(model, device, test_loader): model.eval() test_loss = 0 correct = 0 with torch.no_grad(): for data, target in test_loader: data, target = data.to(device), target.to(device) output = model(data) test_loss += criterion(output, target).item() pred = output.argmax(dim=1, keepdim=True) correct += pred.eq(target.view_as(pred)).sum().item() test_loss /= len(test_loader.dataset) print(f'Test set: Average loss: {test_loss:.4f}, \ Accuracy: {correct}/{len(test_loader.dataset)} ({100. * correct / len(test_loader.dataset):.0f}%)') if __name__ == "__main__": # 设置设备 device = torch.device("cuda" if torch.cuda.is_available() else "cpu") # 加载数据集 transform = transforms.Compose([ transforms.ToTensor(), transforms.Normalize((0.5,), (0.5,)) ]) train_dataset = datasets.MNIST(root='dataset/mnist/', train=True, download=True, transform=transform) test_dataset = datasets.MNIST(root='dataset/mnist/', train=False, download=True, transform=transform) train_loader = DataLoader(dataset=train_dataset, batch_size=64, shuffle=True) test_loader = DataLoader(dataset=test_dataset, batch_size=64, shuffle=False) # 初始化网络和优化器 model = Simple_CNN().to(device) optimizer = optim.Adam(model.parameters(), lr=0.001) criterion = nn.CrossEntropyLoss() # 运行训练和测试 epochs = 5 train(model, device, train_loader, optimizer, criterion, epochs) test(model, device, test_loader) print('done')
实验结果:
标签:datasets,nn,dataset,device,pytorch,train,test,loader,MNIST From: https://www.cnblogs.com/picassooo/p/18460054