Files
richard-churchman cc0a2bee21 Extensive modifications to the serialisation of Neural Network models to support Newtonsoft Json.net rather than Binary Serializer.
Included open-source Accord.net library code natively to the solution given the deprecated state of that project.  Accord.net will be supported inside Jube for own needs (such as upgrading to .Net 7+).

Accord.net libraries modified to compile to .Net 6.

Modified Accord.net libraries to better support Newtonsoft Json.net serialisation.

Included annotations in the Accord.net libraries to house important dependency statistics. Serialised json can be used independently of Jube to recall a Neural Network.

Removal of Binary Serializer totally as a precursor to the projects support of .Net 7+.

Extensive bug fixing in crucial functions of Neural Network training and recall.  While this part of the system remains in Alpha, it is substantially more fit for purpose.
2023-12-24 10:22:22 +01:00

295 lines
13 KiB
C#

// Accord Statistics Library
// The Accord.NET Framework
// http://accord-framework.net
//
// Copyright © César Souza, 2009-2017
// cesarsouza at gmail.com
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
//
#pragma warning disable 612, 618
namespace Accord.Statistics.Models.Markov
{
using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using Accord.Statistics.Distributions;
using Accord.Statistics.Models.Markov.Topology;
using Accord.Statistics.Models.Markov.Learning;
using System.Collections.Generic;
using Accord.Compat;
/// <summary>
/// Obsolete. Please use <see cref="HiddenMarkovClassifier{TDistribution, TObservation}"/> instead.
/// </summary>
///
/// <seealso cref="HiddenMarkovClassifierLearning{TDistribution, TObservation}"/>
/// <seealso cref="HiddenMarkovClassifier"/>
///
[Serializable]
[Obsolete("Please use HiddenMarkovClassifier<TDistribution, TObservation> instead.")]
public class HiddenMarkovClassifier<TDistribution> : BaseHiddenMarkovClassifier<HiddenMarkovModel<TDistribution>>,
IEnumerable<HiddenMarkovModel<TDistribution>>, IHiddenMarkovClassifier where TDistribution : IDistribution
{
/// <summary>
/// Gets the number of dimensions of the
/// observations handled by this classifier.
/// </summary>
///
public int Dimension
{
get { return Models[0].Dimension; }
}
/// <summary>
/// Creates a new Sequence Classifier with the given number of classes.
/// </summary>
///
/// <param name="classes">The number of classes in the classifier.</param>
/// <param name="states">An array specifying the number of hidden states for each
/// of the classifiers. By default, and Ergodic topology will be used.</param>
/// <param name="initial">The initial probability distributions for the hidden states.
/// For multivariate continuous density distributions, such as Normal mixtures, the
/// choice of initial values is crucial for a good performance.</param>
///
public HiddenMarkovClassifier(int classes, int[] states, TDistribution initial)
: base(classes)
{
for (int i = 0; i < classes; i++)
Models[i] = new HiddenMarkovModel<TDistribution>(states[i], initial);
}
/// <summary>
/// Creates a new Sequence Classifier with the given number of classes.
/// </summary>
///
/// <param name="classes">The number of classes in the classifier.</param>
/// <param name="topology">The topology of the hidden states. A forward-only topology
/// is indicated to sequence classification problems, such as speech recognition.</param>
/// <param name="initial">The initial probability distributions for the hidden states.
/// For multivariate continuous density distributions, such as Normal mixtures, the
/// choice of initial values is crucial for a good performance.</param>
///
public HiddenMarkovClassifier(int classes, ITopology topology, TDistribution initial)
: base(classes)
{
for (int i = 0; i < classes; i++)
Models[i] = new HiddenMarkovModel<TDistribution>(topology, initial);
}
/// <summary>
/// Creates a new Sequence Classifier with the given number of classes.
/// </summary>
///
/// <param name="classes">The number of classes in the classifier.</param>
/// <param name="topology">The topology of the hidden states. A forward-only topology
/// is indicated to sequence classification problems, such as speech recognition.</param>
/// <param name="initial">The initial probability distributions for the hidden states.
/// For multivariate continuous density distributions, such as Normal mixtures, the
/// choice of initial values is crucial for a good performance.</param>
///
public HiddenMarkovClassifier(int classes, ITopology topology, TDistribution[] initial)
: base(classes)
{
for (int i = 0; i < classes; i++)
Models[i] = new HiddenMarkovModel<TDistribution>(topology, initial[i]);
}
/// <summary>
/// Creates a new Sequence Classifier with the given number of classes.
/// </summary>
///
/// <param name="classes">The number of classes in the classifier.</param>
/// <param name="topology">The topology of the hidden states. A forward-only topology
/// is indicated to sequence classification problems, such as speech recognition.</param>
/// <param name="initial">The initial probability distributions for the hidden states.
/// For multivariate continuous density distributions, such as Normal mixtures, the
/// choice of initial values is crucial for a good performance.</param>
///
public HiddenMarkovClassifier(int classes, ITopology[] topology, TDistribution[] initial)
: base(classes)
{
for (int i = 0; i < classes; i++)
Models[i] = new HiddenMarkovModel<TDistribution>(topology[i], initial[i]);
}
/// <summary>
/// Creates a new Sequence Classifier with the given number of classes.
/// </summary>
///
/// <param name="classes">The number of classes in the classifier.</param>
/// <param name="topology">The topology of the hidden states. A forward-only topology
/// is indicated to sequence classification problems, such as speech recognition.</param>
/// <param name="initial">The initial probability distributions for the hidden states.
/// For multivariate continuous density distributions, such as Normal mixtures, the
/// choice of initial values is crucial for a good performance.</param>
///
public HiddenMarkovClassifier(int classes, ITopology[] topology, TDistribution initial)
: base(classes)
{
for (int i = 0; i < classes; i++)
Models[i] = new HiddenMarkovModel<TDistribution>(topology[i], initial);
}
/// <summary>
/// Creates a new Sequence Classifier with the given number of classes.
/// </summary>
///
/// <param name="classes">The number of classes in the classifier.</param>
/// <param name="topology">The topology of the hidden states. A forward-only topology
/// is indicated to sequence classification problems, such as speech recognition.</param>
/// <param name="initial">The initial probability distributions for the hidden states.
/// For multivariate continuous density distributions, such as Normal mixtures, the
/// choice of initial values is crucial for a good performance.</param>
/// <param name="names">The class labels for each of the models.</param>
///
public HiddenMarkovClassifier(int classes, ITopology[] topology, TDistribution initial, string[] names)
: base(classes)
{
for (int i = 0; i < classes; i++)
Models[i] = new HiddenMarkovModel<TDistribution>(topology[i], initial) { Tag = names[i] };
}
/// <summary>
/// Creates a new Sequence Classifier with the given number of classes.
/// </summary>
///
/// <param name="models">
/// The models specializing in each of the classes of
/// the classification problem.</param>
///
public HiddenMarkovClassifier(HiddenMarkovModel<TDistribution>[] models)
: base(models) { }
/// <summary>
/// Creates a new Sequence Classifier with the given number of classes.
/// </summary>
///
/// <param name="classes">The number of classes in the classifier.</param>
/// <param name="topology">The topology of the hidden states. A forward-only topology
/// is indicated to sequence classification problems, such as speech recognition.</param>
/// <param name="initial">The initial probability distributions for the hidden states.
/// For multivariate continuous density distributions, such as Normal mixtures, the
/// choice of initial values is crucial for a good performance.</param>
/// <param name="names">The class labels for each of the models.</param>
///
public HiddenMarkovClassifier(int classes, ITopology topology, TDistribution initial, string[] names)
: base(classes)
{
for (int i = 0; i < classes; i++)
Models[i] = new HiddenMarkovModel<TDistribution>(topology, initial) { Tag = names[i] };
}
/// <summary>
/// Computes the most likely class for a given sequence.
/// </summary>
///
/// <param name="sequence">The sequence of observations.</param>
///
/// <returns>Return the label of the given sequence, or -1 if it has
/// been rejected by the <see cref="BaseHiddenMarkovClassifier{T}.Threshold">
/// threshold model</see>.</returns>
///
public new int Compute(Array sequence)
{
return base.Compute(sequence);
}
/// <summary>
/// Computes the most likely class for a given sequence.
/// </summary>
///
/// <param name="sequence">The sequence of observations.</param>
/// <param name="response">The probability of the assigned class.</param>
///
/// <returns>Return the label of the given sequence, or -1 if it has
/// been rejected by the <see cref="BaseHiddenMarkovClassifier{T}.Threshold">
/// threshold model</see>.</returns>
///
public new int Compute(Array sequence, out double response)
{
return base.Compute(sequence, out response);
}
/// <summary>
/// Computes the most likely class for a given sequence.
/// </summary>
///
/// <param name="sequence">The sequence of observations.</param>
/// <param name="responsibilities">The class responsibilities (or
/// the probability of the sequence to belong to each class). When
/// using threshold models, the sum of the probabilities will not
/// equal one, and the amount left was the threshold probability.
/// If a threshold model is not being used, the array should sum to
/// one.</param>
///
/// <returns>Return the label of the given sequence, or -1 if it has
/// been rejected by the <see cref="BaseHiddenMarkovClassifier{T}.Threshold">
/// threshold model</see>.</returns>
///
public new int Compute(Array sequence, out double[] responsibilities)
{
return base.Compute(sequence, out responsibilities);
}
/// <summary>
/// Computes the log-likelihood of a sequence
/// belong to a given class according to this
/// classifier.
/// </summary>
/// <param name="sequence">The sequence of observations.</param>
/// <param name="output">The output class label.</param>
///
/// <returns>The log-likelihood of the sequence belonging to the given class.</returns>
///
public new double LogLikelihood(Array sequence, int output)
{
return base.LogLikelihood(sequence, output);
}
/// <summary>
/// Computes the log-likelihood that a sequence
/// belongs any of the classes in the classifier.
/// </summary>
/// <param name="sequence">The sequence of observations.</param>
///
/// <returns>The log-likelihood of the sequence belonging to the classifier.</returns>
///
public new double LogLikelihood(Array sequence)
{
return base.LogLikelihood(sequence);
}
/// <summary>
/// Computes the log-likelihood of a set of sequences
/// belonging to their given respective classes according
/// to this classifier.
/// </summary>
/// <param name="sequences">A set of sequences of observations.</param>
/// <param name="outputs">The output class label for each sequence.</param>
///
/// <returns>The log-likelihood of the sequences belonging to the given classes.</returns>
///
public new double LogLikelihood(Array[] sequences, int[] outputs)
{
return base.LogLikelihood(sequences, outputs);
}
}
}