darknetというC言語で書かれたオープンソースのニューラルネットワーク用フレームワークでRNNによる文章自動作成のお試しログです。
■環境
vagrant (2.0.2)上の CentOS Linux release 7.4.1708 (Core)
GPUなし
■公式サイト
■インストール
公式サイトの手順通りものすごく簡単です。
1 2 3 |
git clone https://github.com/pjreddie/darknet.git cd darknet make |
自前データでの学習を試す場合は make する前に example/rnn.cの train_char_rnn() という関数の重みファイルの出力ディレクトリを変更する必要があります。
1 |
char *backup_directory = "backupディレクトリへのパス"; |
また GPUを使用する場合は Makefileを編集するだけでOKだそうですが、今回はCPUで実行しました。
■公開されいている学習済みの重みファイルを使用してみる
rnnを実行するコマンドは下記の通り
1 |
./darknet rnn generate cfg/rnn.cfg <weights> |
<weights>の部分に学習済みの重みファイルを指定すると、その重みに基づいた文章を生成してくれます。オプションで作成する文章のlengthやseedを設定できるようです。
試しに公式サイトで公開されている grrm.weights という重みファイルを使って、”Beautiful”というseedを与えてみた結果はこのようになりました。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
$ ./darknet rnn generate cfg/rnn.cfg grrm.weights -srand 0 -seed Beautiful rnn layer filters size input output 0 RNN Layer: 256 inputs, 1024 outputs connected 256 -> 1024 connected 1024 -> 1024 connected 1024 -> 1024 Unused field: 'hidden = 1024' 1 RNN Layer: 1024 inputs, 1024 outputs connected 1024 -> 1024 connected 1024 -> 1024 connected 1024 -> 1024 Unused field: 'hidden = 1024' 2 RNN Layer: 1024 inputs, 1024 outputs connected 1024 -> 1024 connected 1024 -> 1024 connected 1024 -> 1024 Unused field: 'hidden = 1024' 3 connected 1024 -> 256 4 softmax 256 5 cost 256 Loading weights from weightFiles/grrm.weights...Done! Beautiful. She made him left and his sister the Spider of the Reins. He made their way back to the Red Keep, the priest should die a man work. He could not seem trust afar. "A gill there on three." "They were the only coin to send a cloak of onions and crawling into the stars." The long striped stag sword's sons growing with innocent. If he could not see the crown of scarlet shoulders to drive the succession to an errivel, the rangers were called as they brought too here. The knight was soar to receive the deck, but he did, but he was slain to him, and the sound of long leagues were being watched the captain's striped silk nodded apart and distant had risen and locked the castle in the woods. He rises like birds. One sister to keep them through the middles. If any words were good, and a pity for his daughter and his brothers was a good family. In the night is dying. This one should want to kill him with you." "I want to have of me, you say?" He knocked off his cheeks and a poleboat. "He sel |
※この重みファイルの元となったジョージ・R・R・マーティンさんについて
https://ja.wikipedia.org/wiki/%E3%82%B8%E3%83%A7%E3%83%BC%E3%82%B8%E3%83%BBR%E3%83%BBR%E3%83%BB%E3%83%9E%E3%83%BC%E3%83%86%E3%82%A3%E3%83%B3
■学習を試してみる
公式サイトで紹介されているシェイクスピアのコーパスファイルをダウンロードして学習してみます。
学習する時のコマンドは下記のような形式になります。
1 |
./darknet rnn train cfg/rnn.train.cfg -file /vagrant/t8.shakespeare.txt |
今回試した環境では、rnn.train.cfgをデフォルト設定のまま実行するとメモリ枯渇でsegmentation faultになってしまいましたので、今回はとりあえず動作を見るだけという事にしてコンフィグの設定をデフォルト状態から変更しました。
batch=20
time_steps=3
[rnn]で設定されている各階層の入出力を512
に設定しました。
実行すると途中経過が表示されていきます。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
rnn layer filters size input output 0 RNN Layer: 256 inputs, 512 outputs connected 256 -> 512 connected 512 -> 512 connected 512 -> 512 Unused field: 'hidden = 512' 1 RNN Layer: 512 inputs, 512 outputs connected 512 -> 512 connected 512 -> 512 connected 512 -> 512 Unused field: 'hidden = 512' 2 RNN Layer: 512 inputs, 512 outputs connected 512 -> 512 connected 512 -> 512 connected 512 -> 512 Unused field: 'hidden = 512' 3 connected 512 -> 256 4 softmax 256 5 cost 256 Learning Rate: 0.1, Momentum: 0.9, Decay: 0.001, Inputs: 256 60 3 1: 3.333992, 3.333992 avg, 0.100000 rate, 0.140000 seconds, 0.000011 epochs 2: 3.264474, 3.327041 avg, 0.100000 rate, 0.120000 seconds, 0.000022 epochs 3: 2.706679, 3.265004 avg, 0.100000 rate, 0.130000 seconds, 0.000033 epochs 4: 3.526642, 3.291168 avg, 0.100000 rate, 0.130000 seconds, 0.000044 epochs 5: 2.738644, 3.235916 avg, 0.100000 rate, 0.120000 seconds, 0.000055 epochs 6: 2.940809, 3.206405 avg, 0.100000 rate, 0.120000 seconds, 0.000066 epochs 7: 2.772719, 3.163037 avg, 0.100000 rate, 0.130000 seconds, 0.000077 epochs 8: 2.967007, 3.143434 avg, 0.100000 rate, 0.120000 seconds, 0.000088 epochs 9: 3.021849, 3.131275 avg, 0.100000 rate, 0.130000 seconds, 0.000099 epochs 10: 3.218193, 3.139967 avg, 0.100000 rate, 0.130000 seconds, 0.000110 epochs 11: 3.027466, 3.128717 avg, 0.100000 rate, 0.130000 seconds, 0.000121 epochs 12: 2.768064, 3.092652 avg, 0.100000 rate, 0.130000 seconds, 0.000132 epochs 13: 3.422880, 3.125674 avg, 0.100000 rate, 0.120000 seconds, 0.000143 epochs 14: 3.060544, 3.119162 avg, 0.100000 rate, 0.130000 seconds, 0.000154 epochs 15: 4.030269, 3.210272 avg, 0.100000 rate, 0.130000 seconds, 0.000165 epochs 16: 3.136480, 3.202893 avg, 0.100000 rate, 0.120000 seconds, 0.000176 epochs |
途中で出力される重みファイルのバックアップを使用して文章を生成してみると以下のようになりました。( rnn.cfgのrnnの各層の入出力を学習時の設定と同じにしてから実行します。)
seedは “It” です。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
$ ./darknet rnn generate cfg/rnn.cfg /vagrant/backup/rnn.backup -srand 0 -seed It rnn layer filters size input output 0 RNN Layer: 256 inputs, 512 outputs connected 256 -> 512 connected 512 -> 512 connected 512 -> 512 Unused field: 'hidden = 512' 1 RNN Layer: 512 inputs, 512 outputs connected 512 -> 512 connected 512 -> 512 connected 512 -> 512 Unused field: 'hidden = 512' 2 RNN Layer: 512 inputs, 512 outputs connected 512 -> 512 connected 512 -> 512 connected 512 -> 512 Unused field: 'hidden = 512' 3 connected 512 -> 256 4 softmax 256 5 cost 256 Loading weights from /vagrant/backup/rnn.backup...Done! It tit teederts ine int liol and wishereallst ein you and eerldald, rot ase theod oue Bo'dand sove, aloud o houdmondom, hing. Ind toot noom; a and tous wey nout, Vaadd leen; .undde hor are you ind asdle as ss I wo lrot warse Uxouth isired noo end toou. And, hind ast foot and and et hit a whe ind a ere lot ll. too suy unt of ad a re tordt ryom t asert. ou .aLSIDK. wirlledydetosiane ' we nak enguncun of of red. of rathe hald hendloo toatheear oot lourdsere SAldle sot and sis sme oulld saant, myoud And ofer aithen iiastaten beam re 'nd theme aipl sanget for youtyee tont e rund witheade, one ond a the and ' heem weat nou prow ond a'dr ind of His fit ene a tel; the No. xrord. und ofaren aell dith iterye it me ond eleths and toot serf endl, louknof'dod and toor soat a marg lat Trot ofll nov Be tind tase nouthe |
少し学習しただけなので文章どころか英単語にすらなってない箇所の方が多い状態ですが、学習が進めば英語を生成するようになる、のでしょうね・・・。