为什么OpenNLP的Document Categorizer训练这么快?

时间:2019-07-21 22:31:58

标签: nlp opennlp

OpenNLP的性能明显不如我测试过的其他文档分类器,因此在放弃它之前,我决定确保我将使用所有的拨盘和旋钮。对我而言突出的一件事是OpenNLP正在非常快速地训练我的模型(大约1.2秒)。我使用的其他NLP工具可能要花几分钟,甚至要花几个小时来训练。我的训练档案中有大约12000条记录。

不幸的是,我尝试将迭代次数从10增加到10000,这似乎对训练时间或准确性没有任何影响。

奇怪的是,OpenNLP的文档对培训时间进行了说明:“现在可能是乘船前往Hulu或其他地方的好时机,因为如果您有大量的培训,这可能需要一段时间。”这让我觉得自己做错了。

            int TRAINING_ITERATIONS = 10000;

            InputStreamFactory dataIn = new MarkableFileInputStreamFactory(new File(dataSetFileName));
            ObjectStream<String> lineStream = new PlainTextByLineStream(dataIn, "UTF-8");
            ObjectStream<DocumentSample> sampleStream = new DocumentSampleStream(lineStream);

            // define the training parameters
            TrainingParameters params = new TrainingParameters();
            params.put(TrainingParameters.ITERATIONS_PARAM, TRAINING_ITERATIONS+"");
            params.put(TrainingParameters.CUTOFF_PARAM, 0+"");
            params.put(AbstractTrainer.ALGORITHM_PARAM, NaiveBayesTrainer.NAIVE_BAYES_VALUE);

            FeatureGenerator[] featureGenerators = { new NGramFeatureGenerator(1,1),
                    new NGramFeatureGenerator(2,3) };
            DoccatFactory factory = new DoccatFactory(featureGenerators);


            // create a model from training data
            StopWatch stopWatch = new StopWatch();

            // Start the watch, do some task and stop the watch.
            stopWatch.start();


            model = DocumentCategorizerME.train("en", sampleStream, params, factory);
            stopWatch.stop();
            System.out.println("Training Time: " + stopWatch.getTime()+"ms"); // finishes in 1.2 second!!! 

这是我得到的输出

Indexing events with TwoPass using cutoff of 0

    Computing event counts...  done. 2407 events
    Indexing...  done.
Collecting events... Done indexing in 0.84 s.
Incorporating indexed data for training...  
done.
    Number of Event Tokens: 2407
        Number of Outcomes: 12
      Number of Predicates: 44219
Computing model parameters...
Stats: (455/2407) 0.18903199002908183
...done.
Training Time: 1241ms

迭代参数是否有作用?

1 个答案:

答案 0 :(得分:0)

好吧,我知道了,朴素贝叶斯只是一种非常快速的算法,因为它没有优化步骤。迭代参数并没有真正做很多事情。如果您使用随附的任何其他训练算法,则确实需要花费很多时间进行训练。出乎意料的是,即使我训练了几个小时,对于我的数据集,其他算法也导致准确性大大降低。

相关问题