macOSでTensorflow

2017-05-18(木)

macOSでTensorflowを頑張ってみた

ここの通りやってもCPUによってはまだ改善点があるようで

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 availab

とでる。bazelでビルドする時にAVX2とFMAも加えてみる。

$ conda create --name tf35 python=3.5 scipy numpy jupyter ipython scikit-learn scikit-image matplotlib pandas h5py
$ activate tf35
$ brew install bazel swig
$ git clone https://github.com/tensorflow/tensorflow.git
$ cd tensorflow
$ git checkout v1.0.1
$ ./configure
$ bazel build -c opt --copt=-mavx --copt=-msse4.1 --copt=-msse4.2 --copt=-mavx2 --copt=-mfma //tensorflow/tools/pip_package:build_pip_package
$ bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkg
$ pip uninstall tensorflow keras
$ pip install /tmp/tensorflow_pkg/tensorflow-1.0.1-cp35-cp35m-macosx_10_7_x86_64.whl
$ pip install keras

curl -O https://raw.githubusercontent.com/fchollet/keras/master/examples/mnist_cnn.py
echo 'K.clear_session()' >> mnist_cnn.py

とした後に

python mnist_cnn.py

でmnistが動く。

Using TensorFlow backend.
x_train shape: (60000, 28, 28, 1)
60000 train samples
10000 test samples
Train on 60000 samples, validate on 10000 samples
Epoch 1/12
60000/60000 [==============================] - 100s - loss: 0.3406 - acc: 0.8961 - val_loss: 0.0818 - val_acc: 0.9734
Epoch 2/12
60000/60000 [==============================] - 96s - loss: 0.1207 - acc: 0.9643 - val_loss: 0.0533 - val_acc: 0.9822
Epoch 3/12
60000/60000 [==============================] - 95s - loss: 0.0891 - acc: 0.9743 - val_loss: 0.0461 - val_acc: 0.9852

1epochあたり100s(Core i7のiMac)。Core i5のmac miniだと

Epoch 10/12
60000/60000 [==============================] - 195s - loss: 0.0391 - acc: 0.9883 - val_loss: 0.0277 - val_acc: 0.9905
Epoch 11/12
60000/60000 [==============================] - 219s - loss: 0.0379 - acc: 0.9893 - val_loss: 0.0306 - val_acc: 0.9899
Epoch 12/12
60000/60000 [==============================] - 247s - loss: 0.0359 - acc: 0.9887 - val_loss: 0.0266 - val_acc: 0.9909

ちなみにGeForce 970だと1 epochあたり9s。これだけ頑張っても10倍遅い。GeForce 1080だと1 epochあたり4s。つらい

Category: Memo Tagged: macOS Tensorflow