人工神经网络框架AForge学习(二):Sigmoid激活函数
Code
namespace AForge.Neuro
{
using System;
/// <summary>
/// Sigmoid activation function
/// 西格玛激活函数
/// </summary>
///
/// <remarks>The class represents sigmoid activation function with
/// the next expression:<br />
/// 这个类表示西格玛激活函数按照下面的公示描述:
/// <code>
/// 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>
/// Output range of the function: <b>[0, 1]</b><br /><br />
/// 输出范围在[0,1]区间
/// Functions graph:<br />
/// <img src="sigmoid.bmp" width="242" height="172" />
/// </remarks>
public class SigmoidFunction : IActivationFunction
{
// sigmoid's alpha value
// 阀值、偏置、活性值?
private double alpha = 2;
/// <summary>
/// Sigmoid's alpha value
/// </summary>
///
/// <remarks>The value determines steepness of the function. Default value: <b>2</b>.
/// </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 <b>x</b>.</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 <b>x</b>.</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
/// 由Function计算出来的y
/// </param>
///
/// <returns>Function derivative, <i>f'(x)</i></returns>
///
/// <remarks>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. <i>(Some applications require as
/// function value, as derivative value, so they can seve the amount of
/// calculations using this method to calculate derivative)</i>
/// 这个方法和Derivative一样计算导数,但它不以输入值x为参数,而是以原先由Funcion计算
/// 出的y为参数.
/// </remarks>
///
public double Derivative2( double y )
{
return ( alpha * y * ( 1 - y ) );
}
}
}
namespace AForge.Neuro
{
using System;
/// <summary>
/// Sigmoid activation function
/// 西格玛激活函数
/// </summary>
///
/// <remarks>The class represents sigmoid activation function with
/// the next expression:<br />
/// 这个类表示西格玛激活函数按照下面的公示描述:
/// <code>
/// 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>
/// Output range of the function: <b>[0, 1]</b><br /><br />
/// 输出范围在[0,1]区间
/// Functions graph:<br />
/// <img src="sigmoid.bmp" width="242" height="172" />
/// </remarks>
public class SigmoidFunction : IActivationFunction
{
// sigmoid's alpha value
// 阀值、偏置、活性值?
private double alpha = 2;
/// <summary>
/// Sigmoid's alpha value
/// </summary>
///
/// <remarks>The value determines steepness of the function. Default value: <b>2</b>.
/// </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 <b>x</b>.</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 <b>x</b>.</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
/// 由Function计算出来的y
/// </param>
///
/// <returns>Function derivative, <i>f'(x)</i></returns>
///
/// <remarks>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. <i>(Some applications require as
/// function value, as derivative value, so they can seve the amount of
/// calculations using this method to calculate derivative)</i>
/// 这个方法和Derivative一样计算导数,但它不以输入值x为参数,而是以原先由Funcion计算
/// 出的y为参数.
/// </remarks>
///
public double Derivative2( double y )
{
return ( alpha * y * ( 1 - y ) );
}
}
}