Apitore blog

Apitoreを運営していた元起業家のブログ

deeplearning4jの極性判定でオンライン学習をやってみた

はじめに

今回はdeeplearning4jを使ったオンライン学習を試してみます。オンライン学習の定義は諸説あるみたいですが、私はパーセプトロン時代の定義に則って「定期的にデータをくべて現在のモデルを逐次更新する」としています。今回は、これまで作ったDistant Supervisionのモデルに極性辞書のデータをオンライン学習します。 amarec (20161112-011908)

デモサイト

ソースコード

オンライン学習

一言で言うと、モデルを読み込んでデータをくべる、それだけです。

public static void main (final String[] args) throws Exception {
  if (args[0]==null || args[1]==null || args[2]==null || args[3]==null)
    System.exit(1);
  WordVectors wvec = WordVectorSerializer.loadTxtVectors(new File(args[0]));
  MultiLayerNetwork model = ModelSerializer.restoreMultiLayerNetwork(args[1],true);
  int batchSize   = 16;
  int testBatch   = 64;
  int nEpochs     = 1;
  System.out.println("Starting online training");
  DataSetIterator train = new AsyncDataSetIterator(
      new SentimentRecurrentIterator(args[2],wvec,batchSize,300,true),2);
  DataSetIterator test = new AsyncDataSetIterator(
      new SentimentRecurrentIterator(args[2],wvec,testBatch,300,false),2);
  for( int i=0; i<nEpochs; i++ ){
    model.fit(train);
    train.reset();
    System.out.println("Epoch " + i + " complete. Starting evaluation:");
    Evaluation evaluation = new Evaluation();
    while(test.hasNext()) {
      DataSet t = test.next();
      INDArray features = t.getFeatures();
      INDArray lables = t.getLabels();
      INDArray inMask = t.getFeaturesMaskArray();
      INDArray outMask = t.getLabelsMaskArray();
      INDArray predicted = model.output(features,false,inMask,outMask);
      evaluation.evalTimeSeries(lables,predicted,outMask);
    }
    test.reset();
    System.out.println(evaluation.stats());
    System.out.println("Save model");
    ModelSerializer.writeModel(model, new FileOutputStream(args[3]), true);
  }
}

実験

事前準備・データ

学習データも増強しました。例のごとくDistant Supervisionです。10万ツイートを超えたので、ここらで一旦Distant Supervision方式の学習データは打ち止めします。 学習データ

positive negative
16,323 121,816

極性辞書は東北大学の乾・岡崎研究室が公開しているものを使いました。本ブログの最後に引用しておきます。全部の辞書データを使ったわけではなくて、これまでの記事で解説したとおり私の極性判定はWord2Vecで単語をベクターにしますので、Word2Vecに登録されていない単語は除外しました。あと、辞書データは基本的には形態素ですが、私の極性判定にあわせるために、kuromoji+Neologdで形態素解析をしなおしておきます。最終的にオンライン学習に使ったデータは以下のようになりました。今回もニュートラルは対象外です。 辞書データ

positive negative
5,215 7,505

評価データは、私が空いた時間にコツコツとSocial Sentimentでアノテーションしたデータを使います。こちらは現状の極性判定が判定を間違えたものをアノテーションしているので、現状手法にとっては非常に難しい評価データになっています。たまに私が間違えて現状手法と同じ感情判定結果をアノテーションしてしまっているので、このあとの評価セクションでは現行手法の性能はゼロになりません。ちなみに、評価データは以下のようになりました。数が少ないですが、これから増やしていきます。 評価データ

positive negative
607 76

評価

まずはバニラです。辞書データをオンライン学習させる以前は、以下のような結果になりました。ポジティブを間違えやすいようです。これは私の作成した評価データを見てもらっても明らかです。

Examples labeled as 0 classified by model as 0: 123 times
Examples labeled as 0 classified by model as 1: 484 times
Examples labeled as 1 classified by model as 0: 20 times
Examples labeled as 1 classified by model as 1: 56 times
Accuracy:  0.2621
Precision: 0.4819
Recall:    0.4697
F1 Score:  0.4758

つづいて、辞書データをオンライン学習させた場合。全体的に性能が向上していますね。結果を見るに、単語のポジティブさやネガティブさを機械が理解しはじめていますね。

Examples labeled as 0 classified by model as 0: 303 times
Examples labeled as 0 classified by model as 1: 304 times
Examples labeled as 1 classified by model as 0: 32 times
Examples labeled as 1 classified by model as 1: 44 times
Accuracy:  0.5081
Precision: 0.5155
Recall:    0.5391
F1 Score:  0.527

少しズルした結果も見てみましょう。辞書データに加えて評価データもオンライン学習します。結果を見るに、性能が一番良いですね。学習と評価に同じデータを使っているので、これは当たり前です。

Examples labeled as 0 classified by model as 0: 449 times
Examples labeled as 0 classified by model as 1: 158 times
Examples labeled as 1 classified by model as 0: 23 times
Examples labeled as 1 classified by model as 1: 53 times
Accuracy:  0.735
Precision: 0.6012
Recall:    0.7185
F1 Score:  0.6547

おわりに

オンライン学習を試してみました。極性辞書でオンライン学習することで、ポジティブな単語とネガティブな単語を多少強調する効果が出たかなという印象です。ちなみに従来研究だと極性辞書は学習素性として組み込む(例 ネガティブな単語があるか否か、ポジティブな単語があるか否か、など)のが一般的です。 今回のズル版と呼んでいるものはApitoreのWebAPIの最新版としてアップデートしておきます。よろしければ使って、感想を下さい。また極性判定結果の修正も募集しています。あわせてよろしくお願い致します。

関連情報

極性辞書(レファレンス)

  1. 小林のぞみ,乾健太郎,松本裕治,立石健二,福島俊一. 意見抽出のための評価表現の収集. 自然言語処理,Vol.12, No.3, pp.203-222, 2005. / Nozomi Kobayashi, Kentaro Inui, Yuji Matsumoto, Kenji Tateishi. Collecting Evaluative Expressions for Opinion Extraction, Journal of Natural Language Processing 12(3), 203-222, 2005.
  2. 東山昌彦, 乾健太郎, 松本裕治, 述語の選択選好性に着目した名詞評価極性の獲得, 言語処理学会第14回年次大会論文集, pp.584-587, 2008. / Masahiko Higashiyama, Kentaro Inui, Yuji Matsumoto. Learning Sentiment of Nouns from Selectional Preferences of Verbs and Adjectives, Proceedings of the 14th Annual Meeting of the Association for Natural Language Processing, pp.584-587, 2008.