How to Install PyTorch on Windows and Linux

PyTorch has become one of the most popular open-source machine learning frameworks for natural language processing, computer vision, and other AI applications. Created by Facebook‘s AI research group, it offers Python support and ease of use that has helped it be adopted by companies like Tesla, Uber, Sony, Amazon and more.

In this comprehensive guide, I‘ll provide step-by-step instructions on how to properly install the latest version of PyTorch on both Windows and Linux operating systems. You‘ll learn PyTorch‘s capabilities, how to leverage GPUs for faster training, and tips for productionizing models.

An Overview of PyTorch

Before we dive into the installation, let‘s briefly overview what PyTorch is and why you may want to use it.

PyTorch is an open source machine learning library for Python, based on Torch, used for applications such as:

  • Computer Vision – Image recognition, object detection
  • Natural Language Processing – Sentiment analysis, text generation
  • Reinforcement Learning
  • Audio Processing
  • Generative Adversarial Networks
  • Biomedical Applications
  • Autonomous Vehicles

Some of the key capabilities include:

  • Hybrid frontend that allows for easy Python integration and debugging
  • Strong GPU acceleration functionality
  • Distributed training across clusters and cloud services
  • Flexible neural network building with dynamic computation graphs

PyTorch is great for getting models up and running quickly and iterating on research ideas. It has gained popularity over TensorFlow due to its beginner friendliness. Yet it offers advanced features for complex neural architectures as well.

Leading technology companies using PyTorch include Facebook, Google, Twitter, NVIDIA, Uber, IBM, Salesforce, Amazon, and Tesla. It powers many state-of-the-art AI applications across industries.

Now that you have an overview of PyTorch‘s capabilities, let‘s jump in and get PyTorch installed on your system!

Prerequisites

Before installing PyTorch, you should ensure your system meets the following prerequisites:

Operating Systems

  • Windows 8 or newer
  • Linux distributions – Ubuntu 16.04+, CentOS 7+, etc.

Software

  • Python version 3.6 or higher
  • Anaconda Distribution
  • PIP (for GPU installs)

Hardware

  • CPU: Intel or AMD processor with 2+ cores
  • RAM: 8GB+ DRAM recommended
  • Storage: 400MB+ free space
  • GPU (optional): NVIDIA GPU with CUDA compute capability 3.5+ and associated drivers for accelerated training

If you don‘t have Anaconda installed yet, refer to this detailed guide on installing Anaconda before proceeding.

With those prerequisites covered, let‘s kick off the installation!

Installing PyTorch on Linux

For Linux-based distributions like Ubuntu or CentOS, we‘ll walk through the installation process using Conda to set up a virtual environment and manage the PyTorch packages.

Updating System Packages

As a best practice, let‘s start off by updating all system packages:

sudo apt update && sudo apt upgrade

This will fetch the latest updates from our package manager.

Launching the Conda Environment

With our system now up to date, we can create and activate a Conda environment to isolate our PyTorch installation. This will also pin a specific Python version:

conda create -n pytorch python=3.7
conda activate pytorch

You should see the environment name change to confirm you‘re now operating inside this virtual env.

Running the PyTorch Installer

Next, we‘ll leverage PyTorch‘s handy GUI-based installer to generate the command we need to install PyTorch properly on our system.

Go to the PyTorch website and scroll down until you see the following wizard:

PyTorch Installation Wizard

Based on dropping down my Linux OS, Conda package manager, CPU target, and Python language, here is the output:

Filled out PyTorch Install Wizard

As you can see, it detects my Ubuntu 18 system, allows me to customize compute target and other options, and generates the conda install command needed.

Let‘s copy and run that installation command:

conda install pytorch torchvision torchaudio cpuonly -c pytorch 

Follow any prompts to complete the installation. Once finished, we can confirm PyTorch is ready to use!

Verifying the Installation

To validate our installation was successful, let‘s open the Python interpreter and import PyTorch:

python
>>> import torch 

If no errors occur during the import, then PyTorch has been successfully installed and is ready for building machine learning models!

Now that we have it up and running on Linux, let‘s also go through the Windows installation process.

Installing PyTorch on Windows

For getting PyTorch running on Windows machines, we‘ll again leverage our Anaconda Prompt terminal and Conda environments to manage the process.

Launching Anaconda Prompt

First, launch the Anaconda Prompt application from your Start menu. An terminal window should open up – this will be our command line for executing all the install steps.

Creating Our Conda Environment

Just like on Linux, we want to initialize a new Conda environment to isolate our PyTorch packages and dependencies:

conda create -n pytorch python=3.7

Activate the new virtual environment:

conda activate pytorch

Now PyTorch can be installed without worrying about version conflicts.

Running the PyTorch GUI Installer

Like before, head to the PyTorch website and access the installation wizard to generate our install command.

Select Windows as the OS, Conda as package manager, CPU build, and Python:

windows-pytorch-install

As you can see, this customized wizard outputs the right Conda install command for our Windows machine under the active Conda environment.

Let‘s copy and run that generated install command:

conda install pytorch torchvision torchaudio cpuonly -c pytorch

Once the installation completes, we‘re ready to validate everything worked!

Verifying the Installation

With PyTorch installed to our environment, open up the python interactive shell:

python

Within the shell, import the torch package:

>>> import torch

If no errors get thrown during the import, you have successfully installed PyTorch on your Windows machine!

You now have PyTorch ready to power your NLP, computer vision, and other machine learning projects built in Python.

Using PyTorch for AI Applications

Now that you have PyTorch available in your environment, I wanted to provide a brief overview of common use cases and examples.

Importing PyTorch

All PyTorch code starts by importing the core packages:

import torch 
import torch.nn as nn
import torchvision

This gives you access to tensor computation, neural networks, computer vision datasets and more.

Creating a Simple Neural Network

Here is an example of a simple convolutional neural network for image classification:

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

        self.conv1 = nn.Conv2d(1, 32, 5) 
        self.pool = nn.MaxPool2d(2, 2)
        self.conv2 = nn.Conv2d(32, 64, 5)
        self.fc1 = nn.Linear(64 * 4 * 4, 120)  
        self.fc2 = nn.Linear(120, 10)

    def forward(self, x):
        x = self.pool(F.relu(self.conv1(x)))
        x = self.pool(F.relu(self.conv2(x)))
        x = torch.flatten(x, 1) # flatten all dimensions except batch
        x = F.relu(self.fc1(x))
        x = self.fc2(x)
        return x

net = Net()

# Training loop        

loss_function = nn.CrossEntropyLoss()
optimizer = torch.optim.SGD(net.parameters(), lr=0.001)  

for i in range(epochs): 
    for images, labels in train_loader:
        optimizer.zero_grad()

        outputs = net(images) 
        loss = loss_function(outputs, labels)

        loss.backward()         
        optimizer.step()   

This shows how you can define convolutional layers, attach loss functions, train a network with batch gradient descent, and more.

From here you can start building and training models for AI applications!

Performance Tuning and Optimizing PyTorch

As you start developing PyTorch models for real-world applications, here are some tips for optimizing performance and scale:

Leveraging GPUs

Using CUDA-enabled NVIDIA GPUs can dramatically speed up training times. Ensure you install the GPU version and leverage .cuda() moves model to GPU memory.

Distributed Training

Scale model training across multiple servers with PyTorch distributed helpers and packages like PyTorch Lightning.

Optimized Data Loaders

Use high performance data loaders like DALI to feed data faster.

There are many more advanced methods for speeding up and scaling your PyTorch code. The key is continuously profiling and benchmarking as you develop.

And there you have it – now you know how to install the powerful PyTorch machine learning framework on both Windows and Linux operating systems. You learned:

  • PyTorch‘s capabilities for computer vision, NLP and other applications
  • The software and hardware prerequisites
  • Steps to install on Conda environments for Linux and Windows
  • How to validate the installation
  • Overview of how to build and train neural networks

You now have PyTorch ready to power all your latest AI project ideas! Let me know in the comments if you have any questions.