TensorFlowインストール(mac)(2020年4月)

macにTensorFlowをインストールした際の記録です。

参考元

今回の作業は以下をベースに進めています。

https://www.tensorflow.org/install/pip?lang=python3

macへインストール

事前の状態確認

コマンド

事前の環境確認のコマンドが以下です。「python3」「pip3」「virtualenv」のそれぞれ確認しています。

$ python3 --version
$ pip3 --version
$ virtualenv --version

実行結果

$ python3 --version
Python 3.7.6
$
$ pip3 --version
pip 19.3.1 from /usr/local/lib/python3.7/site-packages/pip (python 3.7)
$
$ virtualenv --version
-bash: virtualenv: command not found
$

私の環境の場合、「python3」と「pip3」はインストール済み、「virtualenv」は未インストールという状況でした。

python3、pip3インストール

コマンド

python3、pip3インストールのコマンドとして参考元に記載されているのが以下です。

$ /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
$ export PATH="/usr/local/bin:/usr/local/sbin:$PATH"
$ brew update
$ brew install python

Homebrewを使ったインストールの方法になっています。私の環境はインストール済みなので省略します。

virtualenvインストール

コマンド

virtualenvのインストールコマンドが以下です。

$ sudo pip3 install -U virtualenv

実行結果

コマンドを実行した結果が以下です。

$ sudo pip3 install -U virtualenv
Password:
Collecting virtualenv
  Downloading https://files.pythonhosted.org/packages/23/1a/d55f5fb904564365c3d364fd41701a74ad497379f04fd548cccc3f227d98/virtualenv-20.0.18-py2.py3-none-any.whl (4.6MB)
     |████████████████████████████████| 4.6MB 411kB/s
Requirement already satisfied, skipping upgrade: appdirs<2,>=1.4.3 in /usr/local/lib/python3.7/site-packages (from virtualenv) (1.4.3)
Requirement already satisfied, skipping upgrade: six<2,>=1.9.0 in /usr/local/lib/python3.7/site-packages (from virtualenv) (1.14.0)
Requirement already satisfied, skipping upgrade: distlib<1,>=0.3.0 in /usr/local/lib/python3.7/site-packages (from virtualenv) (0.3.0)
Requirement already satisfied, skipping upgrade: filelock<4,>=3.0.0 in /usr/local/lib/python3.7/site-packages (from virtualenv) (3.0.12)
Requirement already satisfied, skipping upgrade: importlib-metadata<2,>=0.12; python_version < "3.8" in /usr/local/lib/python3.7/site-packages (from virtualenv) (1.6.0)
Requirement already satisfied, skipping upgrade: zipp>=0.5 in /usr/local/lib/python3.7/site-packages (from importlib-metadata<2,>=0.12; python_version < "3.8"->virtualenv) (3.1.0)
Installing collected packages: virtualenv
Successfully installed virtualenv-20.0.18
$

無事成功しました。

仮想環境の保管ディレクトリ(./venv)作成

コマンド

以下のコマンドでディレクトリを作成します。

$ virtualenv --system-site-packages -p python3 ./venv

実行結果

コマンドを実行すると以下の様になりました。

$ virtualenv --system-site-packages -p python3 ./venv
created virtual environment CPython3.7.6.final.0-64 in 585ms
  creator CPython3Posix(dest=/Users/masaki/venv, clear=False, global=True)
  seeder FromAppData(download=False, pip=latest, setuptools=latest, wheel=latest, via=copy, app_data_dir=/Users/masaki/Library/Application Support/virtualenv/seed-app-data/v1.0.1)
  activators BashActivator,CShellActivator,FishActivator,PowerShellActivator,PythonActivator,XonshActivator
$

仮想環境の有効化

コマンド

以下のコマンドで仮想環境を有効化します。

$ source ./venv/bin/activate

実行結果

コマンドの実行結果は以下の様になります。

$ source ./venv/bin/activate
(venv) $

実行後、プロンプトの先頭に「(venv)」が付与される様になりました。

pipのアップグレード

コマンド

以下のコマンドでpipのアップグレードをします。

$ sudo pip install --upgrade pip

実行結果

実行結果は以下の様になりました。

(venv) $ sudo pip install --upgrade pip
Requirement already up-to-date: pip in ./venv/lib/python3.7/site-packages (20.0.2)
(venv) $

TensorFlowインストール

コマンド

以下のコマンドでTensorFlowをインストールします。

$ sudo pip install --upgrade tensorflow_cpu

参照元だと「tensorflow_cpu」ではなく「tensorflow」ですが、私の環境の場合「tensorflow」だとこの後のpythonコマンドで以下の様なエラーが出たので、cpuにしています。

2020-04-26 06:20:39.014175: E tensorflow/stream_executor/cuda/cuda_driver.cc:351] failed call to cuInit: UNKNOWN ERROR (303)

人によっては「tensorflow」で大丈夫だと思います。

実行結果

コマンドを実行すると以下の様になります。

(venv) $ sudo pip install --upgrade tensorflow_cpu
Collecting tensorflow_cpu

…(略)…

Successfully built wrapt termcolor gast absl-py
Installing collected packages: numpy, astor, wrapt, keras-preprocessing, absl-py, chardet, urllib3, idna, certifi, requests, pyasn1, pyasn1-modules, cachetools, rsa, google-auth, oauthlib, requests-oauthlib, google-auth-oauthlib, grpcio, markdown, werkzeug, protobuf, tensorboard, google-pasta, termcolor, tensorflow-estimator, scipy, gast, h5py, keras-applications, opt-einsum, tensorflow-cpu
Successfully installed absl-py-0.9.0 astor-0.8.1 cachetools-4.1.0 certifi-2020.4.5.1 chardet-3.0.4 gast-0.2.2 google-auth-1.14.1 google-auth-oauthlib-0.4.1 google-pasta-0.2.0 grpcio-1.28.1 h5py-2.10.0 idna-2.9 keras-applications-1.0.8 keras-preprocessing-1.1.0 markdown-3.2.1 numpy-1.18.3 oauthlib-3.1.0 opt-einsum-3.2.1 protobuf-3.11.3 pyasn1-0.4.8 pyasn1-modules-0.2.8 requests-2.23.0 requests-oauthlib-1.3.0 rsa-4.0 scipy-1.4.1 tensorboard-2.1.1 tensorflow-cpu-2.1.0 tensorflow-estimator-2.1.0 termcolor-1.1.0 urllib3-1.25.9 werkzeug-1.0.1 wrapt-1.12.1
(venv) $

インストール検証

コマンド

インストール検証のコマンドが以下です。

$ python -c "import tensorflow as tf;print(tf.reduce_sum(tf.random.normal([1000, 1000])))"

実行結果

コマンドの実行結果が以下となります。

(venv) $ python -c "import tensorflow as tf;print(tf.reduce_sum(tf.random.normal([1000, 1000])))"
2020-04-26 23:13:24.356799: I tensorflow/core/platform/cpu_feature_guard.cc:142] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
2020-04-26 23:13:24.387930: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x7fef787cfc80 initialized for platform Host (this does not guarantee that XLA will be used). Devices:
2020-04-26 23:13:24.387955: I tensorflow/compiler/xla/service/service.cc:176]   StreamExecutor device (0): Host, Default Version
tf.Tensor(185.51643, shape=(), dtype=float32)
(venv) $

警告が出ているのでわかりづらいですが、「tf.Tensor(185.51643, shape=(), dtype=float32)」の部分がコマンドの実行結果です。

コマンド(修正)

先ほどのコマンドでも問題ないですが、余計な出力があり分かりづらいので少し手を加えたのが以下です。

$ python -c "import os;os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2';import tensorflow as tf;print(tf.reduce_sum(tf.random.normal([1000, 1000])))"

実行結果

修正版のコマンドの実行結果が以下です。

(venv) $ python -c "import os;os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2';import tensorflow as tf;print(tf.reduce_sum(tf.random.normal([1000, 1000])))"
tf.Tensor(-122.783325, shape=(), dtype=float32)
(venv) $

コメント

タイトルとURLをコピーしました