Пример #1
0
        /// <summary>
        /// Train the specified dataSet and priors for the specified number of iterations.
        /// </summary>
        /// <param name="dataSet">Data set.</param>
        /// <param name="priors">Priors.</param>
        /// <param name="numberOfIterations">Number of iterations.</param>
        public Marginals Train(DataSet dataSet, Marginals priors, int numberOfIterations = 10)
        {
            SetObservedVariables(
                dataSet.Features,
                DistributionArrayHelpers.Copy(priors.WeightMeans),
                DistributionArrayHelpers.Copy(priors.WeightPrecisions),
                dataSet.Labels);

#if !USE_PRECOMPILED_ALGORITHM
            engine.Algorithm.DefaultNumberOfIterations = numberOfIterations;
            var posteriorWeights          = engine.Infer <Gaussian[][]>(weights);
            var posteriorWeightMeans      = engine.Infer <Gaussian[]>(weightMeans);
            var posteriorWeightPrecisions = engine.Infer <Gamma[]>(weightPrecisions);
#else
            algorithm.Execute(numberOfIterations);
            var posteriorWeights          = algorithm.Marginal <Gaussian[][]>(weights.Name);
            var posteriorWeightMeans      = algorithm.Marginal <Gaussian[]>(weightMeans.Name);
            var posteriorWeightPrecisions = algorithm.Marginal <Gamma[]>(weightPrecisions.Name);
#endif

            return(new Marginals {
                Weights = posteriorWeights, WeightMeans = posteriorWeightMeans, WeightPrecisions = posteriorWeightPrecisions
            });
        }
Пример #2
0
        /// <summary>
        /// Infers the marginal distribution for the specified variable.
        /// </summary>
        /// <typeparam name="TReturn">Desired return type which may be a distribution type or an array type if the argument is a VariableArray</typeparam>
        /// <param name="var">The variable whose marginal is to be inferred</param>
        /// <returns>The marginal distribution (or an approximation to it)</returns>
        public TReturn Infer <TReturn>(IVariable var)
        {
            IGeneratedAlgorithm ca = InferAll(false, var);

            return(ca.Marginal <TReturn>(var.Name));
        }
Пример #3
0
        /// <summary>
        /// Infers the marginal distribution for the specified variable.
        /// </summary>
        /// <param name="var">The variable whose marginal is to be inferred</param>
        /// <returns>The marginal distribution (or an approximation to it)</returns>
        public object Infer(IVariable var)
        {
            IGeneratedAlgorithm ca = InferAll(false, var);

            return(ca.Marginal(var.Name));
        }