コード例 #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
ファイル: InferenceEngine.cs プロジェクト: zouwuhe/infer
        private void Execute(IGeneratedAlgorithm ca)
        {
            // If there is a message update listener, try to add in the engine to listen to messages.
            if (this.MessageUpdated != null)
            {
                DebuggingSupport.TryAddRemoveEventListenerDynamic(ca, OnMessageUpdated, add: true);
            }

            // Register the ProgressChanged handler only while doing inference within InferenceEngine.
            // We do not want the handler to run if the user accesses the GeneratedAlgorithms directly.
            ca.ProgressChanged += OnProgressChanged;
            try
            {
                Stopwatch s = null;
                if (ShowTimings)
                {
                    s = new Stopwatch();
                    s.Start();
                    FileStats.Clear();
                }
                if (ResetOnObservedValueChanged)
                {
                    ca.Execute(NumberOfIterations);
                }
                else
                {
                    ca.Update(NumberOfIterations - ca.NumberOfIterationsDone);
                }
                if (s != null)
                {
                    long elapsed = s.ElapsedMilliseconds;
                    Console.WriteLine("Inference time was {1}ms (max {0} iterations)",
                                      NumberOfIterations, elapsed);
                    if (FileStats.ReadCount > 0 || FileStats.WriteCount > 0)
                    {
                        Console.WriteLine("{0} file reads {1} file writes", FileStats.ReadCount, FileStats.WriteCount);
                    }
                }
            }
            finally
            {
                ca.ProgressChanged -= OnProgressChanged;
                if (this.MessageUpdated != null)
                {
                    DebuggingSupport.TryAddRemoveEventListenerDynamic(ca, OnMessageUpdated, add: false);
                }
            }
        }