Opensourcetechブログ

OpensourcetechによるLinux・オープンソース・IT技術などに関するブログです。

AIを学ぶ5 〜TensorFlow & Keras を使ってみる〜

 

 

こんにちは、オープンソース技研鯨井貴博です。

 

今回は、AI(深層学習や機械学習)に利用できる Pythonのライブラリ、

TensorFlow と Kerasを使ってみます。

 

 

TensorFlow とは?

Google が開発し公開している 機械学習の為のライブラリです。

TensorFlow

 ※テンサーフロー、テンソルフロー 両方で世の中では呼ばれているようですが、

 リンクをみると後者が正しい模様。

 

オープンソースなので、ソースコードGithubで公開されています。

github.com

 

Keras とは?

Pythonで書かれたTensorFlow(そのほか、CNTK・Theanoなど)で実行可能な、高水準のニューラルネットワークライブラリです。

https://keras.io/ja/

 

こちらもオープンソースなので、Githubでコードが公開されています。

github.com

 

 

pip3 を使用してインストール

pip3 から各ライブラリをインストールします。

なお、pip3が入っていない場合、以下を参照ください。

opensourcetech.hatenablog.jp

HayashiyaKikuzo:~ xxx$ pip3 install --upgrade tensorflow・・・・TensorFlowのインストール
Collecting tensorflow
Using cached https://files.pythonhosted.org/packages/57/38/96ee343729363297d62311272adc28b466d30f74c87dc645badb0102af56/tensorflow-1.9.0-cp36-cp36m-macosx_10_11_x86_64.whl
Requirement not upgraded as not directly required: tensorboard<1.10.0,>=1.9.0 in /usr/local/lib/python3.6/site-packages (from tensorflow) (1.9.0)
Requirement not upgraded as not directly required: grpcio>=1.8.6 in /usr/local/lib/python3.6/site-packages (from tensorflow) (1.13.0)
Requirement not upgraded as not directly required: termcolor>=1.1.0 in /usr/local/lib/python3.6/site-packages (from tensorflow) (1.1.0)
Requirement not upgraded as not directly required: wheel>=0.26 in /usr/local/lib/python3.6/site-packages (from tensorflow) (0.31.0)
Requirement not upgraded as not directly required: gast>=0.2.0 in /usr/local/lib/python3.6/site-packages (from tensorflow) (0.2.0)
Requirement not upgraded as not directly required: setuptools<=39.1.0 in /usr/local/lib/python3.6/site-packages (from tensorflow) (39.0.1)
Requirement not upgraded as not directly required: six>=1.10.0 in /usr/local/lib/python3.6/site-packages (from tensorflow) (1.11.0)
Requirement not upgraded as not directly required: numpy>=1.13.3 in /usr/local/lib/python3.6/site-packages (from tensorflow) (1.14.5)
Requirement not upgraded as not directly required: astor>=0.6.0 in /usr/local/lib/python3.6/site-packages (from tensorflow) (0.7.1)
Requirement not upgraded as not directly required: protobuf>=3.4.0 in /usr/local/lib/python3.6/site-packages (from tensorflow) (3.6.0)
Requirement not upgraded as not directly required: absl-py>=0.1.6 in /usr/local/lib/python3.6/site-packages (from tensorflow) (0.2.2)
Requirement not upgraded as not directly required: markdown>=2.6.8 in /usr/local/lib/python3.6/site-packages (from tensorboard<1.10.0,>=1.9.0->tensorflow) (2.6.11)
Requirement not upgraded as not directly required: werkzeug>=0.11.10 in /usr/local/lib/python3.6/site-packages (from tensorboard<1.10.0,>=1.9.0->tensorflow) (0.14.1)
Installing collected packages: tensorflow
Successfully installed tensorflow-1.9.0
You are using pip version 10.0.1, however version 18.0 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.

HayashiyaKikuzo:~ xxx$ pip3 install keras・・・・Kerasのインストール
Collecting keras
Downloading https://files.pythonhosted.org/packages/68/12/4cabc5c01451eb3b413d19ea151f36e33026fc0efb932bf51bcaf54acbf5/Keras-2.2.0-py2.py3-none-any.whl (300kB)
100% |████████████████████████████████| 307kB 5.0MB/s
Collecting keras-applications==1.0.2 (from keras)
Downloading https://files.pythonhosted.org/packages/e2/60/c557075e586e968d7a9c314aa38c236b37cb3ee6b37e8d57152b1a5e0b47/Keras_Applications-1.0.2-py2.py3-none-any.whl (43kB)
100% |████████████████████████████████| 51kB 9.8MB/s
Collecting keras-preprocessing==1.0.1 (from keras)
Downloading https://files.pythonhosted.org/packages/f8/33/275506afe1d96b221f66f95adba94d1b73f6b6087cfb6132a5655b6fe338/Keras_Preprocessing-1.0.1-py2.py3-none-any.whl
Collecting scipy>=0.14 (from keras)
Downloading https://files.pythonhosted.org/packages/a0/b6/70bf61c1badb5fea82d4c558e05e76c2dee5e77bb072fe465d7c7a87287d/scipy-1.1.0-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.7MB)
100% |████████████████████████████████| 16.7MB 2.0MB/s
Requirement already satisfied: numpy>=1.9.1 in /usr/local/lib/python3.6/site-packages (from keras) (1.14.5)
Collecting h5py (from keras)
Downloading https://files.pythonhosted.org/packages/44/81/50a0560aac57a33c2a624d9e160735f39d7a6324e3f6f115425a1bf01dd9/h5py-2.8.0-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 (6.0MB)
100% |████████████████████████████████| 6.1MB 5.8MB/s
Collecting pyyaml (from keras)
Downloading https://files.pythonhosted.org/packages/9e/a3/1d13970c3f36777c583f136c136f804d70f500168edc1edea6daa7200769/PyYAML-3.13.tar.gz (270kB)
100% |████████████████████████████████| 276kB 20.2MB/s
Requirement already satisfied: six>=1.9.0 in /usr/local/lib/python3.6/site-packages (from keras) (1.11.0)
Building wheels for collected packages: pyyaml
Running setup.py bdist_wheel for pyyaml ... done
Stored in directory: /Users/xxx/Library/Caches/pip/wheels/ad/da/0c/74eb680767247273e2cf2723482cb9c924fe70af57c334513f
Successfully built pyyaml
Installing collected packages: h5py, keras-applications, scipy, keras-preprocessing, pyyaml, keras
Successfully installed h5py-2.8.0 keras-2.2.0 keras-applications-1.0.2 keras-preprocessing-1.0.1 pyyaml-3.13 scipy-1.1.0
You are using pip version 10.0.1, however version 18.0 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.

HayashiyaKikuzo:~ xxx$ pip3 list
Package Version
------------------- -------
absl-py 0.2.2
appnope 0.1.0
astor 0.7.1
backcall 0.1.0
bleach 2.1.3
cycler 0.10.0
decorator 4.3.0
entrypoints 0.2.3
gast 0.2.0
grpcio 1.13.0
h5py 2.8.0
html5lib 1.0.1
ipykernel 4.8.2
ipython 6.4.0
ipython-genutils 0.2.0
ipywidgets 7.3.0
jedi 0.12.1
Jinja2 2.10
jsonschema 2.6.0
jupyter 1.0.0
jupyter-client 5.2.3
jupyter-console 5.2.0
jupyter-core 4.4.0
Keras 2.2.0    <----------------Keras、入りました!
Keras-Applications 1.0.2
Keras-Preprocessing 1.0.1
kiwisolver 1.0.1
Markdown 2.6.11
MarkupSafe 1.0
matplotlib 2.2.2
mistune 0.8.3
nbconvert 5.3.1
nbformat 4.4.0
neo4j-driver 1.5.3
notebook 5.6.0
numpy 1.14.5
pandocfilters 1.4.2
parso 0.3.1
pexpect 4.6.0
pickleshare 0.7.4
pip 10.0.1
prometheus-client 0.3.0
prompt-toolkit 1.0.15
protobuf 3.6.0
ptyprocess 0.6.0
Pygments 2.2.0
pyparsing 2.2.0
python-dateutil 2.7.3
pytz 2018.5
PyYAML 3.13
pyzmq 17.1.0
qtconsole 4.3.1
scipy 1.1.0
Send2Trash 1.5.0
setuptools 39.0.1
simplegeneric 0.8.1
six 1.11.0
tensorboard 1.9.0
tensorflow 1.9.0    <----------------TensorFlow、入りました!
termcolor 1.1.0
terminado 0.8.1
testpath 0.3.1
tornado 5.1
traitlets 4.3.2
wcwidth 0.1.7
webencodings 0.5.1
Werkzeug 0.14.1
wheel 0.31.0
widgetsnbextension 3.3.0
You are using pip version 10.0.1, however version 18.0 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.

 

 

 TensorFlow を試してみる

Install TensorFlow on macOS  |  TensorFlow

f:id:opensourcetech:20180723162517p:plain

上記リンク内にあるサンプルを使って、TensorFlow を試してみます。

HayashiyaKikuzo:~ xxx$ python3
Python 3.6.5 |Anaconda, Inc.| (default, Apr 26 2018, 08:42:37)
[GCC 4.2.1 Compatible Clang 4.0.1 (tags/RELEASE_401/final)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf
/Users/xxx/anaconda3/lib/python3.6/site-packages/h5py/__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.
from ._conv import register_converters as _register_converters
>>> hello = tf.constant('Hello, TensorFlow!')
>>> sess = tf.Session()
2018-07-22 15:38:00.014977: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
>>> print(sess.run(hello))
b'Hello, TensorFlow!'   <--------何故だろう、先頭に「b」がつく!?
>>> print(sess.run(hello).decode())   <-------先頭に「b」がつく問題対応
Hello, TensorFlow!
>>> exit()

 

上記、先頭に「b」がつく問題ですが、

StackOverFlow から対応策がわかりました。

stackoverflow.com

 

 

 TensorFlow & Keras を試してみる

こちらは、以下のチュートリアルにあるサンプルを使って試してみます。

www.tensorflow.orgf:id:opensourcetech:20180723163028p:plain

HayashiyaKikuzo:~ xxx$ python3
Python 3.6.5 |Anaconda, Inc.| (default, Apr 26 2018, 08:42:37)
[GCC 4.2.1 Compatible Clang 4.0.1 (tags/RELEASE_401/final)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf
/Users/xxx/anaconda3/lib/python3.6/site-packages/h5py/__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.
from ._conv import register_converters as _register_converters
>>> mnist = tf.keras.datasets.mnist
>>> (x_train, y_train),(x_test, y_test) = mnist.load_data()
Downloading data from https://s3.amazonaws.com/img-datasets/mnist.npz
11493376/11490434 [==============================] - 4s 0us/step
>>> x_train, x_test = x_train / 255.0, x_test / 255.0
>>> model = tf.keras.models.Sequential([
... tf.keras.layers.Flatten(),
... tf.keras.layers.Dense(512, activation=tf.nn.relu),
... tf.keras.layers.Dropout(0.2),
... tf.keras.layers.Dense(10, activation=tf.nn.softmax)
... ])
>>> model.compile(optimizer='adam',
... loss='sparse_categorical_crossentropy',
... metrics=['accuracy'])
>>> model.fit(x_train, y_train, epochs=5)
Epoch 1/5
2018-07-22 15:47:52.769419: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
60000/60000 [==============================] - 15s 249us/step - loss: 0.2201 - acc: 0.9349
Epoch 2/5
60000/60000 [==============================] - 14s 237us/step - loss: 0.0971 - acc: 0.9710
Epoch 3/5
60000/60000 [==============================] - 14s 238us/step - loss: 0.0681 - acc: 0.9786
Epoch 4/5
60000/60000 [==============================] - 14s 236us/step - loss: 0.0536 - acc: 0.9825
Epoch 5/5
60000/60000 [==============================] - 14s 235us/step - loss: 0.0435 - acc: 0.9858
<tensorflow.python.keras.callbacks.History object at 0xb1feab5f8>
>>> model.evaluate(x_test, y_test)
10000/10000 [==============================] - 0s 43us/step
[0.06850039585522026, 0.9803]
>>> exit()

 

 

  TensorFlow インストールに関するトラブルシュート

TensorFlow のインストールを、当初「pip3 install tensorflow」で行ったのですが

モジュールの読み込みの際、以下のようなエラーが出力されました。

HayashiyaKikuzo:~ xxx$ python3
Python 3.6.5 |Anaconda, Inc.| (default, Apr 26 2018, 08:42:37)
[GCC 4.2.1 Compatible Clang 4.0.1 (tags/RELEASE_401/final)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'tensorflow'  <--- 出力されたエラーメッセージ
>>> exit()

 

以下、解決策。(TensorFlowを --upgradeオプションをつけて再インストール)

HayashiyaKikuzo:~ xxx$ pip3 uninstall tensorflow・・・・一旦、アンインストール
Uninstalling tensorflow-1.9.0:
Would remove:
/usr/local/bin/freeze_graph
/usr/local/bin/saved_model_cli
/usr/local/bin/tensorboard
/usr/local/bin/tflite_convert
/usr/local/bin/toco
/usr/local/bin/toco_from_protos
/usr/local/lib/python3.6/site-packages/tensorflow-1.9.0.dist-info/*
/usr/local/lib/python3.6/site-packages/tensorflow/*
Proceed (y/n)? y
Successfully uninstalled tensorflow-1.9.0
You are using pip version 10.0.1, however version 18.0 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
HayashiyaKikuzo:~ xxx$ pip3 list
Package Version
------------------- -------
absl-py 0.2.2
appnope 0.1.0
astor 0.7.1
backcall 0.1.0
bleach 2.1.3
cycler 0.10.0
decorator 4.3.0
entrypoints 0.2.3
gast 0.2.0
grpcio 1.13.0
h5py 2.8.0
html5lib 1.0.1
ipykernel 4.8.2
ipython 6.4.0
ipython-genutils 0.2.0
ipywidgets 7.3.0
jedi 0.12.1
Jinja2 2.10
jsonschema 2.6.0
jupyter 1.0.0
jupyter-client 5.2.3
jupyter-console 5.2.0
jupyter-core 4.4.0
Keras 2.2.0
Keras-Applications 1.0.2
Keras-Preprocessing 1.0.1
kiwisolver 1.0.1
Markdown 2.6.11
MarkupSafe 1.0
matplotlib 2.2.2
mistune 0.8.3
nbconvert 5.3.1
nbformat 4.4.0
neo4j-driver 1.5.3
notebook 5.6.0
numpy 1.14.5
pandocfilters 1.4.2
parso 0.3.1
pexpect 4.6.0
pickleshare 0.7.4
pip 10.0.1
prometheus-client 0.3.0
prompt-toolkit 1.0.15
protobuf 3.6.0
ptyprocess 0.6.0
Pygments 2.2.0
pyparsing 2.2.0
python-dateutil 2.7.3
pytz 2018.5
PyYAML 3.13
pyzmq 17.1.0
qtconsole 4.3.1
scipy 1.1.0
Send2Trash 1.5.0
setuptools 39.0.1
simplegeneric 0.8.1
six 1.11.0
tensorboard 1.9.0
termcolor 1.1.0
terminado 0.8.1
testpath 0.3.1
tornado 5.1
traitlets 4.3.2
wcwidth 0.1.7
webencodings 0.5.1
Werkzeug 0.14.1
wheel 0.31.0
widgetsnbextension 3.3.0
You are using pip version 10.0.1, however version 18.0 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
HayashiyaKikuzo:~ xxx$ pip3 install --upgrade tensorflow・・・・再インストール
・・・・省略・・・・

 

 

  まとめ

とりあえず、サンプル通りではありますが TensorFlow / Keras を試すことが出来ました。

なお、実際にTensorFlow や Keras を使う上では、以下についても理解し何を分析するかなど決めておく必要がありそうです。

ニュートラルネットワーク

ディープラーニング

パーセプトロン

・ロジスティック回帰

・二値分類

多項式回帰

・線形回帰

などなど

 

 

オープンソース技研

www.slideshare.net

github.com

www.facebook.com

twitter.com

www.instagram.com

 

 

にほんブログ村 IT技術ブログ Linuxへ
Linux

にほんブログ村 IT技術ブログ オープンソースへ
オープンソース

 

bitFlyer ビットコインを始めるなら安心・安全な取引所で

ビットコイン取引高日本一の仮想通貨取引所 coincheck bitcoin

 

 

Opensourcetech by Takahiro Kujirai