Beispiel #1
0
        private void LearnClick5LabelModel(
            int numLabels,
            bool learnScoreMean,
            bool learnScorePrec,
            bool learnJudgePrec,
            bool learnClickPrec,
            bool learnThresholds,
            double nominalScoreMean,
            double nominalScorePrec,
            double nominalJudgePrec,
            double nominalClickPrec,
            int[] labels,
            int[] clicks,
            int[] exams,
            int chunkSize,
            int nPasses,
            bool printToConsole,
            out Gaussian margScoreMean,
            out Gamma margScorePrec,
            out Gamma margJudgePrec,
            out Gamma margClickPrec,
            out Gaussian[] margThresh)
        {
            InferenceEngine engine = new InferenceEngine();
            int             numThresholds = numLabels + 1; // Includes end-points

            //-------------------------------------------------------------
            // Priors
            //-------------------------------------------------------------
            Gaussian priorScoreMean = Gaussian.FromMeanAndVariance(nominalScoreMean, learnScoreMean ? 1 : 0);
            Gamma    priorScorePrec = Gamma.FromMeanAndVariance(nominalScorePrec, learnScorePrec ? 1 : 0);
            Gamma    priorJudgePrec = Gamma.FromMeanAndVariance(nominalJudgePrec, learnJudgePrec ? 1 : 0);
            Gamma    priorClickPrec = Gamma.FromMeanAndVariance(nominalClickPrec, learnClickPrec ? 1 : 0);

            Gaussian[] priorThreshMean;
            CalculatePriors(learnThresholds, numLabels, out priorThreshMean);

            //------------------------------------------------------
            // Observations
            //------------------------------------------------------
            Gaussian[][][] clickObs = getClickObservations(numLabels, chunkSize, labels, clicks, exams);
            int            numChunks = clickObs.Length;

            //-----------------------------------------------------
            // Create an array of batch variables
            //-----------------------------------------------------
            Model model = new Model(numChunks);
            SharedVariable <double, Gaussian> scoreMean = (SharedVariable <double, Gaussian>) SharedVariable <double> .Random(priorScoreMean).Named("scoreMean");

            SharedVariable <double, Gamma> scorePrec = (SharedVariable <double, Gamma>) SharedVariable <double> .Random(priorScorePrec).Named("scorePrec");

            SharedVariable <double, Gamma> judgePrec = (SharedVariable <double, Gamma>) SharedVariable <double> .Random(priorJudgePrec).Named("judgePrec");

            SharedVariable <double, Gamma> clickPrec = (SharedVariable <double, Gamma>) SharedVariable <double> .Random(priorClickPrec).Named("clickPrec");

            SharedVariable <double, Gaussian>[] threshMeans = new SharedVariable <double, Gaussian> [numThresholds];
            for (int t = 0; t < numThresholds; t++)
            {
                threshMeans[t] = (SharedVariable <double, Gaussian>) SharedVariable <double, Gaussian> .Random(priorThreshMean[t]).Named("threshMeans" + t);
            }

            engine.Compiler.DeclarationProvider = Microsoft.ML.Probabilistic.Compiler.RoslynDeclarationProvider.Instance;
            var info = GetType().GetMethod("Click5LabelModel", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
            var ca   = engine.Compiler.CompileWithoutParams(info);

            //----------------------------------------------------------
            // Outer loop iterates over a number of passes
            // Inner loop iterates over the unique labels
            //----------------------------------------------------------
            for (int pass = 0; pass < nPasses; pass++)
            {
                for (int c = 0; c < numChunks; c++)
                {
                    Gaussian[] threshInputs = new Gaussian[threshMeans.Length];
                    for (int i = 0; i < threshInputs.Length; i++)
                    {
                        threshInputs[i] = ((SharedVariable <double, Gaussian>)threshMeans[i]).MessageToBatch(model, c);
                    }
                    ca.SetObservedValue("inputScoreMean", scoreMean.MessageToBatch(model, c));
                    ca.SetObservedValue("inputScorePrec", scorePrec.MessageToBatch(model, c));
                    ca.SetObservedValue("inputJudgePrec", judgePrec.MessageToBatch(model, c));
                    ca.SetObservedValue("inputClickPrec", clickPrec.MessageToBatch(model, c));
                    ca.SetObservedValue("inputThresh", threshInputs);
                    ca.SetObservedValue("clickObservations", clickObs[c]);
                    ca.Execute(10);

                    // Retrieve the output messages
                    model.InferShared(ca, c);

                    if (printToConsole)
                    {
                        Console.WriteLine("****** Pass {0}, chunk {1} ******", pass, c);
                        Console.WriteLine("----- Marginals -----");
                        Console.WriteLine("scoreMean = {0}", scoreMean.Marginal <Gaussian>());
                        Console.WriteLine("scorePrec = {0}", scorePrec.Marginal <Gamma>());
                        Console.WriteLine("judgePrec = {0}", judgePrec.Marginal <Gamma>());
                        Console.WriteLine("clickPrec = {0}", clickPrec.Marginal <Gamma>());
                        for (int t = 0; t < numThresholds; t++)
                        {
                            Console.WriteLine("threshMean {0} = {1}", t, threshMeans[t].Marginal <Gaussian>());
                        }
                    }
                }
            }
            margScoreMean = scoreMean.Marginal <Gaussian>();
            margScorePrec = scorePrec.Marginal <Gamma>();
            margJudgePrec = judgePrec.Marginal <Gamma>();
            margClickPrec = clickPrec.Marginal <Gamma>();
            margThresh    = new Gaussian[numThresholds];
            for (int i = 0; i < margThresh.Length; i++)
            {
                margThresh[i] = threshMeans[i].Marginal <Gaussian>();
            }
        }