MNIST(エムニスト)は28×28ピクセルの手書き画像で,ピクセル値は0から255までの整数である。
ここではPyTorchを使う。Tensorflow版と同じ処理であるが、PyTorchのほうが複雑になるので、ChatGPTのGPT-4とOpenAI APIのgpt-4-turbo-previewに書いてもらって、つぎはぎした。ちなみにMacならPyTorchに似たMLXを使った例 mlx-examples/mnist が速そう。
import torch import torch.nn as nn import torch.nn.functional as F import torch.optim as optim from torchvision import datasets, transforms from torch.utils.data import DataLoader from sklearn.metrics import confusion_matrix # Step 1: Load and preprocess the MNIST dataset # カレントディレクトリにdataというサブディレクトリを作ってそこにデータを取得 transform = transforms.Compose([ transforms.ToTensor(), transforms.Normalize((0.5,), (0.5,)) ]) train_dataset = datasets.MNIST('./data', train=True, download=True, transform=transform) test_dataset = datasets.MNIST('./data', train=False, download=True, transform=transform) train_loader = DataLoader(train_dataset, batch_size=64, shuffle=True) test_loader = DataLoader(test_dataset, batch_size=1000, shuffle=False) # Step 2: Define the neural network model class Net(nn.Module): def __init__(self): super(Net, self).__init__() self.flatten = nn.Flatten() self.fc1 = nn.Linear(28*28, 128) self.dropout = nn.Dropout(0.2) self.fc2 = nn.Linear(128, 10) def forward(self, x): x = self.flatten(x) x = F.relu(self.fc1(x)) x = self.dropout(x) x = self.fc2(x) return x model = Net() # Step 3: Set up the loss function and the optimizer loss_fn = nn.CrossEntropyLoss() optimizer = optim.Adam(model.parameters()) # Step 4: Train the model def train(model, train_loader, optimizer, loss_fn, epoch): model.train() for batch_idx, (data, target) in enumerate(train_loader): optimizer.zero_grad() output = model(data) loss = loss_fn(output, target) loss.backward() optimizer.step() print(f"Epoch {epoch}, Loss: {loss.item()}") for epoch in range(1, 6): train(model, train_loader, optimizer, loss_fn, epoch) # Step 5: Evaluate the model and calculate the confusion matrix def test(model, test_loader): model.eval() test_loss = 0 correct = 0 all_preds = [] all_targets = [] with torch.no_grad(): for data, target in test_loader: output = model(data) test_loss += loss_fn(output, target).item() pred = output.argmax(dim=1, keepdim=True) correct += pred.eq(target.view_as(pred)).sum().item() all_preds.extend(pred.view(-1).tolist()) all_targets.extend(target.view(-1).tolist()) test_loss /= len(test_loader.dataset) accuracy = 100. * correct / len(test_loader.dataset) print(f"Test set: Average loss: {test_loss:.4f}, Accuracy: {correct}/{len(test_loader.dataset)} ({accuracy:.0f}%)") return all_targets, all_preds targets, predictions = test(model, test_loader) print(confusion_matrix(targets, predictions))
Last modified: