Beispiel #1
0
        /// <inheritdoc />
        public override ModelPosteriors InferPosteriors(
            int[][] workerLabel,
            int[][] workerJudgedTweetIndex,
            int[][] words    = null,
            int[] wordCounts = null,
            int[] newWorkerToOldWorkerMap = null,
            int[] newWordToOldWordMap     = null,
            int?[] goldLabels             = null,
            ModelPosteriors oldPosteriors = null,
            int numIterations             = 20)
        {
            this.ObserveLabels(workerLabel, workerJudgedTweetIndex, goldLabels);
            this.ObserveWords(words, wordCounts);
            if (newWorkerToOldWorkerMap == null || oldPosteriors == null)
            {
                this.SetDefaultPriors();
            }
            else
            {
                this.SetPriorsFromPosteriors(newWorkerToOldWorkerMap, newWordToOldWordMap, oldPosteriors);
            }

            // Initialize messages
            var discreteUniform = Discrete.Uniform(this.NumberOfCommunities);

            this.WorkerCommunityInitializer.ObservedValue = Distribution <int> .Array(Util.ArrayInit(workerLabel.Length, w => Discrete.PointMass(discreteUniform.Sample(), this.NumberOfCommunities)));

            var posteriors = new BiasedCommunityWordsPosteriors();
            var evidences  = new List <double>();

            try
            {
                for (var it = 1; it <= numIterations; it++)
                {
                    this.Engine.NumberOfIterations = it;
                    posteriors.TrueLabel           = this.Engine.Infer <Discrete[]>(this.TrueLabel);
                    posteriors.CommunityCpt        = this.Engine.Infer <Dirichlet[][]>(this.ProbWorkerLabel);
                    posteriors.WorkerCommunities   = this.Engine.Infer <Discrete[]>(this.Community);
                    posteriors.BackgroundLabelProb = this.Engine.Infer <Dirichlet>(this.ProbLabel);
                    posteriors.ProbWordPosterior   = this.Engine.Infer <Dirichlet[]>(this.ProbWords);
                    if (this.HasEvidence)
                    {
                        posteriors.Evidence = this.Engine.Infer <Bernoulli>(this.Evidence);
                        Console.WriteLine($"Iteration {it} log evidence:\t{posteriors.Evidence.LogOdds:0.0000}");
                        evidences.Add(posteriors.Evidence.LogOdds);
                        if (ModelBase.HasConverged(evidences))
                        {
                            break;
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }

            return(posteriors);
        }
        /// <inheritdoc />
        public override ModelPosteriors InferPosteriors(
            int[][] workerLabel,
            int[][] workerJudgedTweetIndex,
            int[][] words    = null,
            int[] wordCounts = null,
            int[] newWorkerToOldWorkerMap = null,
            int[] newWordToOldWordMap     = null,
            int?[] goldLabels             = null,
            ModelPosteriors oldPosteriors = null,
            int numIterations             = 20)
        {
            this.ObserveLabels(workerLabel, workerJudgedTweetIndex, goldLabels);
            if (newWorkerToOldWorkerMap == null || oldPosteriors == null)
            {
                this.SetDefaultPriors();
            }
            else
            {
                this.SetPriorsFromPosteriors(newWorkerToOldWorkerMap, newWordToOldWordMap, oldPosteriors);
            }

            var posteriors = new HonestWorkerModelPosteriors();
            var evidences  = new List <double>();

            for (var it = 1; it <= numIterations; it++)
            {
                this.Engine.NumberOfIterations    = it;
                posteriors.TrueLabel              = this.Engine.Infer <Discrete[]>(this.TrueLabel);
                posteriors.BackgroundLabelProb    = this.Engine.Infer <Dirichlet>(this.ProbLabel);
                posteriors.RandomGuessProbability = this.Engine.Infer <Dirichlet>(this.RandomGuessProbability);
                posteriors.WorkerAbility          = this.Engine.Infer <Beta[]>(this.Ability);
                if (this.HasEvidence)
                {
                    posteriors.Evidence = this.Engine.Infer <Bernoulli>(this.Evidence);
                    Console.WriteLine($"Iteration {it} log evidence:\t{posteriors.Evidence.LogOdds:0.0000}");
                    evidences.Add(posteriors.Evidence.LogOdds);
                    if (ModelBase.HasConverged(evidences))
                    {
                        break;
                    }
                }
            }

            return(posteriors);
        }