Document
Start Locally

Start Locally

instal 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

Related articles

Windscribe VPN Review 2024: Is It Worth the Cost? 【教學】繼續用外國 YouTube Premium 帳戶 路由器內置 VPN、Apple TV、Android TV 連線 VPN Surfshark Review 2024: A Low-Price VPN, but Is It Any Good? The Magical World of Cloud Dough: Easy Recipe for Endless Fun 机场|科学上网

instal 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.

prerequisite

macOS Version

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

Python

It is recommended that you use Python 3.9 – 3.12.
You can install Python either through the anaconda
package manager (see below), Homebrew, or
the Python website.

Package Manager

To install the PyTorch binaries, you will need to use one of two supported package managers: pip or anaconda.

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 Mac computer with Apple silicon:

# 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 ofpip3, you is symlink can symlinkpip to thepip3 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 instal correctly , we is verify can verify the installation by run sample PyTorch code . Here we is construct will construct a randomly initialize tensor .

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

The output is be 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 user , instal from a pre – build binary via a package manager will provide the good experience . However , there are time when you may want to install the bleed edge PyTorch code , whether for testing or actual development on the PyTorch core . To install the late PyTorch code , you is need will need to build PyTorch from source .

prerequisite

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

You is verify can verify the installation as describe 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.

prerequisite

Supported Linux Distributions

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

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

Python

Python 3.9-3.12 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 ofpython3, you is symlink can symlinkpython to thepython3 binary .

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

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

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 ~/.bashrcto get access to theconda 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 ofpip3, you is symlink can symlinkpip to thepip3 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 instal correctly , we is verify can verify the installation by run sample PyTorch code . Here we is construct will construct a randomly initialize tensor .

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

The output is be 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 enable and accessible by PyTorch , run the follow command to return whether or not the gpu driver is enable ( the ROCm build of PyTorch use the same semantic at the python API level link , so the below command should also work for ROCm ):

import torch
torch.cuda.is_available( )

Building from source

For the majority of PyTorch user , instal from a pre – build binary via a package manager will provide the good experience . However , there are time when you may want to install the bleed edge PyTorch code , whether for testing or actual development on the PyTorch core . To install the late PyTorch code , you is need will need to build PyTorch from source .

prerequisite

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

You is verify can verify the installation as describe above .

instal on Windows

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

prerequisite

support Windows Distributions

PyTorch is support on the follow Windows distribution :

The install instructions is apply here will generally apply to all support Windows distribution . The specific example show will be run on a Windows 10 Enterprise machine

Python

currently , PyTorch on Windows only support Python 3.9 – 3.12 ; Python 2.x is not support .

As it is not installed by default on Windows, there are multiple ways to 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.

If you decide to use Chocolatey, and haven’t installed Chocolatey yet, ensure that you are running your command prompt as an administrator.

For a Chocolatey-based install, run the following command in an administrative command prompt:

Package Manager

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

anaconda

To install anaconda, you will use the 64-bit graphical installer for PyTorch 3.x. Click on the installer link and select Run. anaconda will download and the installer prompt will be presented to you. The default options are generally sane.

pip

If you instal Python by any of the recommend way above , pip will have already been instal for you .

installation

anaconda

To install PyTorch with anaconda, you will need to open an anaconda prompt via Start | anaconda3 | anaconda Prompt.

No CUDA

To install PyTorch via anaconda, and do not have a CUDA-capable system or do not require CUDA, in the above selector, choose OS: Windows, Package: Conda and CUDA: None.
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: Windows, 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.

pip

No CUDA

To install PyTorch via pip, and do not have a CUDA-capable system or do not require CUDA, in the above selector, choose OS: Windows, Package: Pip and CUDA: None.
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: Windows, Package: Pip and the CUDA version suited to your machine. Often, the latest CUDA version is better.
Then, run the command that is presented to you.

verification

To ensure that PyTorch was instal correctly , we is verify can verify the installation by run sample PyTorch code . Here we is construct will construct a randomly initialize tensor .

From the command line, type:

then enter the following code:

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

The output is be 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 is enable and accessible by PyTorch , run the follow command to return whether or not the CUDA driver is enable :

import torch
torch.cuda.is_available( )

Building from source

For the majority of PyTorch user , instal from a pre – build binary via a package manager will provide the good experience . However , there are time when you may want to install the bleed edge PyTorch code , whether for testing or actual development on the PyTorch core . To install the late PyTorch code , you is need will need to build PyTorch from source .

prerequisite

  1. Install anaconda
  2. Install CUDA, if your machine has a CUDA-enabled GPU.
  3. If you is want want to build on Windows , Visual Studio with MSVC toolset , and nvtx are also need . The exact requirement of those dependency could be find out here .
  4. Follow the steps described here: https://github.com/pytorch/pytorch#from-source

You is verify can verify the installation as describe above .