/// <summary> /// Raises the <see cref="E:GenerativeClassModelLearningStarted"/> event. /// </summary> /// /// <param name="args">The <see cref="Accord.Statistics.Models.Markov.Learning.GenerativeLearningEventArgs"/> instance containing the event data.</param> /// protected void OnGenerativeClassModelLearningStarted(GenerativeLearningEventArgs args) { if (ClassModelLearningStarted != null) { ClassModelLearningStarted(this, args); } }
private void LearnInner(TObservation[][] x, int[] y, int i, int classes, double[] logLikelihood, int[] classCounts) { // We will start the class model learning problem Trace.WriteLine(String.Format("Starting: {0}", i)); var args = new GenerativeLearningEventArgs(i, classes); OnGenerativeClassModelLearningStarted(args); // Select the input/output set corresponding // to the model's specialization class int[] idx = y.Find(y_j => y_j == i); TObservation[][] observations = x.Get(idx); classCounts[i] = observations.Length; if (observations.Length > 0) { // Create and configure the learning algorithm var innerModelTeacher = Learner(i); var innerModel = innerModelTeacher.Learn(observations); Classifier.Models[i] = innerModel; logLikelihood[i] = innerModel.LogLikelihood(observations).Sum(); } // Update and report progress Trace.WriteLine(String.Format("Done: {0} ", i)); OnGenerativeClassModelLearningFinished(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, #if !DEBUG Parallel.For(0, classes, i => #else for (int i = 0; i < classes; i++) #endif { // 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); }
/// <summary> /// Learns a model that can map the given inputs to the given outputs. /// </summary> /// <param name="x">The model inputs.</param> /// <param name="y">The desired outputs associated with each <paramref name="x">inputs</paramref>.</param> /// <param name="weights">The weight of importance for each input-output pair.</param> /// <returns> /// A model that has learned how to produce <paramref name="y" /> given <paramref name="x" />. /// </returns> public TClassifier Learn(TObservation[][] x, int[] y, double[] weights = null) { if (x.Length != y.Length) { throw new DimensionMismatchException("outputs", "The number of inputs and outputs does not match."); } for (int i = 0; i < y.Length; i++) { if (y[i] < 0 || y[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 = y.Find(y_j => y_j == i); TObservation[][] observations = x.Get(inx); classCounts[i] = observations.Length; if (observations.Length > 0) { // Create and configure the learning algorithm var teacher = Learner(i); var model = teacher.Learn(observations); logLikelihood[i] = model.LogLikelihood(observations).Sum(); } // Update and report progress OnGenerativeClassModelLearningFinished(args); }); if (Empirical) { for (int i = 0; i < classes; i++) { Classifier.Priors[i] = (double)classCounts[i] / x.Length; } } if (Rejection) { Classifier.Threshold = Threshold(); } LogLikelihood = logLikelihood.Sum(); return(Classifier); }
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.Get(inx); classCounts[i] = observations.Length; if (observations.Length > 0) { // Create and configure the learning algorithm // Backward compatibility case var 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()); }