LoginSignup
9
4

More than 5 years have passed since last update.

LSTMでカフカの「変身」を元に文章生成してみたら、何者にもなれなかった話。

Last updated at Posted at 2017-09-13

概要

文章生成はどれぐらいのデータセットだと、どれほどの精度が出るものなのか、
一度も試したこともなかったので挑戦してみました。

手法

Tensorflowで書きました。Kerasのほうが単純に書けると思います。。。
2層のLSTMを設置し、1文字後のchar/wordを教師データとして学習。
文章生成時は、予測確度1位のchar/wordを選択しています。
ゆらぎを出す場合は、予測上位からランダム選択で良いと思います。

datasetは、以下のURLからカフカの「変身」を選びました。意味は特にありません。
http://www.gutenberg.org/ebooks/5200

前処理は、本文箇所だけを抽出し、chapter表記を削除しただけです。
記号の置換とかもすれば、もっと綺麗になると思います。

コードはこちら、
https://github.com/pigooosuke/lstm_text_generator

結果

"The"から始まる文章を生成してます。

Charレベル

ボキャブラリー数: 60
char総数: 116622

The same times that I've open upingoull, and his fordit the sigh of the reases,
and no tho gontoused and he pusied hers so that he was to became use to be ard
forgove and he would save with a cramous snignto and if he wolld with hiseof ball
paysfrom a lots of his beadionabthraif asting wouldnewpatreary himself asliad for
hil", and pust ho ewairsutention with anverask so stant that spert flack as home
and he would know all the bost of what had been sudfert horrikeltsubpessitratthr ,
was a giran in mupid.- Gregor had never could be seen fuc  award efuss to bong
severy decisavely atthis way warall.

空白スペースもきちんと学習できているみたいです。
正直、意味がよくわからない。

Wordレベル

ボキャブラリー数: 4997
単語総数: 20252
↑ ボキャブラリーに対して、単語総数のバランスが悪いかも。。。

The room for her hand and she had her her came hands often her selfcontrolled, It
was very sign and the father would have to him but she had been more for her and
she had not very indifferent to him more tried to get and difficult that she would
used to get himself than he had been before. for bed Gregor had him for him to it;
he had back and it was thought that he would have to him for the time that she had
already again in this mother and could not time about this I not me to ask I
parents to the hall, that father only to the door as his mother would always put on
the key on the door with the gold slightly in the notes and day,

charレベルに比べるとまともに見えるけど、意味が読み取れない・・・
ちなみに上記をgoogle翻訳に通すと、

彼女の手のための部屋と彼女は彼女を持っていた彼女は頻繁に彼女は自制して手を来た、
それは非常に印象的であったと父は彼に持っていたが、
彼女は彼女のためだったと彼女は、以前よりも自分自身を得るために使用されていました。
ベッドのためにグレゴールは彼のために彼にそれを持っていた。
彼は戻ってきて、彼はこの母親にもう一度やっていた時間のために彼に彼がいなければならないと考えられました。
私は私が私の両親に会うように私はしませんでした。
常にメモと日に金を少し入れてドアの鍵をかける。

うーん。
カフカマニアなら、「カフカっぽい言い回しじゃん!」ってなるのでしょうか?

ごく短い単語長なら理解できますが、、、
・the father would have to
・it was thought that he would have to
・his mother would always put on the key on the door

改善事項

アイディアレベルですが、
・予測単語は特に絞っていないので、決められたボキャブラリーの中から予測する。
・予測文をランダム長より固定長にしてパディングする。
・複数候補の文を生成し、前後の文章のコサイン類似度が高いものを採用。
などでちょっとまともになるかもしれませんが、劇的な改善があるかは疑問です。
あとclassificationだと2gram-charと3gram-charとかで精度向上するケースもあるので、それをトライするのも面白いかも知れません。上記のやつは1gram-charです。

対話形式の文章生成は、よく取り上げられますけど、
単純に文章を生成するのは、大変ですね。

9
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
4