🎓 A1Learner
🔍

PyTorch Installation

PyTorch installation puts the PyTorch library on a computer so Python can import it. The CPU build runs on the main processor and needs no graphics card.

Install the CPU build

Run this command in a terminal. A terminal is a window where typed commands run.

pip install --index-url https://download.pytorch.org/whl/cpu torch

Check the version

After installing, ask PyTorch for its version. The answer on this course computer is shown below.

import torch
print(torch.__version__)

Read the suffix

The +cpu suffix marks the CPU build. This check prints True for that build.

import torch
print(torch.__version__.endswith("+cpu"))

Example

import torch

print(torch.__version__)
sample = torch.tensor([1, 2, 3])
print(sample)

Key points

  • Use pip with the CPU index address to install the CPU build
  • Download size: about 196 MB for the torch package
  • Disk size: about 800 MB once installed
  • Use torch.__version__ to print the installed version
  • Suffix +cpu: the build runs on the main processor, with no graphics card needed

In this example, the program imports torch first. It prints torch.__version__ to show the installed build. The version ends with +cpu, so the CPU build is installed. It then builds a small tensor of whole numbers. The last line prints the tensor.

Note

This course uses the CPU build, even though a graphics-card build also exists.

Try it Yourself

Edit the code, then press Run.

Running is simulated · Python Playground

Exercise

What does the +cpu suffix in 2.14.1+cpu mean?