Beispiel #1
0
        /// <inheritdoc />
        public override void SetPriorsFromPosteriors(
            int[] newWorkerToOldWorkerMap,
            int[] newWordToOldWordMap,
            ModelPosteriors modelPosteriors)
        {
            base.SetPriorsFromPosteriors(newWorkerToOldWorkerMap, newWordToOldWordMap, modelPosteriors);
            var biasedCommunityWordsModelPosteriors = (BiasedCommunityWordsPosteriors)modelPosteriors;
            var probWordPosteriors = biasedCommunityWordsModelPosteriors.ProbWordPosterior;

            AssertWhenDebugging.IsTrue(this.LabelValueCount == probWordPosteriors.Length);
            this.ProbWordsPrior.ObservedValue = Util.ArrayInit(this.LabelValueCount, i => Dirichlet.Symmetric(this.VocabularySize, this.ProbWordInitialCount));

            for (var labIndex = 0; labIndex < probWordPosteriors.Length; labIndex++)
            {
                var probWordPosterior = probWordPosteriors[labIndex];
                var oldPseudoCounts   = probWordPosterior.PseudoCount;
                var newPseudoCounts   = Vector.Constant(this.VocabularySize, this.ProbWordInitialCount, oldPseudoCounts.Sparsity);
                for (var wordIndex = 0; wordIndex < newWordToOldWordMap.Length; wordIndex++)
                {
                    var oldIdx = newWordToOldWordMap[wordIndex];
                    if (oldIdx >= 0)
                    {
                        newPseudoCounts[wordIndex] = oldPseudoCounts[oldIdx];
                    }
                }

                this.ProbWordsPrior.ObservedValue[labIndex] = new Dirichlet(newPseudoCounts);
            }
        }
Beispiel #2
0
        /// <include file='FactorDocs.xml' path='factor_docs/message_op_class[@name="SubarrayOp{T}"]/message_doc[@name="LogAverageFactor{DistributionType}(IReadOnlyList{DistributionType}, IReadOnlyList{DistributionType}, IReadOnlyList{int})"]/*'/>
        /// <typeparam name="DistributionType">The type of a distribution over an array item.</typeparam>
        public static double LogAverageFactor <DistributionType>(IReadOnlyList <DistributionType> items, IReadOnlyList <DistributionType> array, IReadOnlyList <int> indices)
            where DistributionType : CanGetLogAverageOf <DistributionType>
        {
            AssertWhenDebugging.Distinct(indices);
            double z = 0.0;

            for (int i = 0; i < indices.Count; i++)
            {
                z += array[indices[i]].GetLogAverageOf(items[i]);
            }
            return(z);
        }
Beispiel #3
0
 /// <include file='FactorDocs.xml' path='factor_docs/message_op_class[@name="SubarrayOp{T}"]/message_doc[@name="ArrayAverageConditional{DistributionType, ArrayType}(IReadOnlyList{DistributionType}, IReadOnlyList{int}, ArrayType)"]/*'/>
 /// <typeparam name="DistributionType">The type of a distribution over an array item.</typeparam>
 /// <typeparam name="ArrayType">The type of the outgoing message.</typeparam>
 public static ArrayType ArrayAverageConditional <DistributionType, ArrayType>([SkipIfAllUniform] IReadOnlyList <DistributionType> items, IReadOnlyList <int> indices, ArrayType result)
     where ArrayType : IList <DistributionType>, SettableToUniform
     where DistributionType : SettableTo <DistributionType>
 {
     Assert.IsTrue(items.Count == indices.Count, "items.Count != indices.Count");
     AssertWhenDebugging.Distinct(indices);
     result.SetToUniform();
     for (int i = 0; i < indices.Count; i++)
     {
         DistributionType value = result[indices[i]];
         value.SetTo(items[i]);
         result[indices[i]] = value;
     }
     return(result);
 }
Beispiel #4
0
 /// <include file='FactorDocs.xml' path='factor_docs/message_op_class[@name="SubarrayOp{T}"]/message_doc[@name="ArrayAverageConditional{DistributionType, ArrayType}(IReadOnlyList{T}, IReadOnlyList{int}, ArrayType)"]/*'/>
 /// <typeparam name="DistributionType">The type of a distribution over an array item.</typeparam>
 /// <typeparam name="ArrayType">The type of the outgoing message.</typeparam>
 public static ArrayType ArrayAverageConditional <DistributionType, ArrayType>(IReadOnlyList <T> items, IReadOnlyList <int> indices, ArrayType result)
     where ArrayType : IList <DistributionType>, SettableToUniform
     where DistributionType : HasPoint <T>
 {
     if (items.Count != indices.Count)
     {
         throw new ArgumentException(indices.Count + " indices were given to Subarray but the output array has length " + items.Count);
     }
     AssertWhenDebugging.Distinct(indices);
     result.SetToUniform();
     for (int i = 0; i < indices.Count; i++)
     {
         DistributionType value = result[indices[i]];
         value.Point        = items[i];
         result[indices[i]] = value;
     }
     return(result);
 }