macOS Mojave で Chainer 環境構築

macOS Mojave で Chainer 環境構築

使用環境

$ sw_vers 
ProductName:    Mac OS X
ProductVersion: 10.14.5
BuildVersion:   18F132

Install Python

Download Python | Python.org

3.7.3 is the latest.

$ python3 --version
Python 3.7.3

Create virtual environment.

$ python3 -m venv .
$ source bin/activate

Install Chainer

Installation — Chainer 6.0.0 documentation

$ pip install -U setuptools pip
$ pip install chainer
$ pip freeze
cached-property==1.5.1
chainer==6.0.0
chainerrl==0.6.0
cycler==0.10.0
filelock==3.0.12
future==0.17.1
graphviz==0.11
gym==0.12.5
kiwisolver==1.1.0
matplotlib==3.1.0
numpy==1.16.4
Pillow==6.0.0
protobuf==3.7.1
pyglet==1.3.2
pyparsing==2.4.0
python-dateutil==2.8.0
scipy==1.3.0
six==1.12.0
typing==3.6.6
typing-extensions==3.7.2

Run the MNIST example

chainer/examples/mnist at v6.0.0 · chainer/chainer · GitHub

$ python train_mnist.py 
./lib/python3.7/site-packages/chainer/_environment_check.py:41: UserWarning: Accelerate has been detected as a NumPy backend library.
vecLib, which is a part of Accelerate, is known not to work correctly with Chainer.
We recommend using other BLAS libraries such as OpenBLAS.
For details of the issue, please see
https://docs.chainer.org/en/stable/tips.html#mnist-example-does-not-converge-in-cpu-mode-on-mac-os-x.

Please be aware that Mac OS X is not an officially supported OS.

  ''')  # NOQA
Device: @numpy
# unit: 1000
# Minibatch-size: 100
# epoch: 20

Downloading from http://yann.lecun.com/exdb/mnist/train-images-idx3-ubyte.gz...
Downloading from http://yann.lecun.com/exdb/mnist/train-labels-idx1-ubyte.gz...
Downloading from http://yann.lecun.com/exdb/mnist/t10k-images-idx3-ubyte.gz...
Downloading from http://yann.lecun.com/exdb/mnist/t10k-labels-idx1-ubyte.gz...
epoch       main/loss   validation/main/loss  main/accuracy  validation/main/accuracy  elapsed_time
1           0.190276    0.0994537             0.942633       0.9698                    23.8376       
2           0.0747232   0.0705411             0.976717       0.9778                    47.7668       
3           0.0487931   0.0757109             0.984167       0.9769                    72.6437       
4           0.0366443   0.0786135             0.988367       0.9765                    98.4305       
5           0.0273172   0.0694717             0.991467       0.981                     125.23        
6           0.0218385   0.108687              0.99275        0.9726                    153.306       
7           0.0198589   0.0779753             0.993367       0.9791                    188.598       
8           0.0186954   0.0732773             0.993783       0.9823                    218.851       
9           0.0176328   0.0889091             0.994433       0.9785                    249.011       
10          0.0142019   0.0829101             0.995233       0.9814                    279.932       
11          0.0121828   0.113355              0.996433       0.9776                    311.948       
12          0.0144403   0.0905099             0.995367       0.9808                    344.985       
13          0.0137652   0.0838266             0.996067       0.9833                    379.64        
14          0.00821494  0.117748              0.997517       0.9802                    413.659       
15          0.0132004   0.109114              0.996067       0.9817                    449.17        
16          0.0143113   0.112049              0.995533       0.9804                    485.779       
17          0.00892646  0.0957723             0.997017       0.9831                    523.138       
18          0.00662617  0.104108              0.998117       0.9817                    564.709       
19          0.00824879  0.12197               0.99755        0.9808                    604.888       
20          0.0157249   0.111977              0.995883       0.9829                    645.684       

There is a warning message.

./lib/python3.7/site-packages/chainer/_environment_check.py:41: UserWarning: Accelerate has been detected as a NumPy backend library.
vecLib, which is a part of Accelerate, is known not to work correctly with Chainer.
We recommend using other BLAS libraries such as OpenBLAS.
For details of the issue, please see
https://docs.chainer.org/en/stable/tips.html#mnist-example-does-not-converge-in-cpu-mode-on-mac-os-x.

Need to install NumPy separately. Let's try with Anaconda.

$ conda create --name condachainer pip
$ conda activate condachainer
$ pip install -U setuptools pip
$ pip install chainer
$ conda env export
name: condachainer
channels:
  - defaults
dependencies:
  - blas=1.0=mkl
  - ca-certificates=2019.5.15=0
  - certifi=2019.6.16=py37_0
  - cycler=0.10.0=py37_0
  - freetype=2.9.1=hb4e5f40_0
  - intel-openmp=2019.4=233
  - kiwisolver=1.1.0=py37h0a44026_0
  - libcxx=4.0.1=hcfea43d_1
  - libcxxabi=4.0.1=hcfea43d_1
  - libedit=3.1.20181209=hb402a30_0
  - libffi=3.2.1=h475c297_4
  - libgfortran=3.0.1=h93005f0_2
  - libpng=1.6.37=ha441bb4_0
  - matplotlib=3.1.0=py37h54f8f79_0
  - mkl=2019.4=233
  - mkl_fft=1.0.12=py37h5e564d8_0
  - mkl_random=1.0.2=py37h27c97d8_0
  - ncurses=6.1=h0a44026_1
  - numpy-base=1.16.4=py37h6575580_0
  - openssl=1.1.1c=h1de35cc_1
  - pip=19.1.1=py37_0
  - pyparsing=2.4.0=py_0
  - python=3.7.3=h359304d_0
  - python-dateutil=2.8.0=py37_0
  - pytz=2019.1=py_0
  - readline=7.0=h1de35cc_5
  - setuptools=41.0.1=py37_0
  - sqlite=3.28.0=ha441bb4_0
  - tk=8.6.8=ha441bb4_0
  - tornado=6.0.2=py37h1de35cc_0
  - wheel=0.33.4=py37_0
  - xz=5.2.4=h1de35cc_4
  - zlib=1.2.11=h1de35cc_3
  - pip:
    - chainer==6.1.0
    - filelock==3.0.12
    - numpy==1.16.4
    - protobuf==3.7.1
    - six==1.12.0
    - typing==3.6.6
    - typing-extensions==3.6.6
prefix: ~/anaconda3/envs/condachainer

Releases · chainer/chainer · GitHub

v6.1.0 @beam2d beam2d released this 20 hours ago

Oh...never mind.

$ python train_mnist.py 
Device: @numpy
# unit: 1000
# Minibatch-size: 100
# epoch: 20

epoch       main/loss   validation/main/loss  main/accuracy  validation/main/accuracy  elapsed_time
1           0.189819    0.0923269             0.942533       0.9721                    18.4105       
2           0.0730058   0.0963992             0.97735        0.9696                    38.9289       
3           0.0507709   0.0681645             0.983517       0.9796                    60.7336       
4           0.0357853   0.0733023             0.988467       0.9784                    83.1306       
5           0.0286872   0.082298              0.990917       0.979                     106.233       
6           0.0228451   0.0738484             0.99235        0.9806                    131.034       
7           0.0206049   0.0837659             0.993467       0.9796                    155.578       
8           0.0200223   0.0870863             0.993383       0.98                      180.522       
9           0.0133957   0.0855648             0.995667       0.9831                    206.545       
10          0.0175329   0.0806837             0.994467       0.982                     233.34        
11          0.0119728   0.0871093             0.99595        0.9829                    260.76        
12          0.0110821   0.0840505             0.996633       0.9829                    288.536       
13          0.0158595   0.0899242             0.995533       0.9823                    317.186       
14          0.00708507  0.104456              0.99755        0.982                     347.658       
15          0.0122286   0.0986352             0.996533       0.9806                    376.948       
16          0.0142947   0.107316              0.995783       0.9815                    406.64        
17          0.00880313  0.114419              0.9972         0.9815                    438.935       
18          0.00582421  0.0949353             0.998267       0.9826                    471.025       
19          0.00788875  0.123834              0.997783       0.9814                    504.499       
20          0.0126048   0.10169               0.996483       0.9816                    539.86        

1.2x - 1.3x faster than before.

その他

Install -> お試し実行で warning -> Anaconda 使って再度 install -> してる間に Chainer のバージョンが上がる、というオチまでついて環境構築完了。

Chainer は日本発のフレームワークということで色々期待が持てるが Mac はあまり積極的にはサポートされていないので最初っから Ubuntu 用意した方が良かったかもしれない。