ml5jsのLSTMサンプルを動かしてみる

本を読み始めたのはいいが、とりあえず動かしたい

ゼロから作るDeep Learningをちびちび読んでいるのですが。
ちびちびすぎて、まだ4章。
先が長すぎる。

とりあえず動かしてみよう。

ってことで。

ml5.jsLSTMのサンプルプログラムを動かしてみようと思い立ちました。

LSTMって何?っていう疑問はとりあえず脇に置いておいて。
日本語のデータ作って、動かすぞ。

というのをゴールにすることにします。

Local環境で動かす

とりあえず、ここから一式持ってきました。
https://github.com/ml5js/ml5-examples

で。これがLocalで動くことを確認。
https://github.com/ml5js/ml5-examples/tree/master/p5js/LSTM/LSTM_Text

ここまでは順調。
次はデータ作成。

tensorflowをインストールする

とりあえずここだけを見て。
https://www.tensorflow.org/install/?hl=ja

tensflow

えい。

1
2
3
4
5
6
c:\work\ml5js\training-lstm>pip install tensorflow
Collecting tensorflow
Could not find a version that satisfies the requirement tensorflow (from versions: )
No matching distribution found for tensorflow
You are using pip version 18.0, however version 18.1 is available.
You should consider upgrading via the 'python -m pip install --upgrade pip' command.

だめだった。

言われた通り、pipをupgrade。

1
2
3
4
5
6
7
8
9
c:\work\ml5js\training-lstm>python -m pip install --upgrade pip
Collecting pip
Downloading https://files.pythonhosted.org/packages/c2/d7/90f34cb0d83a6c5631cf71dfe64cc1054598c843a92b400e55675cc2ac37/pip-18.1-py2.py3-none-any.whl (1.3MB)
100% |████████████████████████████████| 1.3MB 4.6MB/s
Installing collected packages: pip
Found existing installation: pip 18.0
Uninstalling pip-18.0:
Successfully uninstalled pip-18.0
Successfully installed pip-18.1

もういっかい。えい。

1
2
3
4
c:\work\ml5js>pip install tensorflow
Collecting tensorflow
Could not find a version that satisfies the requirement tensorflow (from versions: )
No matching distribution found for tensorflow

だめだった。

しかたがないので。

ちゃんとインストールガイドを見ることに。
https://www.tensorflow.org/install/pip?hl=ja

ここまでたどり着いた。

1
2
3
4
5
6
7
8
c:\work\ml5js>python --version
Python 3.6.5

c:\work\ml5js>pip --version
pip 18.1 from c:\users\nahok\appdata\local\programs\python\python36-32\lib\site-packages\pip (python 3.6)

c:\work\ml5js>virtualenv --version
16.2.0

しかしインストールできない。

URL指定でもやってみた。

1
2
C:\work\ml5js>pip3 install --upgrade https://storage.googleapis.com/tensorflow/windows/cpu/tensorflow-1.12.0-cp36-cp36m-win_amd64.whl
tensorflow-1.12.0-cp36-cp36m-win_amd64.whl is not a supported wheel on this platform.

だめだった。

そして気づく。

おや。ぼくのPython、32bit?

1
2
3
C:\work\ml5js>python
Python 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 16:07:46) [MSC v.1900 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.

PC自体が64bitなのは知っていたのでノーケアだった。

もはや。

いつ、どうやってpythonをインストールしたのか定かではない。
Pythonには32bitと64bitがあるのか。そうか。そうなのか。

入れなおした。(追加して入れた。Pathは入れてから環境変数直した)

1
2
3
4
C:\work\ml5js>python
Python 3.6.8 (tags/v3.6.8:3c6b436a57, Dec 24 2018, 00:16:47) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>

えい。

1
2
3
C:\work\ml5js>pip3 install --upgrade https://storage.googleapis.com/tensorflow/windows/cpu/tensorflow-1.12.0-cp36-cp36m-win_amd64.whl
(中略)
Successfully installed tensorflow-1.12.0

やっと成功した。

virtualenvいれたのに普通にインストールしちゃった。

気にしない。

チュートリアルTraining a LSTM

やっと本題。

このチュートリアルをすすめます。
https://ml5js.org/docs/training-lstm

  1. トレーニング用のリポジトリから一式持ってくる

gitでcloneしてくるだけ。

1
2
3
4
5
6
7
C:\work\ml5js>git clone https://github.com/ml5js/training-lstm.git
Cloning into 'training-lstm'...
remote: Enumerating objects: 6, done.
remote: Counting objects: 100% (6/6), done.
remote: Compressing objects: 100% (6/6), done.
remote: Total 30 (delta 2), reused 0 (delta 0), pack-reused 24
Unpacking objects: 100% (30/30), done.
  1. データを持ってくる

さて。どうしよう。青空文庫でいっか。

こちらを参考に。(というかそのまま実行)
【Python】MeCabを使って青空文庫を簡単に分かち書きする方法

GoogleColabって初めて知りました。
すごいですね。

ここでゲットした「吾輩は猫である」の分かち書きテキストを。
トレーニングのインプットにするべく。
mydataフォルダを作って、input.txtという名前で保存。

  1. トレーニング!!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
c:\work\ml5js\training-lstm>python train.py --data_dir=.\mydata
Here we go! Reading text file...
0/10500 (epoch 0), train_loss = 8.013, time/batch = 0.963
Model saved to checkpoints\.\mydata\.\mydata!
1/10500 (epoch 0), train_loss = 7.999, time/batch = 0.691
2/10500 (epoch 0), train_loss = 7.961, time/batch = 0.675
(中略)
10497/10500 (epoch 49), train_loss = 1.844, time/batch = 0.602
10498/10500 (epoch 49), train_loss = 1.918, time/batch = 0.614
10499/10500 (epoch 49), train_loss = 1.826, time/batch = 0.593
Model saved to checkpoints\.\mydata\.\mydata!
Converting model to ml5js: .\mydata .\mydata-10499
Done! The output model is in models
Check https://ml5js.org/docs/training-lstm for more information.

終わった!!

どれくらい時間かかったかわからないんですが。

0.6秒 × 10500 = 105分

くらいでしょうか。

4.自分のデータを使う

LSTM_Textのmodelsフォルダにフォルダごと放り込む。
lstm

sketch.jsのデータ指定しているところを変更する。

1
2
3
// Create the LSTM Generator passing it the model directory
//charRNN = ml5.charRNN('./models/woolf/', modelReady);
charRNN = ml5.charRNN('./models/mydata/', modelReady);

index.htmlも、ちょろちょろ見た目を修正して。
できたー。

lstm text

まとめ

だから、何?という感じ。

せっかくなのでアップしてみた。
http://rissoku.net/petitapp/LSTM_Text/

さ。

ゼロから作るDeep Learningの続き読もっと。