Categories
Python PyTorch

How to install PyTorch 1.4.0 easily (conda & pip)

Here you will learn how to install PyTorch 1.4.0 through conda (Anaconda/Miniconda) and pip. PyTorch is a popular Deep Learning framework. A lot of open source code or papers still use 1.4 but PyTorch installation guides usually installs the latest version by default. Perhaps you already tried the latest version, but it does not work and you will have to install PyTorch 1.4.0. How can I fix it?

Prerequisite

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

4 Steps to Install PyTorch 1.4.0

  1. [Optional] Check if CUDA is installed

    It is strongly recommended that you have CUDA 10.1 installed. Because PyTorch 1.4.0 does not support CUDA 10.2 or CUDA 11.0. Click here to 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.4.0

    CUDA 10.2 is not officially supported, you have to install CUDA 10.1.
    CUDA 10.1: conda install pytorch==1.4.0 torchvision==0.5.0 cudatoolkit=10.1 -c pytorch
    CUDA 10.0: conda install pytorch==1.4.0 torchvision==0.5.0 cudatoolkit=10.0 -c pytorch
    CUDA 9.2: conda install pytorch==1.4.0 torchvision==0.5.0 cudatoolkit=9.2 -c pytorch
    CPU Only (your PyTorch code will run slower):
    conda install pytorch==1.4.0 torchvision==0.5.0 cpuonly -c pytorch

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

    As CUDA does not support macOS, run
    conda install pytorch==1.4.0 torchvision==0.5.0 -c pytorch

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

    CUDA 10.2 is not supported, you have to install CUDA 10.1.
    CUDA 10.1: pip3 install torch==1.4.0 torchvision==0.5.0 -f https://download.pytorch.org/whl/cu101/torch_stable.html
    CUDA 10.0: pip3 install torch==1.4.0 torchvision==0.5.0 -f https://download.pytorch.org/whl/cu100/torch_stable.html
    CUDA 9.2: pip3 install torch==1.4.0+cu92 torchvision==0.5.0+cu92 -f https://download.pytorch.org/whl/cu92/torch_stable.html
    CPU only (GPU is much better…): pip install torch==1.4.0+cpu torchvision==0.5.0+cpu -f https://download.pytorch.org/whl/torch_stable.html

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

    pip install torch==1.4.0 torchvision==0.5.0

  6. Verify PyTorch 1.4.0 is installed

    RunΒ Python/Python3Β with

    import torch
    print(torch.__version__)

  7. Verify PyTorch 1.4.0 is using CUDA

    import torch
    torch.cuda.is_available()

Check if PyTorch 1.4.0 is installed

We’ll test the installation by running a sample PyTorch script to ensure that PyTorch 1.4.0 has been installed properly. Here we will create a tensor that is randomly initialised.

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

The output are shown below. Yours will be similar.

tensor([[0.3380, 0.3845, 0.3217],
[0.8337, 0.9050, 0.2650],
[0.2979, 0.7141, 0.9069],
[0.1449, 0.1132, 0.1375],
[0.4675, 0.3947, 0.1426]])

Check if CUDA is available to PyTorch 1.4.0

To verify if PyTorch 1.4.0 is available and accessible for your GPU driver and CUDA, run the following Python code to decide whether or not the CUDA driver is enabled:

import torch
torch.cuda.is_available()

In case for people who are interested, the following 2 sections introduces PyTorch v1 and CUDA.

What is PyTorch?

PyTorch is an open-source Deep Learning framework that is scalable and versatile for testing, reliable and supportive for deployment. It enables quick, modular experimentation via an autograding component designed for fast and python-like execution. With the introduction of PyTorch 1.0, the framework now has graph-based execution, a hybrid front-end that allows for smooth mode switching, collaborative testing, and effective and secure 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.

Like

By VarHowto Editor

Welcome to VarHowto!

Leave a Reply

Your email address will not be published. Required fields are marked *