public void Update(VariationalInferenceParameter varInference, CorpusDocument document)
        {
            for (int i = 0; i < CovarianceSufficientStatistic.RowCount; i++)
            {
                MuSufficientStatistic[i] = MuSufficientStatistic[i] + varInference.Lambda[i];
                for (int j = 0; j < CovarianceSufficientStatistic.ColumnCount; j++)
                {
                    double tmp = varInference.Lambda[i] * varInference.Lambda[j];
                    if (i == j)
                    {
                        CovarianceSufficientStatistic[i, j] = CovarianceSufficientStatistic[i, j] + varInference.Nu[i] + tmp;
                    }
                    else
                    {
                        CovarianceSufficientStatistic[i, j] = CovarianceSufficientStatistic[i, j] + tmp;
                    }
                }
            }

            for (int i = 0; i < document.NbTerms; i++)
            {
                for (int j = 0; j < BetaSufficientStatistic.RowCount; j++)
                {
                    var word  = document.GetWord(i);
                    var count = document.GetCount(i);
                    BetaSufficientStatistic[j, word] = BetaSufficientStatistic[j, word] + count * varInference.Phi[i, j];
                }
            }

            NData++;
        }
        public void OptimizePhi(LLNAModel model, CorpusDocument doc)
        {
            // Compute phi proportion in log space.
            for (int n = 0; n < doc.NbTerms; n++)
            {
                double logSumN = 0;
                for (int i = 0; i < model.K; i++)
                {
                    var word = doc.GetWord(n);
                    LogPhi[n, i] = Lambda[i] + model.LogBeta[i, word];
                    if (i == 0)
                    {
                        logSumN = LogPhi[n, i];
                    }
                    else
                    {
                        logSumN = MathHelper.LogSum(logSumN, LogPhi[n, i]);
                    }
                }

                for (int i = 0; i < model.K; i++)
                {
                    LogPhi[n, i] = LogPhi[n, i] - logSumN;
                    Phi[n, i]    = Math.Exp(LogPhi[n, i]);
                }
            }
        }
        public void OptimizeNu(LLNAModel model, CorpusDocument doc)
        {
            for (int i = 0; i < model.K - 1; i++)
            {
                double initNu = 10;
                double nuI = 0, logNuI = 0, df = 0, d2f = 0;
                int    iter = 0;
                logNuI = Math.Log(initNu);
                do
                {
                    iter++;
                    nuI = Math.Exp(logNuI);
                    if (double.IsNaN(nuI))
                    {
                        initNu = initNu * 2;
                        logNuI = Math.Log(initNu);
                        nuI    = initNu;
                    }

                    df = -(model.InvCovariance[i, i] * 0.5)
                         - (0.5 * ((doc.Total / Zeta) * Math.Exp(Lambda[i] + nuI / 2)))
                         + (0.5 * (1 / nuI));
                    d2f = -(0.25 * (doc.Total / Zeta) * Math.Exp(Lambda[i] + nuI / 2))
                          - (0.5 * (1 / (nuI * nuI)));
                    logNuI = logNuI - (df * nuI) / (d2f * nuI * nuI + df * nuI);
                }while (Math.Abs(df) > NEWTON_THRESH);

                Nu[i] = Math.Exp(logNuI);
            }
        }
Esempio n. 4
0
        public Bundle(int k, CorpusDocument document, LLNAModel model, VariationalInferenceParameter inference)
        {
            SumPhi = Vector <double> .Build.Dense(k - 1, 0);

            Document     = document;
            Model        = model;
            VarInference = inference;
            Init(k);
        }
        public void OptimizeLambda(LLNAModel model, CorpusDocument doc)
        {
            int    i = 0, n = model.K - 1, iter = 0;
            double fOld   = 0;
            var    bundle = new Bundle(model.K, doc, model, this);
            var    x      = Vector <double> .Build.Dense(model.K - 1);

            for (i = 0; i < model.K - 1; i++)
            {
                x[i] = Lambda[i];
            }

            /*
             * var iterator = new MultidimensionalIterator(n);
             * var fdf = new MultidimensionalFunctionFDF
             * {
             *  F = (vector) =>
             *  {
             *      return ComputeFunction(vector, bundle);
             *  },
             *  DF = (vector) =>
             *  {
             *      return ComputeGradient(vector, bundle);
             *  },
             *  N = n
             * };
             * var multidimensionalMinimizer = new MultidimensionalMinimizer(n, iterator, fdf);
             * multidimensionalMinimizer.Set(x, 0.01, 1e-3);
             * do
             * {
             *  iter++;
             *  fOld = multidimensionalMinimizer.F;
             *  multidimensionalMinimizer.Iterate();
             * }
             * while (true);
             */

            var obj = ObjectiveFunction.Gradient((vector) => {
                return(ComputeFunction(vector, bundle));
            }, (vector) =>
            {
                return(ComputeGradient(vector, bundle));
            });
            var solver = new ConjugateGradientMinimizer(1e-8, 5000);
            var result = solver.FindMinimum(obj, x);

            for (i = 0; i < model.K - 1; i++)
            {
                Lambda[i] = result.MinimizingPoint[i];
            }

            Lambda[i] = 0;
        }
        public void UpdateLikelihoodBound(CorpusDocument doc, LLNAModel model)
        {
            TopicScores.SetValue(0);
            // p(η | μ, Σ) (distribution of topic proportions) && q(η | λ, ν)
            // E[log p(\eta | \mu, \Sigma)] + H(q(\eta | \lambda, \nu)
            double lHood = 0.5 * model.LogDeterminantInvCovariance + 0.5 * (model.K - 1);

            for (int i = 0; i < (model.K - 1); i++)
            {
                double v = -0.5 * Nu[i] * model.InvCovariance[i, i];
                for (int j = 0; j < model.K - 1; j++)
                {
                    v -= 0.5 *
                         (Lambda[i] - model.Mu[i]) *
                         model.InvCovariance[i, j] *
                         (Lambda[j] - model.Mu[j]);
                }

                v     += 0.5 * Math.Log(Nu[i]);
                lHood += v;
            }

            // E[log p(z_n | \eta)] + E[log p(w_n | \beta)] + H(q(z_n | \phi_n))
            lHood -= CalculateLikelihoodBound() * doc.Total;
            for (int i = 0; i < doc.NbTerms; i++)
            {
                for (int j = 0; j < model.K; j++)
                {
                    var phi    = Phi[i, j];
                    var logPhi = LogPhi[i, j];
                    if (phi > 0)
                    {
                        TopicScores[j] += phi * doc.GetCount(i);
                        double a1 = Lambda[j];
                        int    ww = doc.GetWord(i);
                        double a2 = model.LogBeta[j, doc.GetWord(i)];
                        var    ss = doc.GetCount(i) * phi *
                                    ((Lambda[j] + model.LogBeta[j, doc.GetWord(i)]) - logPhi);
                        lHood += ss;
                    }
                }
            }

            LHood = lHood;
        }