In this tutorial, you will learn how to quickly train a neural network using PyTorch to learn how to recognize handwritten numbers.

This article uses Didi Cloud Notebook as the development environment. Didi Cloud Notebook service integrates CUDA, CuDNN, Python, TensorFlow, Pytorch, MxNet, Keras and other deep learning frameworks without user installation.


Part.1

1. Purchase the Notebook service

To buy the Notebook service, register with Didi Cloud and authenticate with your real name

Registration steps:

  • Register instant red envelope 500 yuan
  • Real-name authentication

2. Go to the Console Notebook page and click the Create Notebook instance button

3. Select basic configuration:

  • Select payment mode: currently, only time is supported.
  • Select available areas: Select areas close to your customers, including Guangzhou 1 and 2.
  • Select specifications: Select cpus, Gpus, graphics cards, and memory as required.
  • Select image: Jupyter Notebook image and Jupyter Lab image are provided, here choose >jupyter-lab-v1.
  • Set the system disk size. The system disk size ranges from 80GB to 500GB.

4. Name and label

  • Enter the Notebook name.
  • Enter the label key and key value, and click Add button to add multiple labels.

5. Access the Notebook

  • Go to my Notebook page and click open Notebook in the action column.
  • Go to the Notebook details page and click to open Notebook.


Part.2

MNIST handwritten number recognition program is constructed

import matplotlib.pyplot as plt  
import numpy as np
import torch  
import torch.nn as nn  
import torch.nn.functional as F  
import torch.optim as optim  
import torchvision


from torchvision import datasets, transformsCopy the code

Download the classic MNIST dataset

device = torch.device("cuda" if torch.cuda.is_available() else "cpu")


# Training set Dataloader
train_loader = torch.utils.data.DataLoader(
    datasets.MNIST(root='. ', train=True, download=True, transform=transforms.Compose([transforms.ToTensor(), transforms.Normalize((0.1307,)), (0.3081,))), shuffle=True, num_workers=4)# Test set Dataloader
test_loader = torch.utils.data.DataLoader(
    datasets.MNIST(root='. ', train=False, transform=transforms.Compose([transforms.ToTensor(), transforms.Normalize((0.1307,), (0.3081,)))), batch_size=64, shuffle=True, num_workers=4)Copy the code

Here, we use a 4-layer CNN (convolutional neural network), and the network structure is CONV-CONV-FC-FC

class 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):
        # Perform the usual forward pass
        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)


model = Net().to(device)Copy the code

Now we use the SGD(Random gradient descent) algorithm to train the model to learn the classification task in a supervised manner

Optimizer = optim.sgd (model.parameters(), lr=0.01) def train(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 % 1 == 0:
            print('\rTrain Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}'.format(
                epoch, batch_idx * len(data), len(train_loader.dataset),
                100. * batch_idx / len(train_loader), loss.item()), end=' ')


def test():
    with torch.no_grad():
        model.eval()
        test_loss = 0
        correct = 0
        for data, target in test_loader:
            data, target = data.to(device), target.to(device)
            output = model(data)
            test_loss += F.nll_loss(output, target).item()
            pred = output.max(1, keepdim=True)[1]
            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)))Copy the code

At the beginning of the training, the model was tested once for each trained epoch. Within 20 epochs, the model accuracy could reach 98.7%

epochs = 20
for epoch in range(1, epochs + 1):
    train(epoch)
    test()
    
Train Epoch: 1 [29984/60000 (100%)]  Loss: 0.130790
Test set: Average loss: 0.0033, Accuracy: 9370/10000 (94%)

Train Epoch: 2 [29984/60000 (100%)]  Loss: 0.212607
Test set: Average loss: 0.0020, Accuracy: 9594/10000 (96%)

Train Epoch: 3 [29984/60000 (100%)]  Loss: 0.054339
Test set: Average loss: 0.0016, Accuracy: 9673/10000 (97%)

Train Epoch: 4 [29984/60000 (100%)]  Loss: 0.085429
Test set: Average loss: 0.0012, Accuracy: 9766/10000 (98%)

Train Epoch: 5 [29984/60000 (100%)]  Loss: 0.084620
Test set: Average loss: 0.0010, Accuracy: 9800/10000 (98%)

Train Epoch: 6 [29984/60000 (100%)]  Loss: 0.053965
Test set: Average loss: 0.0009, Accuracy: 9826/10000 (98%)

Train Epoch: 7 [29984/60000 (100%)]  Loss: 0.098088
Test set: Average loss: 0.0008, Accuracy: 9826/10000 (98%)

Train Epoch: 8 [29184/60000 (49%)]  Loss: 0.008589Copy the code


In order to improve the efficiency of research and development, it is all dry technology didi cloud technology salon registration!

Pay attention to didi cloud official account:

Reply “class” to get free registration

Reply “Server” to get a free one-month experience of cloud server entry