[機械学習]
RNNで文章要約
手法
fast_abs_rlが目当ての手法、seq2seqで文章要約をやっている。
fast_abs_rl
- Fast Abstractive Summarization with Reinforce-Selected Sentence Rewriting
- 生成型要約のために、最初に文を抽出しそれを書き直す形で要約を生成
- ソースコード:https://github.com/ChenRocks/fast_abs_rl
DeepMind-Teaching-Machines-to-Read-and-Comprehend
- Teaching Machines to Read and Comprehend、上記の論文に引用されている。データセットも一緒。
- INPUT: 文章と要約のデータセット(教師データ)
- OUTPUT: 文脈と穴あき問題が与えられたとき答えられるモデル
- ソースコード: https://github.com/thomasmesnard/DeepMind-Teaching-Machines-to-Read-and-Comprehend
実践
- fast_abs_rlのトレーニング済みモデルを動かす
Decode summaries from the pretrained model
事前学習済みモデルから要約をデコードする
Download the pretrained models here. You will also need a preprocessed version of the CNN/DailyMail dataset. Please follow the instructions here for downloading and preprocessing the CNN/DailyMail dataset. After that, specify the path of data files by setting the environment variable export DATA=[path/to/decompressed/data]
こちらから事前トレーニング済みモデルをダウンロードします。 また、CNN / DailyMailデータセットの前処理済みバージョンも必要です。 CNN / DailyMailデータセットをダウンロードして前処理するには、こちらの手順に従ってください。 その後、環境変数export DATA = [path / to / decompressed / data]を設定して、データファイルのパスを指定します。
We provide 2 versions of pretrained models. Using acl you can reproduce the results reported in our paper. Using new you will get our latest result trained with a newer version of PyTorch library which leads to slightly higher scores.
事前トレーニング済みモデルの2つのバージョンを提供しています。 aclを使用すると、私たちの論文で報告された結果を再現できます。 newを使用すると、PyTorchライブラリの新しいバージョンでトレーニングされた最新の結果が得られ、スコアがわずかに高くなります。
To decode, run
デコードするには、以下のように(ここからは独自の手順)
データセットの準備
- https://github.com/ChenRocks/cnn-dailymail に従う
- storiesファイルのDL
- https://cs.nyu.edu/~kcho/DMQA/ からCNN,DailyMailのデータセット(stories部分のみをDLしておく)
- Stanford Core NLP v3.7.0 をDLしてプロジェクト配下に展開
- https://stanfordnlp.github.io/CoreNLP/history.html に今までのリリースが保存されている
- storiesファイルを適当なところに配置して実行
- storiesファイルのDL
$ CLASSPATH=stanford-corenlp-full-2016-10-31/stanford-corenlp-3.7.0.jar \ // jarの実体 python make_datafiles.py \ /Users/hiroyuki/dataset/stories/cnn/stories/ \ // cnnのstoriesディレクトリ /Users/hiroyuki/dataset/stories/dailymail/stories/ // dailymailのstoriesディレクトリ
- 実行には2時間ぐらいかかる
- その後、finished_files 以下に test.tar, val.tar, train.tarができるので、それを解凍して fast_abs_rl の実行時に使う
fast_abs_rl の準備
- 事前にAnaconda等でPython3.6の環境を作り、pipもpython3.6で動くようにしておく
- WEBだとpyenv使う人多いのだが、機械学習はAnacondaの方がいいのかもしれない(郷に入っては郷に従う感じで)
// fast_abs_rlへ移動 $ cd fast_abs_rl // pytorchの依存が適当だったのでちょい修正 $ cat requirements.txt torch==1.2.0 gensim tensorboardX cytoolz pyrouge // 依存関係のライブラリをDL $ pip install -r requirements.txt // プロジェクトルートに事前トレーニング済みモデルを展開 $ mkdir pretrained $ tar xvf ~/Downloads/pretrained.tar.gz -C ./pretrained/ // JSON形式になったデータセットを展開 $ mkdir decompressed $ tar xvf test.tar -C decompressed $ tar xvf val.tar -C decompressed $ tar xvf train.tar -C decompressed
- CPUしかない環境の設定を入れる
- オプション切り替えがあるのだが、ソースコード側が対応できていない
- 修正PRset map location to 'cpu' if cuda is false #62
--- a/decoding.py +++ b/decoding.py @@ -48,7 +48,7 @@ def load_best_ckpt(model_dir, reverse=False): key=lambda c: float(c.split('-')[1]), reverse=reverse) print('loading checkpoint {}...'.format(ckpts[0])) ckpt = torch.load( - join(model_dir, 'ckpt/{}'.format(ckpts[0])) + join(model_dir, 'ckpt/{}'.format(ckpts[0])), map_location='cpu' )['state_dict'] return ckpt
fast_abs_rl の準備
// 実行コマンド, Pycharmなどに覚えさせたほうがいい気がする $ DATA=decompressed \ python decode_full_model.py \ --path=output \ --model_dir=./pretrained/pretrained/new \ --beam=1
出力された結果を元記事と比較
元記事
+ | 9b040dde80ca3388e9ce9512bb270c2a9e72f1de.story |
出力された要約
- 色々試してみて
- good
- 普通の英文ならば普通に要約された文章が出てくる
- 二重引用符等はノイズになるので、INPUTとして渡す前に事前にエスケープしといた方がいい
- bad
- 思ったより言い換え表現(paraphrase)ができてないように見える
- 出力された要約の長さを調節できるのかどうかわからなかった
- good
a man in suburban boston is selling snow online to customers in warmer states . for $ 89 , he will ship 6 pounds of snow in an insulated styrofoam box .