Using PyTorch to construct neural network model for handwriting recognition

PyTorch is an open source machine learning library based on the Torch library, which is applied to computer vision and natural language processing applications. This chapter will learn from the installation, construction of the basic neural network through the Torch, and computing gradient.

How can we install Torch?

Torch for Linux, Windows, Mac and other development environments have a specific installation method, first search the official web pagePytorch.org/, as shown below we can root… During the installation, open the Anaconda command line and enter the following commands.

After installing the Torch, open the usual editor for testing

OK, we can see that we have successfully installed the Torch on the computer

The following code are edited in Jupyter NoteBook, conda and other installation methods are not described in this article

Define the neural network in “PYTORCH”

Deep learning algorithm is also known as neural network algorithm, which is a computing system composed of multiple layers of interconnected computing units. By passing data through these interconnected units, the neural network is able to learn how to approximate the calculations needed to transform input into bit output. In the Torch, you can use the Torch. Nn package to build a neural network.

The most commonly heard and most basic MNIST data set is the handwriting recognition data. Defining the neural network for the MNIST data set requires the following steps

1. The import libraries

2. Define and initialize the neural network

3. Specify the data set to build the model

4. Transfer data through the model for testing

From the perspective of application, the following content of the definition of neural network will not be described too much.

Import related library loading data

The libraries needed to build the neural network are torch. Nn and torch. Nn. Functional

import os
import torch
from torch import nn
from torch.utils.data import DataLoader
from torchvision import datasets, transforms
Copy the code

With the modules and classes described above, torch. Nn helps us create and train neural networks that contain forward(input) and return output.

Defines, initializes the neural network

The neural network we defined will help us identify the image, using the convolution built into PyTorch. The convolution process adds each element of the image to local Neighbors, weighted by the kernel or small matrix ratio, which will help us extract certain features (edge detection, sharpness, blur, etc.) from the input image.

A class that defines a Net model has two requirements. The first is to write an __init__ reference nn.Moudle. This function is where you define the full connection layer in your neural network.

Using convolution, we output an image channel from the constructed neural network model, output the target matching the number from 0 to 9 of 10 tags, the following build the traditional MNIST algorithm

class Net(nn.Module) :
  def __init__(self) :
    super(Net, self).__init__()

    # First 2D convolutional layer, taking in 1 input channel (image),
    # outputting 32 convolutional features, with a square kernel size of 3
    self.conv1 = nn.Conv2d(1.32.3.1)
    # Second 2D convolutional layer, taking in the 32 input layers,
    # outputting 64 convolutional features, with a square kernel size of 3
    self.conv2 = nn.Conv2d(32.64.3.1)

    # Designed to ensure that adjacent pixels are either all 0s or all active
    # with an input probability
    self.dropout1 = nn.Dropout2d(0.25)
    self.dropout2 = nn.Dropout2d(0.5)

    # First fully connected layer
    self.fc1 = nn.Linear(9216.128)
    # Second fully connected layer that outputs our 10 labels
    self.fc2 = nn.Linear(128.10)

my_nn = Net()
print(my_nn)
Copy the code

As shown in the code, the constructed three-layer neural network, the first two-dimensional receiving layer injects image data and outputs 32 features with the square kernel size of 3; the second two-dimensional convolutional layer injects 32 groups of data and obtains 64 features with the square kernel size of 3

Training by specifying data transfer

We have completed the definition of the neural network, and now we will use the data for training. In building the model with PyTorch, we only need to define the Foward function and pass the data to the calculation graph, which will represent our feedforward algorithm.

class Net(nn.Module) :
  def __init__(self) :
    super(Net, self).__init__()
    self.conv1 = nn.Conv2d(1.32.3.1)
    self.conv2 = nn.Conv2d(32.64.3.1)
    self.dropout1 = nn.Dropout2d(0.25)
    self.dropout2 = nn.Dropout2d(0.5)
    self.fc1 = nn.Linear(9216.128)
    self.fc2 = nn.Linear(128.10)

  # x represents our data
  def forward(self, x) :
    # Pass data through conv1
    x = self.conv1(x)
    # Use the rectified-linear activation function over x
    x = F.relu(x)

    x = self.conv2(x)
    x = F.relu(x)

    # Run max pooling over x
    x = F.max_pool2d(x, 2)
    # Pass data through dropout1
    x = self.dropout1(x)
    # Flatten x with start_dim=1
    x = torch.flatten(x, 1)
    # Pass data through fc1
    x = self.fc1(x)
    x = F.relu(x)
    x = self.dropout2(x)
    x = self.fc2(x)

    # Apply softmax to x
    output = F.log_softmax(x, dim=1)
    return output
Copy the code

Refer to the development documentation:Pytorch.org/tutorials/b…

Recommended reading

  • Differential operator method
  • Using PyTorch to construct neural network model for handwriting recognition
  • Neural network model and back propagation calculation were constructed using PyTorch
  • How to optimize model parameters and integrate models
  • TORCHVISION Target detection fine-tuning tutorial