DEV Community

darkside haguremetal
darkside haguremetal

Posted on

Install PyTorch on Mac by pip

Overview

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

Environment


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 https://files.pythonhosted.org/packages/95/80/0851d6088bb054be3ddf47bfde1aedb4e1cbd170cf8e1c60cad321b97162/torch-1.1.0.post2-cp37-none-macosx_10_7_x86_64.whl (88.1MB)
     |████████████████████████████████| 88.1MB 604kB/s
Collecting torchvision
  Downloading https://files.pythonhosted.org/packages/af/7c/247d46a1f76dee688636d4d5394e440bb32c4e251ea8afe4442c91296830/torchvision-0.3.0-cp37-cp37m-macosx_10_7_x86_64.whl (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)
print(x)
  • 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
print(torch.cuda.is_available())
  • If CUDA is not available, result shows below
False

Top comments (0)