PyTorch is an open-source deep learning framework that is developed by Meta AI and is now part of the Linux Foundation.

PyTorch is designed to provide a flexible and efficient platform for building and training neural networks. It is widely used due to its dynamic computational graph, which allows users to modify the architecture during runtime, making debugging and experimentation easier.

PyTorch’s objective is to provide a more flexible, user-friendly deep learning framework that addresses the limitations of static computational graphs found in earlier tools like TensorFlow.

Prior to PyTorch, many frameworks used static computation graphs that require the entire model structure to be defined before training, making 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 faster prototyping, easier debugging, and more intuitive code.

Additionally, PyTorch seamlessly integrates with Python, encouraging 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 contributed to PyTorch’s rapid adoption, especially in academic research, where experimentation and iteration are crucial.

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 for creating a model for digit recognition, before then proceeding to train it.

Before you begin

Before you begin make sure Python3 is installed on your system. You can check this by running:

    

        
        
            python3 --version
        
    

The expected output is the Python version, for example:

    

        
        Python 3.11.2

        
    

If Python3 is not installed, download and install it from python.org .

Alternatively, you can also install Python3 using package managers such as Brew or APT.

If you are using Windows on Arm you can refer to the Python install guide .

Next, download and install Visual Studio Code .

Install PyTorch and additional Python packages

To prepare a virtual Python environment, install PyTorch, and the additional tools you will need for this Learning Path:

  1. Open a terminal or command prompt and navigate to your project directory.

  2. Create a virtual environment by running:

    

        
        
            python -m venv pytorch-env
        
    

This will create a virtual environment named pytorch-env.

  1. Activate the virtual environment:
  • On Windows:
    

        
        
            pytorch-env\Scripts\activate
        
    
  • On macOS or Linux:
    

        
        
            source pytorch-env/bin/activate
        
    

Once activated, you should see the virtual environment name in your terminal prompt.

  1. Install PyTorch using pip:
    

        
        
            pip install torch torchvision --index-url https://download.pytorch.org/whl/cpu
        
    
  1. Install torchsummary, Jupyter and IPython Kernel:
    

        
        
            pip install torchsummary
pip install jupyter
pip install ipykernel
        
    
  1. Register your virtual environment as a new kernel:
    

        
        
            python3 -m ipykernel install --user --name=pytorch-env
        
    
  1. Install the Jupyter Extension in VS Code:
  • Open VS Code and go to the Extensions view (click on the Extensions icon or press Ctrl+Shift+X).

  • Search for “Jupyter” and install the official Jupyter extension.

  • Optionally, also install the Python extension if you haven’t already, as it improves Python language support in VS Code.

To ensure everything is set up correctly:

  1. Open Visual Studio Code.
  2. Click New file, and select Jupyter Notebook .ipynb Support.
  3. Save the file as pytorch-digits.ipynb.
  4. Select the Python kernel you created earlier (pytorch-env). To do so, click Kernels in the top right corner. Then, click Jupyter Kernel…, and you will see the Python kernel as shown below:

Image Alt Text:img1

  1. In your Jupyter notebook, run the following code to verify PyTorch is working correctly:
    

        
        
            import torch
print(torch.__version__)
        
    

It will look as follows: Image Alt Text:img2

With your development environment created, you can proceed to creating a PyTorch model.

Back
Next