Raspberry Pi

How to Install PyTorch on Raspberry Pi

PyTorch is an open-source machine learning framework Python and Torch library based for applications including computer vision and natural language processing. It was introduced by the Facebook artificial intelligence group named as Meta AI after it release, it’s now become part of different operating systems, including Windows, macOS, and Linux.

You may need PyTorch on Raspberry Pi to perform different machine learning-based projects and you can install it on Raspberry Pi through this article.

Install PyTorch on Raspberry Pi

PyTorch is a Python-based framework and it can be installed from the Python Package Manager called pip. Follow the below-given steps to install PyTorch through pip.

Step 1: Install pip

First, you have to check whether pip is installed on Raspberry Pi and for this purpose, you have to run the following command that will install pip if it’s not installed.

$ sudo apt install python3-pip

To confirm pip installation, run the below-given “version” command:

$ pip3 --version

Step 2: Install PyTorch Through pip

Go to the official PyTorch website and select the “Pip” section. You will see a terminal command, which you have to execute on the Raspberry Pi terminal.

Copy the above highlighted command and run it on the terminal to install the PyTorch framework.

$ pip3 install torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/cu116

The process takes a few seconds to install the PyTorch on Raspberry Pi and once it’s done, you must ensure whether it is running on Raspberry Pi.

For this purpose, you must install “numpy” through the below-given command because it will be used in our example:

$ pip3 install numpy

Now, run the following command to open Python on the terminal:

$ python3

Then use the following command to import Torch library:

import torch

Now I am creating a three-dimensional vector using Tensor through the following code:

torch.Tensor([0, 0, 0])

If the above output returns “tensor([0., 0., 0])”, it means PyTorch is successfully operating on a Raspberry Pi system.

Conclusion

PyTorch is a machine-learning-based Python framework for computer vision and natural image processing and you can install it from the Python Package Manager (pip). After the installation, you can start using PyTorch in Python by importing the “torch” library just before adding a machine learning code.

About the author

Awais Khan

I'm an Engineer and an academic researcher by profession. My interest for Raspberry Pi, embedded systems and blogging has brought me here to share my knowledge with others.