打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Start Locally | PyTorch

Start Locally

Select your preferences and run the install command. Stable represents the most currently tested and supported version of PyTorch. This should be suitable for many users. Preview is available if you want the latest, not fully tested and supported, builds that are generated nightly. Please ensure that you have met the prerequisites below (e.g., numpy), depending on your package manager. Anaconda is our recommended package manager since it installs all dependencies. You can also install previous versions of PyTorch. Note that LibTorch is only available for C++.

NOTE: Latest PyTorch requires Python 3.8 or later. For more details, see Python section below.

PyTorch Build
Your OS
Package
Language
Compute Platform
Run this Command:
PyTorch Build
Stable (2.2.1)
Preview (Nightly)
Your OS
Linux
Mac
Windows
Package
Conda
Pip
LibTorch
Source
Language
Python
C++ / Java
Compute Platform
CUDA 11.8
CUDA 12.1
ROCm 5.7
CPU
Run this Command:
pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cpu


Installing on macOS

PyTorch can be installed and used on macOS. Depending on your system and GPU capabilities, your experience with PyTorch on a Mac may vary in terms of processing time.

Prerequisites

macOS Version

PyTorch is supported on macOS 10.15 (Catalina) or above.

Python

It is recommended that you use Python 3.8 - 3.11. You can install Python either through the Anaconda package manager (see below), Homebrew, or the Python website.

In one of the upcoming PyTorch releases, support for Python 3.8 will be deprecated.

Package Manager

To install the PyTorch binaries, you will need to use one of two supported package managers: Anaconda or pip. Anaconda is the recommended package manager as it will provide you all of the PyTorch dependencies in one, sandboxed install, including Python.

Anaconda

To install Anaconda, you can download graphical installer or use the command-line installer. If you use the command-line installer, you can right-click on the installer link, select Copy Link Address, or use the following commands on Intel Mac:

# The version of Anaconda may be different depending on when you are installing`
curl -O https://repo.anaconda.com/miniconda/Miniconda3-latest-MacOSX-x86_64.sh
sh Miniconda3-latest-MacOSX-x86_64.sh
# and follow the prompts. The defaults are generally good.`

or following commands on M1 Mac:

# The version of Anaconda may be different depending on when you are installing`
curl -O https://repo.anaconda.com/miniconda/Miniconda3-latest-MacOSX-arm64.sh
sh Miniconda3-latest-MacOSX-arm64.sh
# and follow the prompts. The defaults are generally good.`

pip

Python 3

If you installed Python via Homebrew or the Python website, pip was installed with it. If you installed Python 3.x, then you will be using the command pip3.

Tip: If you want to use just the command pip, instead of pip3, you can symlink pip to the pip3 binary.

Installation

Anaconda

To install PyTorch via Anaconda, use the following conda command:

conda install pytorch torchvision -c pytorch

pip

To install PyTorch via pip, use one of the following two commands, depending on your Python version:

# Python 3.x
pip3 install torch torchvision

Verification

To ensure that PyTorch was installed correctly, we can verify the installation by running sample PyTorch code. Here we will construct a randomly initialized tensor.

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

The output should be something similar to:

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]])

Building from source

For the majority of PyTorch users, installing from a pre-built binary via a package manager will provide the best experience. However, there are times when you may want to install the bleeding edge PyTorch code, whether for testing or actual development on the PyTorch core. To install the latest PyTorch code, you will need to build PyTorch from source.

Prerequisites

  1. [Optional] Install Anaconda
  2. Follow the steps described here: https://github.com/pytorch/pytorch#from-source

You can verify the installation as described above.

Installing on Linux

PyTorch can be installed and used on various Linux distributions. Depending on your system and compute requirements, your experience with PyTorch on Linux may vary in terms of processing time. It is recommended, but not required, that your Linux system has an NVIDIA or AMD GPU in order to harness the full power of PyTorch’s CUDA support or ROCm support.

Prerequisites

Supported Linux Distributions

PyTorch is supported on Linux distributions that use glibc >= v2.17, which include the following:

The install instructions here will generally apply to all supported Linux distributions. An example difference is that your distribution may support yum instead of apt. The specific examples shown were run on an Ubuntu 18.04 machine.

Python

Python 3.8-3.11 is generally installed by default on any of our supported Linux distributions, which meets our recommendation.

Tip: By default, you will have to use the command python3 to run Python. If you want to use just the command python, instead of python3, you can symlink python to the python3 binary.

However, if you want to install another version, there are multiple ways:

If you decide to use APT, you can run the following command to install it:

sudo apt install python

If you use Anaconda to install PyTorch, it will install a sandboxed version of Python that will be used for running PyTorch applications.

Package Manager

To install the PyTorch binaries, you will need to use one of two supported package managers: Anaconda or pip. Anaconda is the recommended package manager as it will provide you all of the PyTorch dependencies in one, sandboxed install, including Python.

Anaconda

To install Anaconda, you will use the command-line installer. Right-click on the 64-bit installer link, select Copy Link Location, and then use the following commands:

# The version of Anaconda may be different depending on when you are installing`
curl -O https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh
sh Miniconda3-latest-Linux-x86_64.sh
# and follow the prompts. The defaults are generally good.`

You may have to open a new terminal or re-source your ~/.bashrc to get access to the conda command.

pip

Python 3

While Python 3.x is installed by default on Linux, pip is not installed by default.

sudo apt install python3-pip

Tip: If you want to use just the command pip, instead of pip3, you can symlink pip to the pip3 binary.

Installation

Anaconda

No CUDA/ROCm

To install PyTorch via Anaconda, and do not have a CUDA-capable or ROCm-capable system or do not require CUDA/ROCm (i.e. GPU support), in the above selector, choose OS: Linux, Package: Conda, Language: Python and Compute Platform: CPU. Then, run the command that is presented to you.

With CUDA

To install PyTorch via Anaconda, and you do have a CUDA-capable system, in the above selector, choose OS: Linux, Package: Conda and the CUDA version suited to your machine. Often, the latest CUDA version is better. Then, run the command that is presented to you.

With ROCm

PyTorch via Anaconda is not supported on ROCm currently. Please use pip instead.

pip

No CUDA

To install PyTorch via pip, and do not have a CUDA-capable or ROCm-capable system or do not require CUDA/ROCm (i.e. GPU support), in the above selector, choose OS: Linux, Package: Pip, Language: Python and Compute Platform: CPU. Then, run the command that is presented to you.

With CUDA

To install PyTorch via pip, and do have a CUDA-capable system, in the above selector, choose OS: Linux, Package: Pip, Language: Python and the CUDA version suited to your machine. Often, the latest CUDA version is better. Then, run the command that is presented to you.

With ROCm

To install PyTorch via pip, and do have a ROCm-capable system, in the above selector, choose OS: Linux, Package: Pip, Language: Python and the ROCm version supported. Then, run the command that is presented to you.

Verification

To ensure that PyTorch was installed correctly, we can verify the installation by running sample PyTorch code. Here we will construct a randomly initialized tensor.

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

The output should be something similar to:

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]])

Additionally, to check if your GPU driver and CUDA/ROCm is enabled and accessible by PyTorch, run the following commands to return whether or not the GPU driver is enabled (the ROCm build of PyTorch uses the same semantics at the python API level (https://github.com/pytorch/pytorch/blob/master/docs/source/notes/hip.rst#hip-interfaces-reuse-the-cuda-interfaces), so the below commands should also work for ROCm):

import torch
torch.cuda.is_available()

Building from source

For the majority of PyTorch users, installing from a pre-built binary via a package manager will provide the best experience. However, there are times when you may want to install the bleeding edge PyTorch code, whether for testing or actual development on the PyTorch core. To install the latest PyTorch code, you will need to build PyTorch from source.

Prerequisites

  1. Install Anaconda or Pip
  2. If you need to build PyTorch with GPU support a. for NVIDIA GPUs, install CUDA, if your machine has a CUDA-enabled GPU. b. for AMD GPUs, install ROCm, if your machine has a ROCm-enabled GPU
  3. Follow the steps described here: https://github.com/pytorch/pytorch#from-source

You can verify the installation as described above.

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
2019 Mac13.6安装cuda+cudnn+pytorch
Ubuntu系统Anaconda安装Pytorch,教你如何优雅的安装环境(1-5)
Anaconda常用命令
linux下conda的虚拟环境用pip、conda、apt-get安装三方库的区别
【Python】Windows系统下Spyder安装python第三方包的两种方法
【技术观点】深度学习框架Theano利用GPU加速
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服