Files
jube-fork/Accord.Statistics/Kernels/Sparse/SparseGaussian.cs
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

209 lines
7.1 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// 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
//
namespace Accord.Statistics.Kernels.Sparse
{
using System;
using Accord.Math.Distances;
using Accord.Compat;
/// <summary>
/// Sparse Gaussian Kernel.
/// </summary>
///
/// <remarks>
/// <para>
/// The Gaussian kernel requires tuning for the proper value of σ. Different approaches
/// to this problem includes the use of brute force (i.e. using a grid-search algorithm)
/// or a gradient ascent optimization.</para>
///
/// <para>
/// For an example on how to create a sparse kernel, please see the <see cref="SparseLinear"/> page.</para>
///
/// <para>
/// References:
/// <list type="bullet">
/// <item><description>
/// P. F. Evangelista, M. J. Embrechts, and B. K. Szymanski. Some Properties of the
/// Gaussian Kernel for One Class Learning. Available on: http://www.cs.rpi.edu/~szymansk/papers/icann07.pdf </description></item>
/// </list></para>
/// </remarks>
///
[Serializable]
[Obsolete("Please use the Gaussian kernel with Sparse<double> instead.")]
public sealed class SparseGaussian : KernelBase, IKernel, IDistance, IReverseDistance
{
private double sigma;
private double gamma;
/// <summary>
/// Constructs a new Sparse Gaussian Kernel
/// </summary>
///
public SparseGaussian()
: this(1)
{
}
/// <summary>
/// Constructs a new Sparse Gaussian Kernel
/// </summary>
///
/// <param name="sigma">The standard deviation for the Gaussian distribution. Default is 1.</param>
///
public SparseGaussian(double sigma)
{
this.Sigma = sigma;
}
/// <summary>
/// Gets or sets the sigma value for the kernel. When setting
/// sigma, gamma gets updated accordingly (gamma = 0.5*/sigma^2).
/// </summary>
///
public double Sigma
{
get { return sigma; }
set
{
sigma = value;
gamma = 1.0 / (2.0 * sigma * sigma);
}
}
/// <summary>
/// Gets or sets the gamma value for the kernel. When setting
/// gamma, sigma gets updated accordingly (gamma = 0.5*/sigma^2).
/// </summary>
///
public double Gamma
{
get { return gamma; }
set
{
gamma = value;
sigma = Math.Sqrt(1.0 / (gamma * 2.0));
}
}
/// <summary>
/// Gaussian Kernel function.
/// </summary>
///
/// <param name="x">Vector <c>x</c> in input space.</param>
/// <param name="y">Vector <c>y</c> in input space.</param>
/// <returns>Dot product in feature (kernel) space.</returns>
///
public override double Function(double[] x, double[] y)
{
// Optimization in case x and y are
// exactly the same object reference.
if (x == y)
return 1.0;
#pragma warning disable 0618
double norm = SparseLinear.SquaredEuclidean(x, y);
return Math.Exp(-gamma * norm);
}
/// <summary>
/// Computes the distance in input space
/// between two points given in feature space.
/// </summary>
///
/// <param name="x">Vector <c>x</c> in feature (kernel) space.</param>
/// <param name="y">Vector <c>y</c> in feature (kernel) space.</param>
///
/// <returns>
/// Distance between <c>x</c> and <c>y</c> in input space.
/// </returns>
///
public override double Distance(double[] x, double[] y)
{
if (x == y)
return 0.0;
double norm = SparseLinear.SquaredEuclidean(x, y);
return 2 - 2 * Math.Exp(-gamma * norm);
}
/// <summary>
/// Computes the squared distance in input space
/// between two points given in feature space.
/// </summary>
///
/// <param name="x">Vector <c>x</c> in feature (kernel) space.</param>
/// <param name="y">Vector <c>y</c> in feature (kernel) space.</param>
///
/// <returns>
/// Squared distance between <c>x</c> and <c>y</c> in input space.
/// </returns>
///
public double ReverseDistance(double[] x, double[] y)
{
if (x == y)
return 0.0;
double norm = SparseLinear.SquaredEuclidean(x, y);
return -(1.0 / gamma) * Math.Log(1.0 - 0.5 * norm);
}
/// <summary>
/// Estimate appropriate values for sigma given a data set.
/// </summary>
///
/// <remarks>
/// This method uses a simple heuristic to obtain appropriate values
/// for sigma in a radial basis function kernel. The heuristic is shown
/// by Caputo, Sim, Furesjo and Smola, "Appearance-based object
/// recognition using SVMs: which kernel should I use?", 2002.
/// </remarks>
///
/// <param name="inputs">The data set.</param>
/// <param name="samples">The number of random samples to analyze.</param>
/// <param name="range">The range of suitable values for sigma.</param>
/// <returns>A Gaussian kernel initialized with an appropriate sigma value.</returns>
///
public static SparseGaussian Estimate(double[][] inputs, int samples, out DoubleRange range)
{
if (samples > inputs.Length)
throw new ArgumentOutOfRangeException("samples");
double[] distances = Gaussian.Distances(inputs, samples);
double q1 = Math.Sqrt(distances[(int)Math.Ceiling(0.15 * distances.Length)] / 2.0);
double q9 = Math.Sqrt(distances[(int)Math.Ceiling(0.85 * distances.Length)] / 2.0);
double qm = Math.Sqrt(Measures.Median(distances, alreadySorted: true) / 2.0);
range = new DoubleRange(q1, q9);
return new SparseGaussian(sigma: qm);
}
}
}