C#中的错误/丢失/成本函数列表

时间:2016-04-03 15:43:04

标签: neural-network derivative cost-based-optimizer

我无法在代码中找到一个列表,这似乎是适合它的地方。 对于激活函数列表,请查看here

this链接中的函数转换开始。

这是我的尝试: (同样在这里不确定所有数学是否正确所以不作为答案发布)

using UnityEngine;
using System.Collections;
using System;

///<summary>
///Evaluation Functions from:
///https://stats.stackexchange.com/questions/154879/a-list-of-cost-functions-used-in-neural-networks-alongside-applications/154880#154880
///D infront means the Deravitive of the function
///error is the result for one perceptron.
///d_error is the derivative needed for backpropagation, 
///to calculate the delta weights for the network
///</summary>
[System.Serializable]
public class Evaluation 
{
    public ErrorFunction errorFunction;
    double error;
    double d_error;
    double td;
    double od;

    public Evaluation(ErrorFunction _errorFunction)
    {
        errorFunction = _errorFunction;
    }
    public double CalculateError(double t, double o)
    {
        switch(errorFunction)
        {
        case ErrorFunction.Quadratic:
            return Quadratic(t,o);
        case ErrorFunction.CrossEntropy:
            return CrossEntropy(t,o);
        case ErrorFunction.Exponentional:
            return Exponentional(t,o);
        case ErrorFunction.Hellinger:
            return Hellinger(t,o);
        case ErrorFunction.KullbackLeibler:
            return KullbackLeibler(t,o);
        case ErrorFunction.GeneralizedKL:
            return GeneralizedKL(t,o);
        case ErrorFunction.ItakuraSaito:
            return ItakuraSaito(t,o);
        }
        return 0;
    }
    public double DerivativeError(double t, double o)
    {
        switch(errorFunction)
        {
        case ErrorFunction.Quadratic:
            return DQuadratic(t,o);
        case ErrorFunction.CrossEntropy:
            return DCrossEntropy(t,o);
        case ErrorFunction.Exponentional:
            return DExponentional(t,o);
        case ErrorFunction.Hellinger:
            return DHellinger(t,o);
        case ErrorFunction.KullbackLeibler:
            return DKullbackLeibler(t,o);
        case ErrorFunction.GeneralizedKL:
            return DGeneralizedKL(t,o);
        case ErrorFunction.ItakuraSaito:
            return DItakuraSaito(t,o);
        }
        return 0;
    }
    public double Quadratic(double t, double o)
    {
        error = 0.5*Math.Pow((o-t),2);
        return error;
    }
    public double DQuadratic(double t, double o)
    {
        d_error = t-o;
        return d_error;
    }
    public double CrossEntropy(double t, double o)
    {
        error = t*Math.Log(o)+(1-t)*Math.Log(1-o);
        return -error;
    }
    public double DCrossEntropy(double t, double o)
    {
        d_error = (o-t)/((o+1)*o);
        return d_error;
    }
    public double Exponentional(double t, double o)
    {
        error = Math.Pow((o-t),2);
        return Math.PI*Math.Exp(1/Math.PI*error);
    }
    public double DExponentional(double t, double o)
    {
        d_error = (2/Math.PI)*(o-t)*Exponentional(t,o);
        return d_error;
    }
    public double Hellinger(double t, double o)
    {
        error = Math.Pow((Math.Sqrt(o)-Math.Sqrt(t)),2);
        return (1/Math.Sqrt(2))*error;
    }
    public double DHellinger(double t, double o)
    {
        d_error = (Math.Sqrt(o)-Math.Sqrt(t))/(Math.Sqrt(2)*Math.Sqrt(o));
        return d_error;
    }
    public double KullbackLeibler(double t, double o)
    {
        error = t*Math.Log(t/o);
        return error;
    }
    public double DKullbackLeibler(double t, double o)
    {
        d_error = t/o;
        return error;
    }
    public double GeneralizedKL(double t, double o)
    {
        error = KullbackLeibler(t,o) - t + o;
        return error;
    }
    public double DGeneralizedKL(double t, double o)
    {
        d_error = (t + o)/o;
        return d_error;
    }
    public double ItakuraSaito(double t, double o)
    {
        error = t/o - Math.Log(t/o) - 1;
        return error;
    }
    public double DItakuraSaito(double t, double o)
    {
        d_error = (t+Math.Pow(o,2))/Math.Pow(o,2);
        return d_error;
    }
}
public enum ErrorFunction
{
    none,
    Quadratic,
    CrossEntropy,
    Exponentional,
    Hellinger,
    KullbackLeibler,
    GeneralizedKL,
    ItakuraSaito
}

0 个答案:

没有答案