はりうすブログ (のすけのメモ)

湘南にある小さな会社 代表 ”のすけ”のブログです

Googleのオープンソース人工知能エンジン TensorFlow導入メモ

こんにちは、のすけです。

でましたね、Google人工知能エンジンのオープンソース TensorFlow f:id:hollywis:20151112185244p:plain

実は最近、人工知能学会に入会した僕としては飛びつかずにはいられない

というわけで実際にTensorFlowを触ってみました。

今回はPython APIを利用してみたいと思います。

今回の環境

Pythonのインストール

www.python.org

Macなら下記コマンドでokです

brew install python

TensorFlowのインストール

pipを使ってインストールします (MacにはまだCPU-versionバージョンしかないようです。GPU版はもう少し待ちましょう )

pip install https://storage.googleapis.com/tensorflow/mac/tensorflow-0.5.0-py2-none-any.whl

Collecting tensorflow==0.5.0 from https://storage.googleapis.com/tensorflow/mac/tensorflow-0.5.0-py2-none-any.whl Downloading https://storage.googleapis.com/tensorflow/mac/tensorflow-0.5.0-py2-none-any.whl (9.8MB) 100% |████████████████████████████████| 9.8MB 33kB/s Collecting six>=1.10.0 (from tensorflow==0.5.0) Downloading six-1.10.0-py2.py3-none-any.whl Collecting numpy>=1.9.2 (from tensorflow==0.5.0) Downloading numpy-1.10.1-cp27-none-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.whl (3.7MB) 100% |████████████████████████████████| 3.7MB 76kB/s Installing collected packages: six, numpy, tensorflow Successfully installed numpy-1.10.1 six-1.10.0 tensorflow-0.5.0

TensorFlowの動作確認

次のように実行してみましょう

$ python
>>> import tensorflow as tf
>>> hello = tf.constant('Hello, TensorFlow!')
>>> sess = tf.Session()
>>> print sess.run(hello)
Hello, TensorFlow!
>>> a = tf.constant(10)
>>> b = tf.constant(32)
>>> print sess.run(a+b)
42

途中でこんな警告出力が発生

I tensorflow/core/common_runtime/local_device.cc:25] Local device intra op parallelism threads: 4
can't determine number of CPU cores: assuming 4
I tensorflow/core/common_runtime/local_session.cc:45] Local session inter op parallelism threads: 4

CPUのコア数が特定できん!と言っているようですね。 とりあえず、気にせず実行しました。

MNIST For ML Beginnersをやってみる

MNIST For ML BeginnersはTensorFlowのサンプルの一つです。

このような手書きの文字を機械学習で分類する課題です。

http://api.tensorflow.org/system/image/body/1700/MNIST.png

qiita.com

に分かりやすい解説がありましたので、参考にやってみました。

Extracting MNIST_data/train-images-idx3-ubyte.gz
Extracting MNIST_data/train-labels-idx1-ubyte.gz
Extracting MNIST_data/t10k-images-idx3-ubyte.gz
Extracting MNIST_data/t10k-labels-idx1-ubyte.gz
can't determine number of CPU cores: assuming 4
I tensorflow/core/common_runtime/local_device.cc:25] Local device intra op parallelism threads: 4
can't determine number of CPU cores: assuming 4
I tensorflow/core/common_runtime/local_session.cc:45] Local session inter op parallelism threads: 4
**結果**
0.9093

データ読み込みから結果まで、瞬殺でした。 結果は0.9093

「This should be about 91%..と」ということですので、概ねその通りになりましたね。

続いてDeep MNIST for Expertsもやってみる

Beginnersが91%程度の識別率でしたが、なんとExpertsの方は99.7%overの識別率だそうです。 その手法とは、、、deep convolutional. つまり今話題の深層畳み込みニューラルネットワーク(ディープラーニングの一つ)です。

step 0, training accuracy 0.1
step 100, training accuracy 0.78
step 200, training accuracy 0.88
step 300, training accuracy 0.9
step 400, training accuracy 0.94
step 500, training accuracy 0.92
step 600, training accuracy 1
step 700, training accuracy 0.94
step 800, training accuracy 0.96
step 900, training accuracy 0.96
step 1000, training accuracy 0.98
step 1100, training accuracy 0.98
step 1200, training accuracy 0.96
step 1300, training accuracy 1
step 1400, training accuracy 0.98
step 1500, training accuracy 0.98
step 1600, training accuracy 0.92
step 1700, training accuracy 1
step 1800, training accuracy 1
step 1900, training accuracy 0.98
step 2000, training accuracy 0.92

実行過程が表示される。step数が増えるに従ってaccuracy(正確さ)が1に近づいているのが分かる。

f:id:hollywis:20151112145330p:plain:w300 f:id:hollywis:20151112145849p:plain

実行中のCPUの様子。

そして、約1時間半後。。。

test  accuracy 0.9925

99.2%という結果でした。本当に凄いですね。

TensorFlowには他にも例があるため、いろいろ試してみたいと思います。

次の記事は↓

hollywis.hatenablog.com