まんぼう日記

takataka's diary

Mac に TensorFlow をインストールしてロジスティック回帰させてみた

たかたかは, Deep Learning 関係で遊ぶ/研究する際にこれまで Theano を主に使ってきた(つい先日 0.9.0 が出ましたね: Welcome — Theano 0.9.0 documentation )んですが,ちょっと試しに TensorFlow をインストールして動かしてみることにしました.

 

 Mac に TensorFlow をインストール

Installing TensorFlow on Mac OS X  |  TensorFlow  を参考にしたらあっと言う間でした.こういう前提:

  •  Python の環境環境は,MacPorts で入れた Python 3.5 (2.7から移行しようとしてた所なので,python35 のインストールから)
  •  virtualenv を使う(たかたかは pip 使ってないので,MacPorts でインストール)
  •  TensorFlow with CPU support only の方を入れる

 

準備

まずは MacPorts で python35 と py35-ipython をインストール.

$ sudo port install python35
$ sudo port select --set python python35
$ sudo port select --set python3 python35
$ sudo port install py35-ipython35
$ sudo port select --set ipython py35-ipython
$ sudo port select --set ipython3 py35-ipython

port select して,それぞれ python/python3 および ipython/ipython3 で起動するようにもしときました.

 

次は,py35-pip と py35-virtualenv をインストール.

$ sudo port install py35-pip
$ sudo port select --set pip py35-pip
$ sudo port install py35-virtualenv
$ sudo port select --set virtualenv virtualenv35

virtualenv というのは…,説明が面倒&たかたかもまだよくわかってない所があるので,以下へどうぞ:

 

早速 virtualenv を使い,TensorFlow 用の環境を用意します.ここでは ~/tf というディレクトリに作ることにしました.

$ virtualenv --system-site-packages ~/tf

virtualenv のオプションは,システムに既にインストールされてるものは利用する,という選択を表します.より独立した環境を構築したい場合は --no-site-packages としたらいいみたい.

 

TensorFlow のインストール

上記で作った仮想環境に入ります.

$ source ~/tf/bin/activate 

そして,pip コマンドで tensorflow をインストール.ただ,Installing TensorFlow on Mac OS X  |  TensorFlow  に書いてある通りにやったら

(tf) $ pip install --upgrade tensorflow
Collecting tensorflow
  Could not find a version that satisfies the requirement tensorflow (from versions: )
No matching distribution found for tensorflow

とか怒られてしまいました.Installing with virtualenv の Step 6 に書いてあるように,TF_BINARY_URL を指定したらokでした.

(tf) $ pip install --upgrade https://storage.googleapis.com/tensorflow/mac/cpu/tensorflow-1.0.1-py3-none-any.whl

Successfully installed numpy-1.12.1 protobuf-3.2.0 tensorflow-1.0.1

tensorflow-1.0.1 の他に,この環境に numpy と protobuf もインストールされたようです.

 

TensorFlow でロジスティック回帰

さて何しよっか,ていう所ですが,Theano の時と同じように MNIST のロジスティック回帰をやることにしました.Theano の時の記事はこちら(ついでに隠れ層追加したんも):

 

サンプルプログラムを動かしてみる

Getting Started With TensorFlow  |  TensorFlow に詳しい解説がありますが,「Theano とおんなじ感じやね」と斜め読みして MNIST For ML Beginners  |  TensorFlow へ.そこで mnist_softmax.py ( tensorflow/mnist_softmax.py at master · tensorflow/tensorflow · GitHub ) というサンプルを見つけました.早速実行.

(tf) $ python mnist_softmax.py
Successfully downloaded train-images-idx3-ubyte.gz 9912422 bytes.
Extracting /tmp/tensorflow/mnist/input_data/train-images-idx3-ubyte.gz
Successfully downloaded train-labels-idx1-ubyte.gz 28881 bytes.
Extracting /tmp/tensorflow/mnist/input_data/train-labels-idx1-ubyte.gz
Successfully downloaded t10k-images-idx3-ubyte.gz 1648877 bytes.
Extracting /tmp/tensorflow/mnist/input_data/t10k-images-idx3-ubyte.gz
Successfully downloaded t10k-labels-idx1-ubyte.gz 4542 bytes.
Extracting /tmp/tensorflow/mnist/input_data/t10k-labels-idx1-ubyte.gz
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.
0.9206

最初の方のメッセージは,MNISTのデータをとってきたよって言ってるだけ.最後の 0.9206 というのが,学習後のネットワークにテストデータを食わせて求めた識別率.まあこんなもんでしょう.

 

で,W で始まる3行が気になるところ.AVX命令とか使える CPU やのに TensorFlow のライブラリがそれを使うようにコンパイルされてへんで,やって.うーむ.有効にしたらきっと速くなるんやろけど,それには TensorFlow をソースからインストールする必要がありそう.とりあえず今はこのままで.

 

自分で書いてみる

上記のページではサンプルコードの各行が詳しく解説されてます.それを追っかければ十分理解できると思いますが,勉強を兼ねて自分でも書いてみました.サンプルと同じく MNIST のロジスティック回帰ですが,データの扱いが違ったり,慣性項が付いてたり,細かいところは違います.

 

作ったプログラムはこちら:TensorFlow でロジスティック回帰 · GitHub

 

結果はこんなん.

### training: D = 784  K = 10  NL = 50000  NV = 10000  batchsize = 100
0 2.33671 87.5979997218 88.2100000978
1 0.399208 10.9600007534 9.7500026226
2 0.353739 9.99400019646 9.16000008583
3 0.333163 9.4319999218 8.64999890327
4 0.320639 9.06800031662 8.30000042915
5 0.312122 8.85000228882 8.27000141144
6 0.305403 8.68200063705 8.14999938011
7 0.300177 8.47600102425 8.06999802589
8 0.295925 8.32800269127 7.95000195503
9 0.29234 8.20999741554 7.98000097275
10 0.289298 8.13999772072 7.88000226021
20 0.271896 7.59400129318 7.44000077248
30 0.263191 7.34800100327 7.23999738693
40 0.257762 7.16999769211 7.1799993515
50 0.253725 7.06999897957 7.05999732018
60 0.25063 6.99200034142 7.13999867439
70 0.248064 6.9019973278 7.12000131607
80 0.245952 6.85200095177 7.02999830246
90 0.243965 6.77199959755 7.09000229836
### test: NT = 10000
7.44000077248

誤識別率 7.4%.MNIST の Logistic Regression Theano版 - まんぼう日記 の時と全く同条件というわけではないけど,ほぼ同じ結果.

 

こうなったら,CNN にして,GPU で動かす実験して,といろいろやりたくなりますな.