guog算法笔记

V1

2023/04/06阅读：19主题：默认主题

# torch实现经典数字集案例

## PyTorch实现

``import torchimport torch.nn as nnimport torch.optim as optimimport torchvision.datasets as datasetsimport torchvision.transforms as transforms``

``# 定义数据预处理transform = transforms.Compose([transforms.ToTensor(),                                transforms.Normalize((0.1307,), (0.3081,))])# 加载MNIST数据集train_dataset = datasets.MNIST(root='./data', train=True, download=True, transform=transform)test_dataset = datasets.MNIST(root='./data', train=False, download=True, transform=transform)# 创建数据加载器train_loader = torch.utils.data.DataLoader(train_dataset, batch_size=64, shuffle=True)test_loader = torch.utils.data.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=5)        self.conv2 = nn.Conv2d(32, 64, kernel_size=5)        self.pool = nn.MaxPool2d(2)        self.fc1 = nn.Linear(1024, 128)        self.fc2 = nn.Linear(128, 10)    def forward(self, x):        x = self.pool(torch``

``model = Net()criterion = nn.CrossEntropyLoss()optimizer = optim.SGD(model.parameters(), lr=0.01, momentum=0.5)``

``def train(model, train_loader, criterion, optimizer, epoch):    model.train()    for batch_idx, (data, target) in enumerate(train_loader):        optimizer.zero_grad()        output = model(data)        loss = criterion(output, target)        loss.backward()        optimizer.step()        if batch_idx % 100 == 0:            print('Train Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}'.format(                epoch, batch_idx * len(data), len(train_loader.dataset),                100. * batch_idx / len(train_loader), loss.item()))``

``def test(model, test_loader, criterion):    model.eval()    test_loss = 0    correct = 0    with torch.no_grad():        for data, target in test_loader:            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('\nTest set: Average loss: {:.4f}, Accuracy: {}/{} ({:.0f}%)\n'.format(        test_loss, correct, len(test_loader.dataset),        100. * correct / len(test_loader.dataset)))``

``for epoch in range(1, 11):    train(model, train_loader, criterion, optimizer, epoch)    test(model, test_loader, criterion)``

``Train Epoch: 1 [0/60000 (0%)]	Loss: 2.299191Train Epoch: 1 [6400/60000 (11%)]	Loss``

``import torchimport torch.nn as nnimport torch.nn.functional as Fimport torch.optim as optimfrom torchvision import datasets, transformsclass Net(nn.Module):    def __init__(self):        super(Net, self).__init__()        self.conv1 = nn.Conv2d(1, 10, kernel_size=5)        self.conv2 = nn.Conv2d(10, 20, kernel_size=5)        self.fc1 = nn.Linear(320, 50)        self.fc2 = nn.Linear(50, 10)    def forward(self, x):        x = F.relu(F.max_pool2d(self.conv1(x), 2))        x = F.relu(F.max_pool2d(self.conv2(x), 2))        x = x.view(-1, 320)        x = F.relu(self.fc1(x))        x = self.fc2(x)        return F.log_softmax(x, dim=1)def train(model, train_loader, criterion, optimizer, epoch):    model.train()    for batch_idx, (data, target) in enumerate(train_loader):        optimizer.zero_grad()        output = model(data)        loss = criterion(output, target)        loss.backward()        optimizer.step()        if batch_idx % 100 == 0:            print('Train Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}'.format(                epoch, batch_idx * len(data), len(train_loader.dataset),                100. * batch_idx / len(train_loader), loss.item()))def test(model, test_loader, criterion):    model.eval()    test_loss = 0    correct = 0    with torch.no_grad():        for data, target in test_loader:            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('\nTest set: Average loss: {:.4f}, Accuracy: {}/{} ({:.0f}%)\n'.format(        test_loss, correct, len(test_loader.dataset),        100. * correct / len(test_loader.dataset)))if __name__ == '__main__':    torch.manual_seed(42)    transform = transforms.Compose([        transforms.ToTensor(),        transforms.Normalize((0.1307,), (0.3081,))    ])    train_dataset = datasets.MNIST('data/', train=True, download=True, transform=transform)    test_dataset = datasets.MNIST('data/', train=False, download=True, transform=transform)    train_loader = torch.utils.data.DataLoader(train_dataset, batch_size=64, shuffle=True)    test_loader = torch.utils.data.DataLoader(test_dataset, batch_size=1000)    model = Net()    criterion = nn.CrossEntropyLoss()    optimizer = optim.SGD(model.parameters(), lr=0.01, momentum=0.5)    for epoch in range(1, 11):        train(model, train_loader, criterion, optimizer, epoch)        test(model, test_loader, criterion)``

V1