mirror of
https://github.com/hanzoai/jube-fork.git
synced 2026-10-30 18:33:33 +00:00
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.
147 lines
4.5 KiB
C#
147 lines
4.5 KiB
C#
// AForge Neural Net Library
|
|
// AForge.NET framework
|
|
// http://www.aforgenet.com/framework/
|
|
//
|
|
// Copyright © AForge.NET, 2007-2012
|
|
// [email protected]
|
|
//
|
|
|
|
namespace Accord.Neuro
|
|
{
|
|
using System;
|
|
using Accord.Compat;
|
|
|
|
/// <summary>
|
|
/// Sigmoid activation function.
|
|
/// </summary>
|
|
///
|
|
/// <remarks><para>The class represents sigmoid activation function with
|
|
/// the next expression:
|
|
/// <code lang="none">
|
|
/// 1
|
|
/// f(x) = ------------------
|
|
/// 1 + exp(-alpha * x)
|
|
///
|
|
/// alpha * exp(-alpha * x )
|
|
/// f'(x) = ---------------------------- = alpha * f(x) * (1 - f(x))
|
|
/// (1 + exp(-alpha * x))^2
|
|
/// </code>
|
|
/// </para>
|
|
///
|
|
/// <para>Output range of the function: <b>[0, 1]</b>.</para>
|
|
///
|
|
/// <para>Functions graph:</para>
|
|
/// <img src="..\images\neuro\sigmoid.bmp" width="242" height="172" />
|
|
/// </remarks>
|
|
///
|
|
[Serializable]
|
|
public class SigmoidFunction : IActivationFunction, ICloneable
|
|
{
|
|
// sigmoid's alpha value
|
|
private double alpha = 2;
|
|
|
|
/// <summary>
|
|
/// Sigmoid's alpha value.
|
|
/// </summary>
|
|
///
|
|
/// <remarks><para>The value determines steepness of the function. Increasing value of
|
|
/// this property changes sigmoid to look more like a threshold function. Decreasing
|
|
/// value of this property makes sigmoid to be very smooth (slowly growing from its
|
|
/// minimum value to its maximum value).</para>
|
|
///
|
|
/// <para>Default value is set to <b>2</b>.</para>
|
|
/// </remarks>
|
|
///
|
|
public double Alpha
|
|
{
|
|
get { return alpha; }
|
|
set { alpha = value; }
|
|
}
|
|
|
|
/// <summary>
|
|
/// Initializes a new instance of the <see cref="SigmoidFunction"/> class.
|
|
/// </summary>
|
|
public SigmoidFunction( ) { }
|
|
|
|
/// <summary>
|
|
/// Initializes a new instance of the <see cref="SigmoidFunction"/> class.
|
|
/// </summary>
|
|
///
|
|
/// <param name="alpha">Sigmoid's alpha value.</param>
|
|
///
|
|
public SigmoidFunction( double alpha )
|
|
{
|
|
this.alpha = alpha;
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// Calculates function value.
|
|
/// </summary>
|
|
///
|
|
/// <param name="x">Function input value.</param>
|
|
///
|
|
/// <returns>Function output value, <i>f(x)</i>.</returns>
|
|
///
|
|
/// <remarks>The method calculates function value at point <paramref name="x"/>.</remarks>
|
|
///
|
|
public double Function( double x )
|
|
{
|
|
return ( 1 / ( 1 + Math.Exp( -alpha * x ) ) );
|
|
}
|
|
|
|
/// <summary>
|
|
/// Calculates function derivative.
|
|
/// </summary>
|
|
///
|
|
/// <param name="x">Function input value.</param>
|
|
///
|
|
/// <returns>Function derivative, <i>f'(x)</i>.</returns>
|
|
///
|
|
/// <remarks>The method calculates function derivative at point <paramref name="x"/>.</remarks>
|
|
///
|
|
public double Derivative( double x )
|
|
{
|
|
double y = Function( x );
|
|
|
|
return ( alpha * y * ( 1 - y ) );
|
|
}
|
|
|
|
/// <summary>
|
|
/// Calculates function derivative.
|
|
/// </summary>
|
|
///
|
|
/// <param name="y">Function output value - the value, which was obtained
|
|
/// with the help of <see cref="Function"/> method.</param>
|
|
///
|
|
/// <returns>Function derivative, <i>f'(x)</i>.</returns>
|
|
///
|
|
/// <remarks><para>The method calculates the same derivative value as the
|
|
/// <see cref="Derivative"/> method, but it takes not the input <b>x</b> value
|
|
/// itself, but the function value, which was calculated previously with
|
|
/// the help of <see cref="Function"/> method.</para>
|
|
///
|
|
/// <para><note>Some applications require as function value, as derivative value,
|
|
/// so they can save the amount of calculations using this method to calculate derivative.</note></para>
|
|
/// </remarks>
|
|
///
|
|
public double Derivative2( double y )
|
|
{
|
|
return ( alpha * y * ( 1 - y ) );
|
|
}
|
|
|
|
/// <summary>
|
|
/// Creates a new object that is a copy of the current instance.
|
|
/// </summary>
|
|
///
|
|
/// <returns>
|
|
/// A new object that is a copy of this instance.
|
|
/// </returns>
|
|
///
|
|
public object Clone( )
|
|
{
|
|
return new SigmoidFunction( alpha );
|
|
}
|
|
}
|
|
}
|