DEV Community

darkside haguremetal
darkside haguremetal

Posted on

Install PyTorch on Mac by pip


  1. Install PyTorch on Mac OS X 10.14.4
  2. Check whether it works.


How to install PyTorch

  • PyTorch official says you can install PyTorch by conda if you already have Anaconda.
  • But conda may just fetch package which already build, while pip may build package after getting a wheel compatible with installation environment, so performance may differ.
    • I'm not so sure. Log may make it clear.
  • Then, use pip to install PyTorch
$ pip3 install torch torchvision
Collecting torch
  Downloading (88.1MB)
     |████████████████████████████████| 88.1MB 604kB/s
Collecting torchvision
  Downloading (231kB)
     |████████████████████████████████| 235kB 894kB/s
Requirement already satisfied: numpy in /Users/foo/.pyenv/versions/anaconda3-5.3.1/lib/python3.7/site-packages (from torch) (1.15.1)
Requirement already satisfied: pillow>=4.1.1 in /Users/foo/.pyenv/versions/anaconda3-5.3.1/lib/python3.7/site-packages (from torchvision) (5.2.0)
Requirement already satisfied: six in /Users/foo/.pyenv/versions/anaconda3-5.3.1/lib/python3.7/site-packages (from torchvision) (1.11.0)
Installing collected packages: torch, torchvision
Successfully installed torch-1.1.0.post2 torchvision-0.3.0
  • Check if PyTorch works well.
from __future__ import print_function
import torch
x = torch.rand(5, 3)
  • Result
tensor([[0.7747, 0.9627, 0.1806],
        [0.9219, 0.1319, 0.9026],
        [0.6719, 0.6495, 0.5341],
        [0.6629, 0.4546, 0.7309],
        [0.3577, 0.8918, 0.8544]])
  • Check if GPU CUDA is available
import torch
  • If CUDA is not available, result shows below

Top comments (0)