Introduction
Prepare a PyTorch Development Environment
Create a PyTorch model for MNIST
About PyTorch Model Training
Perform Training and Save the Model
Deploy the Model for Inference
Learn about Inference on Android
Create an Android Application
Prepare the Test Data
Run the Application
Optimizing Neural Network Models in PyTorch
Create an optimized PyTorch model for MNIST
Run optimization
Update the Android application
Next Steps
Meta AI have designed an Open Source deep learning framework called PyTorch, that is now part of the Linux Foundation.
PyTorch provides a flexible and efficient platform for building and training neural networks. It has a dynamic computational graph that allows users to modify the architecture during runtime, making debugging and experimentation easier, and therefore making it popular among developers.
PyTorch provides a more flexible, user-friendly deep learning framework that reduces the limitations of static computational graphs found in earlier tools, such as TensorFlow.
Prior to PyTorch, many frameworks used static computational graphs that require the entire model structure to be defined before training, which makes experimentation and debugging cumbersome. PyTorch introduced dynamic computational graphs, also known as “define-by-run”, that allow the graph to be constructed dynamically as operations are executed. This flexibility significantly improves ease of use for researchers and developers, enabling:
PyTorch also seamlessly integrates with Python, which creates a native coding experience. Its deep integration with GPU acceleration also makes it a powerful tool for both research and production environments. This combination of flexibility, usability, and performance has ensured PyTorch’s rapid adoption, particularly in academic research, where experimentation and iteration are crucial activities.
A typical process for creating a feedforward neural network in PyTorch involves defining a sequential stack of fully-connected layers, which are also known as linear layers. Each layer transforms the input by applying a set of weights and biases, followed by an activation function like ReLU. PyTorch supports this process using the torch.nn module, where layers are easily defined and composed.
To create a model, users subclass the torch.nn.Module class, defining the network architecture in the init method, and implement the forward pass in the forward method. PyTorch’s intuitive API and support for GPU acceleration make it ideal for building efficient feedforward networks, particularly in tasks such as image classification and digit recognition.
In this Learning Path, you will explore how to use PyTorch to create and train a model for digit recognition.
Before you begin, make sure Python3 is installed on your system. You can check this by running:
python3 --version
You should then see the Python version printed in the output, for example:
Python 3.11.2
If Python3 is not installed, you can download and install it from python.org .
Alternatively, you can also install Python3 using package managers such as Homebrew or APT.
If you are using Windows on Arm, see the Python install guide .
Next, if you do not already have it, download and install Visual Studio Code .
To prepare a virtual Python environment, first you need to install PyTorch, and then move on to installing the additional tools that you will need for this Learning Path.
Open a terminal or command prompt, and navigate to your project directory.
Create a virtual environment by running:
python -m venv pytorch-env
This creates a virtual environment called pytorch-env
.
pytorch-env\Scripts\activate
source pytorch-env/bin/activate
Once activated, you can see the virtual environment name (pytorch-env)
before your terminal prompt.
pip install torch torchvision --index-url https://download.pytorch.org/whl/cpu
pip install torchsummary
pip install jupyter
pip install ipykernel
python3 -m ipykernel install --user --name=pytorch-env
Open VS Code and go to the Extensions view, by clicking on the Extensions icon or pressing Ctrl+Shift+X.
Search for “Jupyter” and install the official Jupyter extension.
Optionally, also install the Python extension if you have not already, as it improves Python language support in VS Code.
To ensure everything is set up correctly, follow these next steps:
Open Visual Studio Code.
Click New file, and select Jupyter Notebook .ipynb Support
.
Save the file as pytorch-digits.ipynb
.
Select the Python kernel you created earlier, pytorch-env
. To do so, click Kernels in the top right-hand corner. Then, click Jupyter Kernel…, and you will see the Python kernel as shown below:
import torch
print(torch.__version__)
It will look as follows:
Now you have set up your development environment, you can move on to creating a PyTorch model.