みながすなるディープラーニングなるものをWebエンジニアのわたしもしてみんとす。
ChainerとTensorflowのどっちを使うか迷ったけど、Googleさんを信じてTensorflowをインストールすることにした。
そしてさすがGoogleさん、ちゃんとmacOSでインストールするためのドキュメントがある。
Installing TensorFlow on Mac OS X:
https://www.tensorflow.org/install/install_mac
なるほどpipでインストールできるんか!さすがGoogleさんやで!
pip install tensorflow
として、サンプルを実行してみた。
>> Downloading cifar-10-binary.tar.gz 100.0%
Successfully downloaded cifar-10-binary.tar.gz 170052171 bytes.
Filling queue with 20000 CIFAR images before starting to train. This will take a few minutes.
W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.1 instructions, but these are available on your machine and could speed up CPU computations.
W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.2 instructions, but these are available on your machine and could speed up CPU computations.
W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX instructions, but these are available on your machine and could speed up CPU computations.
W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX2 instructions, but these are available on your machine and could speed up CPU computations.
W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use FMA instructions, but these are available on your machine and could speed up CPU computations.
2017-04-06 17:33:09.604148: step 0, loss = 4.68 (299.9 examples/sec; 0.427 sec/batch)
2017-04-06 17:33:15.304220: step 10, loss = 4.62 (224.6 examples/sec; 0.570 sec/batch)
2017-04-06 17:33:21.174254: step 20, loss = 4.51 (218.1 examples/sec; 0.587 sec/batch)
2017-04-06 17:33:27.141802: step 30, loss = 4.50 (214.5 examples/sec; 0.597 sec/batch)
2017-04-06 17:33:33.371282: step 40, loss = 4.32 (205.5 examples/sec; 0.623 sec/batch)
2017-04-06 17:33:39.447115: step 50, loss = 4.39 (210.7 examples/sec; 0.608 sec/batch)
2017-04-06 17:33:45.514131: step 60, loss = 4.34 (211.0 examples/sec; 0.607 sec/batch)
2017-04-06 17:33:51.856185: step 70, loss = 4.22 (201.8 examples/sec; 0.634 sec/batch)
うおおお、なんだこの警告。
W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.1 instructions, but these are available on your machine and could speed up CPU computations.
どうやらライブラリが実行している端末のCPUが対応しているSIMD命令を使うように最適化されていない、ということらしい(上の場合、SSE4.1を使うようにコンパイルされていない、という意味)。
なるほど、インストールする端末のCPUがわからないから、SIMD命令を使わない、最適化しない状態でビルドされているということか。
こうなると自前でビルドするしかない。
ということでどうせなら、と例によってHomebrewのフォーミュラを作成しました。
$ brew install tensorflow
とすると最適化されていないbottle(コンパイル済みのバイナリー)がインストールされてしまうので、
$ brew install --build-from-source tensorflow
としてソースからビルドしてインストールしましょう。
ちょっと時間はかかりますが、使っている端末向けに最適化されたTensorflowがインストールされます。
もう一回サンプルを実行してみました。
Filling queue with 20000 CIFAR images before starting to train. This will take a few minutes.
2017-05-12 18:03:10.203364: I tensorflow/compiler/xla/service/platform_util.cc:58] platform Host present with 4 visible devices
2017-05-12 18:03:10.203711: I tensorflow/compiler/xla/service/service.cc:183] XLA service 0x7f8af1e73ae0 executing computations on platform Host. Devices:
2017-05-12 18:03:10.203726: I tensorflow/compiler/xla/service/service.cc:191] StreamExecutor device (0): <undefined>, <undefined>
2017-05-12 18:03:13.687847: step 0, loss = 4.68 (348.0 examples/sec; 0.368 sec/batch)
2017-05-12 18:03:17.197853: step 10, loss = 4.65 (364.7 examples/sec; 0.351 sec/batch)
2017-05-12 18:03:20.611429: step 20, loss = 4.48 (375.0 examples/sec; 0.341 sec/batch)
2017-05-12 18:03:24.085883: step 30, loss = 4.45 (368.4 examples/sec; 0.347 sec/batch)
2017-05-12 18:03:28.001790: step 40, loss = 4.34 (326.9 examples/sec; 0.392 sec/batch)
2017-05-12 18:03:33.552331: step 50, loss = 4.42 (230.6 examples/sec; 0.555 sec/batch)
2017-05-12 18:03:37.479313: step 60, loss = 4.25 (326.0 examples/sec; 0.393 sec/batch)
2017-05-12 18:03:40.992778: step 70, loss = 4.22 (364.3 examples/sec; 0.351 sec/batch)
警告が出なくなりました!さらに速度も1.6倍程度に向上しています!(224.6 → 364.7 examples/sec)
GPU対応
とはいえこれはCPUレベルでの最適化。やはりバリバリGPGPUで回したいですよね。
が。
TensorFlow 1.1.0 will be the last time we release a binary with Mac GPU support. Going forward, we will stop testing on Mac GPU systems. We continue to welcome patches that maintain Mac GPU support, and we will try to keep the Mac GPU build working.
どうやらmacOSでGPU対応がちゃんと使える1.1.0が最後になるということのようです。
Macではあくまで開発・テストのみを行って、実際に学習や推論を回すのはGPUが載ったWindows/Linuxマシンにする方がいいのかもしれません…。