Created
January 6, 2025 06:07
-
-
Save schroneko/8a64cd7c585442f0e6ee361703a6e362 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import torch | |
| import torch.nn as nn | |
| import torch.nn.functional as F | |
| from torchvision import datasets, transforms | |
| class ConvNet(nn.Module): | |
| def __init__(self): | |
| super(ConvNet, self).__init__() | |
| # 畳み込み層 | |
| self.conv1 = nn.Conv2d(1, 32, 3, padding=1) | |
| self.conv2 = nn.Conv2d(32, 64, 3, padding=1) | |
| # 全結合層 | |
| self.fc1 = nn.Linear(64 * 7 * 7, 100) | |
| self.fc2 = nn.Linear(100, 200) | |
| self.fc3 = nn.Linear(200, 10) | |
| # ドロップアウト | |
| self.dropout = nn.Dropout(0.5) | |
| def forward(self, x): | |
| # 畳み込み層1 | |
| x = self.conv1(x) | |
| x = F.relu(x) | |
| x = F.max_pool2d(x, 2) | |
| # 畳み込み層2 | |
| x = self.conv2(x) | |
| x = F.relu(x) | |
| x = F.max_pool2d(x, 2) | |
| # 全結合層への変換 | |
| x = x.view(-1, 64 * 7 * 7) | |
| # 全結合層1 | |
| x = self.fc1(x) | |
| x = F.relu(x) | |
| x = self.dropout(x) | |
| # 全結合層2 | |
| x = self.fc2(x) | |
| x = F.relu(x) | |
| x = self.dropout(x) | |
| # 出力層 | |
| x = self.fc3(x) | |
| return F.log_softmax(x, dim=1) | |
| def train(model, device, train_loader, optimizer, epoch): | |
| model.train() | |
| for batch_idx, (data, target) in enumerate(train_loader): | |
| data, target = data.to(device), target.to(device) | |
| optimizer.zero_grad() | |
| output = model(data) | |
| loss = F.nll_loss(output, target) | |
| loss.backward() | |
| optimizer.step() | |
| if batch_idx % 100 == 0: | |
| print(f'Train Epoch {epoch} [{batch_idx * len(data)}/{len(train_loader.dataset)} ' | |
| f'({100. * batch_idx / len(train_loader):.0f}%)]\tLoss {loss.item():.6f}') | |
| 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 += F.nll_loss(output, target, reduction='sum').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'\nTest set: Average loss {test_loss:.4f}, ' | |
| f'Accuracy {correct}/{len(test_loader.dataset)} ' | |
| f'({100. * correct / len(test_loader.dataset):.2f}%)\n') | |
| def main(): | |
| # デバイスの設定 | |
| device = torch.device("cuda" if torch.cuda.is_available() else "cpu") | |
| # データローダーの設定 | |
| train_loader = torch.utils.data.DataLoader( | |
| datasets.MNIST('./data', train=True, download=True, | |
| transform=transforms.Compose([ | |
| transforms.ToTensor(), | |
| transforms.Normalize((0.1307,), (0.3081,)) | |
| ])), | |
| batch_size=100, shuffle=True) | |
| test_loader = torch.utils.data.DataLoader( | |
| datasets.MNIST('./data', train=False, | |
| transform=transforms.Compose([ | |
| transforms.ToTensor(), | |
| transforms.Normalize((0.1307,), (0.3081,)) | |
| ])), | |
| batch_size=100, shuffle=True) | |
| # モデルの設定 | |
| model = ConvNet().to(device) | |
| # オプティマイザの設定(モメンタム付きSGD) | |
| optimizer = torch.optim.SGD(model.parameters(), lr=0.01, momentum=0.9) | |
| # 学習の実行 | |
| for epoch in range(10): | |
| train(model, device, train_loader, optimizer, epoch) | |
| test(model, device, test_loader) | |
| if __name__ == '__main__': | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment