Categories
Python PyTorch

How to install PyTorch 1.6.0 (conda & pip)

Here you will learn how to install PyTorch 1.6.0 through conda (Anaconda/Miniconda) and pip. PyTorch is a common Platform for Deep Learning and 1.6.0 is its latest version.

Prerequisite

This tutorial assumes you can run python and a package manager like pip or conda. Miniconda and Anaconda are both good, but miniconda is lightweight. We wrote an article about how to install Miniconda.

4 Steps to Install PyTorch 1.6.0

  1. [Optional] Check if CUDA is installed

    It is highly recommended that you have CUDA installed. Note that PyTorch 1.6.0 does not support CUDA 11.0. If you haven't installed CUDA, please install CUDA 10.2 or install CUDA 10.1.

    Once/If you have it installed, you can check its version here.

  2. [For conda on Ubuntu/Linux and Windows 10]
    Run conda install and specify PyTorch version 1.6.0

    Starting from here, we will install PyTorch 1.6.0.

    CUDA 10.2: conda install pytorch==1.6.0 torchvision==0.7.0 cudatoolkit=10.2 -c pytorch
    CUDA 10.1: conda install pytorch==1.6.0 torchvision==0.7.0 cudatoolkit=10.1 -c pytorch
    CUDA 9.2: conda install pytorch==1.6.0 torchvision==0.7.0 cudatoolkit=9.2 -c pytorch
    CPU Only (your PyTorch code will run slower):
    conda install pytorch==1.6.0 torchvision==0.7.0 cpuonly -c pytorch

  3. [For conda on macOS]
    Run conda install and specify PyTorch version 1.6.0

    There is only one command to install PyTorch 1.6.0 on macOS:
    conda install pytorch==1.6.0 torchvision==0.7.0 -c pytorch

  4. [For pip] Run pip3 install by specifying version with -f

    CUDA 10.2: pip install torch==1.6.0 torchvision==0.7.0
    CUDA 10.1: pip3 install torch==1.6.0 torchvision==0.7.0 -f https://download.pytorch.org/whl/cu101/torch_stable.html
    CUDA 10.0 is not officially supported by PyTorch 1.6.0, you have to install CUDA 10.2, CUDA 10.1, or CUDA 9.2.
    CUDA 9.2: pip3 install torch==1.6.0 torchvision==0.7.0 -f https://download.pytorch.org/whl/cu92/torch_stable.html
    PyTorch 1.6.0 also doesn't support CUDA 9.1 or 9.0.
    CPU only (GPU is much better…): pip install torch==1.6.0+cpu torchvision==0.7.0+cpu -f https://download.pytorch.org/whl/torch_stable.html

  5. [For pip on macOS] Run pip3 install by specifying version with -f

    pip install torch==1.6.0 torchvision==0.7.0

  6. Verify PyTorch 1.6.0 is installed

    RunΒ Python or Python3Β with

    import torch
    print(torch.__version__)


    This should output 1.6.0.

  7. Verify PyTorch 1.6.0 is using CUDA

    import torch
    torch.cuda.is_available()

Verify if PyTorch 1.6.0 is installed

To ensure the correct installation of PyTorch 1.6.0, we will verify the installation by running a sample PyTorch script. Here, we will construct a tensor which is initialized at random.

import torch
print(torch.rand(3, 5))

The output are shown below. Yours will be similar.

tensor([[0.3190, 0.3945, 0.3317],
[0.8437, 0.9150, 0.2660],
[0.2079, 0.7241, 0.9099],
[0.1549, 0.1232, 0.1365],
[0.4975, 0.3047, 0.1456]])

Check if CUDA is available to PyTorch 1.6.0

To confirm that PyTorch 1.6.0 is available for your GPU and CUDA driver, run the following Python code to decide if the CUDA driver is enabled:

import torch
torch.cuda.is_available()

In case of people interested, PyTorch v1 and CUDA are introduced in the following 2 sections.

What is PyTorch?

PyTorch is an open-source Deep Learning framework for testing, reliable and supporting deployment that is scalable and flexible. This enables quick, flexible experimentation through an autograding feature optimized for quick and python-like execution. The framework now has graph-based execution with the release of PyTorch 1.0, a hybrid front-end that allows for smooth mode switching, collaborative testing, and efficient and stable deployment on mobile platforms.

PyTorch has 4 key features according to its official homepage.

  1. PyTorch is production-ready: TorchScript smoothly toggles between eager and graph modes. TorchServe speeds up the production process.
  2. PyTorch support distributed training: The torch.collaborative interface allows for efficient distributed training and performance optimization in research and development.
  3. PyTorch has a robust ecosystem: It has an expansive ecosystem of tools and libraries to support applications such as computer vision and NLP.
  4. PyTorch has native cloud support: It is well recognized for its zero-friction development and fast scaling on key cloud providers.

+6

By VarHowto Editor

Welcome to VarHowto!