/// <summary> /// Raises the <see cref="E:GenerativeClassModelLearningStarted"/> event. /// </summary> /// /// <param name="args">The <see cref="BestCS.Statistics.Models.Markov.Learning.GenerativeLearningEventArgs"/> instance containing the event data.</param> /// protected void OnGenerativeClassModelLearningStarted(GenerativeLearningEventArgs args) { if (ClassModelLearningStarted != null) { ClassModelLearningStarted(this, args); } }
/// <summary> /// Trains each model to recognize each of the output labels. /// </summary> /// <returns>The sum log-likelihood for all models after training.</returns> /// protected double Run <T>(T[] inputs, int[] outputs) { if (inputs == null) { throw new ArgumentNullException("inputs"); } if (outputs == null) { throw new ArgumentNullException("outputs"); } if (inputs.Length != outputs.Length) { throw new DimensionMismatchException("outputs", "The number of inputs and outputs does not match."); } for (int i = 0; i < outputs.Length; i++) { if (outputs[i] < 0 || outputs[i] >= Classifier.Classes) { throw new ArgumentOutOfRangeException("outputs"); } } int classes = Classifier.Classes; double[] logLikelihood = new double[classes]; int[] classCounts = new int[classes]; // For each model, Parallel.For(0, classes, i => { // We will start the class model learning problem var args = new GenerativeLearningEventArgs(i, classes); OnGenerativeClassModelLearningStarted(args); // Select the input/output set corresponding // to the model's specialization class int[] inx = outputs.Find(y => y == i); T[] observations = inputs.Submatrix(inx); classCounts[i] = observations.Length; if (observations.Length > 0) { // Create and configure the learning algorithm IUnsupervisedLearning teacher = Algorithm(i); // Train the current model in the input/output subset logLikelihood[i] = teacher.Run(observations as Array[]); } // Update and report progress OnGenerativeClassModelLearningFinished(args); }); if (Empirical) { for (int i = 0; i < classes; i++) { Classifier.Priors[i] = (double)classCounts[i] / inputs.Length; } } if (Rejection) { Classifier.Threshold = Threshold(); } // Returns the sum log-likelihood for all models. return(logLikelihood.Sum()); }