Пример #1
0
        private ExpectationResult Expectation(
            Corpus corpus,
            LLNAModel llnaModel,
            LLNASufficientStatistics llnaSufficientStatistics,
            Matrix <double> corpusLambda,
            Matrix <double> corpusNu,
            Matrix <double> corpusPhiSum,
            bool resetVar)
        {
            double avgNIter = 0, avgConverged = 0;
            double total = 0;

            for (int i = 0; i < corpus.NbDocuments; i++)
            {
                Debug.WriteLine($"Document {i}");
                var doc          = corpus.GetDocument(i);
                var varInference = new VariationalInferenceParameter(doc.NbTerms, llnaModel.K);
                if (resetVar)
                {
                    varInference.Init(llnaModel);
                }
                else
                {
                    varInference.Lambda = corpusLambda.Row(i);
                    varInference.Nu     = corpusNu.Row(i);
                    varInference.OptimizeZeta(llnaModel);
                    varInference.OptimizePhi(llnaModel, doc);
                    varInference.NIter = 0;
                }

                var lHood = Inference(varInference, doc, llnaModel);
                llnaSufficientStatistics.Update(varInference, doc);
                total    += lHood;
                avgNIter += varInference.NIter;
                if (varInference.Converged)
                {
                    avgConverged++;
                }

                corpusLambda.SetRow(i, varInference.Lambda);
                corpusNu.SetRow(i, varInference.Nu);
                var phiSum = varInference.Phi.ColumnSum();
                corpusPhiSum.SetRow(i, phiSum);
            }

            avgNIter     = avgNIter / corpus.NbDocuments;
            avgConverged = avgConverged / corpus.NbDocuments;
            return(new ExpectationResult(avgNIter, avgConverged, total));
        }