TensorFlow + Keras入門 ~環境構築からサンプルコード実行まで~

  • このエントリーをはてなブックマークに追加
  • Pocket
  • LINEで送る
         

はじめに

機械学習を手軽に試せるライブラリであるTensorFlowと、そのラッパーライブラリであるKerasを使ってみたいと思います。

TensorFlowはGoogleが公開しているライブラリであり、機械学習やディープラーニングを始めとする様々な演算に使用することが出来ます。

TensorFlowだけでも機械学習は実装できますが、Kerasを使うことでより短く簡単にプログラムを実装できるので、さくっとプロトタイピングしたい時におすすめです。

TensorFlow
Keras公式サイト(日本語)

 

動作環境

  • macOS High Sierra Version 10.13
  • python 3.6.3
  • pyenv 1.1.5
  • anaconda 3-5.0.0
  • Keras 2.0.8
  • tensorflow 1.3.0

 

Anacondaのインストール

Anacondaは機械学習用のライブラリがバンドル化されたパッケージです。軽量化したパッケージにMinicondaというものもありますが、今回はAnacondaを使って環境を構築します。

pyenv install

まずはPythonのバージョン管理にpyenvを準備します。

$ brew update
$ brew install pyenv

既にpyenvがインストールされていれば不要です。

Anaconda install

Anacondaの最新バージョンを確認し、バージョン指定でインストールします。

$ pyenv install -l
$ pyenv install anaconda3-5.0.0
$ pyenv versions

** pyenvが必要ない場合は公式のインストーラをダウンロードしてください **
公式インストーラ

環境をAnacondaに切り替え

$ pyenv global anaconda3-5.0.0

Anacondaのパスを通す

$ echo 'PATH="~/.pyenv/versions/anaconda3-5.0.0/bin:$PATH"' >> ~/.bash_profile
$ source ~/.bash_profile

Keras実行用の仮想環境作成

$ conda create -n keras python=3.6.3
Fetching package metadata ...........
Solving package specifications: .

Package plan for installation in environment /Users/yuichi/.pyenv/versions/anaconda3-5.0.0/envs/keras:

The following NEW packages will be INSTALLED:

ca-certificates: 2017.08.26-ha1e5d58_0
certifi: 2017.7.27.1-py36hd973bb6_0
libcxx: 4.0.1-h579ed51_0
libcxxabi: 4.0.1-hebd6815_0
libedit: 3.1-hb4e282d_0
libffi: 3.2.1-hd939716_3
ncurses: 6.0-ha932d30_1
openssl: 1.0.2l-h57f3a61_2
pip: 9.0.1-py36hbd95645_3
python: 3.6.3-h6804ab2_0
readline: 7.0-h81b24a6_3
setuptools: 36.5.0-py36h2134326_0
sqlite: 3.20.1-h900c3b0_1
tk: 8.6.7-hcdce994_1
wheel: 0.29.0-py36h3597b6d_1
xz: 5.2.3-ha24016e_1
zlib: 1.2.11-h60db283_1

Proceed ([y]/n)? y

続けるかどうか聞かれるのでyで進みます。

仮想環境(keras)をアクティベート

$ source ~/.pyenv/versions/anaconda3-5.0.0/bin/activate keras
(keras) $ conda install -c conda-forge tensorflow
Fetching package metadata .............
Solving package specifications: .

Package plan for installation in environment /Users/yuichi/.pyenv/versions/anaconda3-5.0.0/envs/keras:

The following NEW packages will be INSTALLED:

backports: 1.0-py36_1 conda-forge
backports.weakref: 1.0rc1-py36_1 conda-forge
bleach: 1.5.0-py36_0 conda-forge
html5lib: 0.9999999-py36_0 conda-forge
intel-openmp: 2018.0.0-h68bdfb3_7
libgfortran: 3.0.1-h93005f0_2
markdown: 2.6.9-py36_0 conda-forge
mkl: 2018.0.0-h5ef208c_6
mock: 2.0.0-py36_0 conda-forge
numpy: 1.13.3-py36h2cdce51_0
pbr: 3.1.1-py36_0 conda-forge
protobuf: 3.3.2-py36_0 conda-forge
six: 1.11.0-py36_1 conda-forge
tensorflow: 1.3.0-py36_0 conda-forge
webencodings: 0.5-py36_0 conda-forge
werkzeug: 0.12.2-py_1 conda-forge

Proceed ([y]/n)? y

同様に続けるかどうか聞かれるのでyで進みます。

(keras) $ pip install keras h5py
Collecting keras
Downloading Keras-2.0.8-py2.py3-none-any.whl (276kB)
100% |████████████████████████████████| 276kB 1.3MB/s
Collecting h5py
Downloading h5py-2.7.1-cp36-cp36m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.whl (4.7MB)
100% |████████████████████████████████| 4.8MB 197kB/s
Collecting scipy>=0.14 (from keras)
Downloading scipy-0.19.1-cp36-cp36m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.whl (16.2MB)
100% |████████████████████████████████| 16.2MB 57kB/s
Requirement already satisfied: six>=1.9.0 in ./.pyenv/versions/anaconda3-5.0.0/envs/keras/lib/python3.6/site-packages (from keras)
Requirement already satisfied: numpy>=1.9.1 in ./.pyenv/versions/anaconda3-5.0.0/envs/keras/lib/python3.6/site-packages (from keras)
Collecting pyyaml (from keras)
Downloading PyYAML-3.12.tar.gz (253kB)
100% |████████████████████████████████| 256kB 1.5MB/s
Building wheels for collected packages: pyyaml
Running setup.py bdist_wheel for pyyaml ... done
Stored in directory: /Users/yuichi/Library/Caches/pip/wheels/2c/f7/79/13f3a12cd723892437c0cfbde1230ab4d82947ff7b3839a4fc
Successfully built pyyaml
Installing collected packages: scipy, pyyaml, keras, h5py
Successfully installed h5py-2.7.1 keras-2.0.8 pyyaml-3.12 scipy-0.19.1

成功しました!

KerasのバックエンドにTensorflowを設定されていることを確認します

$ less ~/.keras/keras.json
{
"floatx": "float32",
"epsilon": 1e-07,
"backend": "tensorflow",
"image_data_format": "channels_last"
}

もし作成されていなければ、jsonファイルを作成して下さい。
デフォルトで設定されていたら変更は必要ありません。

サンプルコード実行

公式にいくつかサンプルコードが公開されているので、今回はMNISTを試してみます。

Kerasサンプルコード

MNIST

サンプルコードの内容です。

'''Trains a simple deep NN on the MNIST dataset.

Gets to 98.40% test accuracy after 20 epochs
(there is *a lot* of margin for parameter tuning).
2 seconds per epoch on a K520 GPU.
'''

from __future__ import print_function

import keras
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense, Dropout
from keras.optimizers import RMSprop

batch_size = 128
num_classes = 10
epochs = 20

# the data, shuffled and split between train and test sets
(x_train, y_train), (x_test, y_test) = mnist.load_data()

x_train = x_train.reshape(60000, 784)
x_test = x_test.reshape(10000, 784)
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train /= 255
x_test /= 255
print(x_train.shape[0], 'train samples')
print(x_test.shape[0], 'test samples')

# convert class vectors to binary class matrices
y_train = keras.utils.to_categorical(y_train, num_classes)
y_test = keras.utils.to_categorical(y_test, num_classes)

model = Sequential()
model.add(Dense(512, activation='relu', input_shape=(784,)))
model.add(Dropout(0.2))
model.add(Dense(512, activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(num_classes, activation='softmax'))

model.summary()

model.compile(loss='categorical_crossentropy',
optimizer=RMSprop(),
metrics=['accuracy'])

history = model.fit(x_train, y_train,
batch_size=batch_size,
epochs=epochs,
verbose=1,
validation_data=(x_test, y_test))
score = model.evaluate(x_test, y_test, verbose=0)
print('Test loss:', score[0])
print('Test accuracy:', score[1])

それでは実行してみましょう。

(keras) $ python mnist_mlp.py
Using TensorFlow backend.
Downloading data from https://s3.amazonaws.com/img-datasets/mnist.npz
11337728/11490434 [============================>.] - ETA: 0s60000 train samples
10000 test samples
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
dense_1 (Dense) (None, 512) 401920
_________________________________________________________________
dropout_1 (Dropout) (None, 512) 0
_________________________________________________________________
dense_2 (Dense) (None, 512) 262656
_________________________________________________________________
dropout_2 (Dropout) (None, 512) 0
_________________________________________________________________
dense_3 (Dense) (None, 10) 5130
=================================================================
Total params: 669,706
Trainable params: 669,706
Non-trainable params: 0
_________________________________________________________________
Train on 60000 samples, validate on 10000 samples
Epoch 1/20
2017-10-21 13:17:49.303551: 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.
2017-10-21 13:17:49.303872: 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.
2017-10-21 13:17:49.303893: 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.
2017-10-21 13:17:49.303906: 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.
60000/60000 [==============================] - 16s - loss: 0.2418 - acc: 0.9246 - val_loss: 0.1184 - val_acc: 0.9649
Epoch 2/20
60000/60000 [==============================] - 14s - loss: 0.1030 - acc: 0.9683 - val_loss: 0.0931 - val_acc: 0.9727
Epoch 3/20
60000/60000 [==============================] - 14s - loss: 0.0743 - acc: 0.9780 - val_loss: 0.0732 - val_acc: 0.9780
Epoch 4/20
60000/60000 [==============================] - 16s - loss: 0.0604 - acc: 0.9821 - val_loss: 0.0772 - val_acc: 0.9795
Epoch 5/20
60000/60000 [==============================] - 16s - loss: 0.0497 - acc: 0.9853 - val_loss: 0.0744 - val_acc: 0.9807
Epoch 6/20
60000/60000 [==============================] - 14s - loss: 0.0446 - acc: 0.9867 - val_loss: 0.0812 - val_acc: 0.9807
Epoch 7/20
60000/60000 [==============================] - 14s - loss: 0.0381 - acc: 0.9889 - val_loss: 0.0924 - val_acc: 0.9783
Epoch 8/20
60000/60000 [==============================] - 14s - loss: 0.0348 - acc: 0.9899 - val_loss: 0.0817 - val_acc: 0.9844
Epoch 9/20
60000/60000 [==============================] - 14s - loss: 0.0327 - acc: 0.9905 - val_loss: 0.0917 - val_acc: 0.9806
Epoch 10/20
60000/60000 [==============================] - 15s - loss: 0.0295 - acc: 0.9916 - val_loss: 0.1015 - val_acc: 0.9816
Epoch 11/20
60000/60000 [==============================] - 13s - loss: 0.0272 - acc: 0.9924 - val_loss: 0.0938 - val_acc: 0.9832
Epoch 12/20
60000/60000 [==============================] - 13s - loss: 0.0251 - acc: 0.9932 - val_loss: 0.0991 - val_acc: 0.9822
Epoch 13/20
60000/60000 [==============================] - 13s - loss: 0.0235 - acc: 0.9936 - val_loss: 0.1054 - val_acc: 0.9825
Epoch 14/20
60000/60000 [==============================] - 12s - loss: 0.0225 - acc: 0.9940 - val_loss: 0.1040 - val_acc: 0.9821
Epoch 15/20
60000/60000 [==============================] - 12s - loss: 0.0218 - acc: 0.9939 - val_loss: 0.0985 - val_acc: 0.9835
Epoch 16/20
60000/60000 [==============================] - 12s - loss: 0.0210 - acc: 0.9947 - val_loss: 0.1035 - val_acc: 0.9839
Epoch 17/20
60000/60000 [==============================] - 12s - loss: 0.0200 - acc: 0.9948 - val_loss: 0.1114 - val_acc: 0.9824
Epoch 18/20
60000/60000 [==============================] - 12s - loss: 0.0203 - acc: 0.9949 - val_loss: 0.1085 - val_acc: 0.9833
Epoch 19/20
60000/60000 [==============================] - 12s - loss: 0.0203 - acc: 0.9946 - val_loss: 0.1138 - val_acc: 0.9831
Epoch 20/20
60000/60000 [==============================] - 12s - loss: 0.0196 - acc: 0.9951 - val_loss: 0.1005 - val_acc: 0.9844
Test loss: 0.100473192545
Test accuracy: 0.9844

正解率は0.9844となりました!

サンプルとは言え、かなり高精度ですね。

途中でWarningが出ているのはCPUの拡張命令をしてあげれば解消されるようです。

Python: Keras/TensorFlow の学習を CPU の拡張命令で高速化する (Mac OS X)

まとめ

今回はTensorFlow + Kerasで機械学習するための環境構築からサンプルコードの実行までを行いました。

Kerasはシンプルに実装できそうでいい感じですね。

色々試してみたいと思います!

それではまた。

スポンサーリンク

  • このエントリーをはてなブックマークに追加
  • Pocket
  • LINEで送る

SNSでもご購読できます。