C# 中的激活函数列表

2024-03-19

我可以在数学中找到激活函数列表,但在代码中却找不到。 所以我想如果应该有这样一个列表的话,这将是代码中放置这样一个列表的正确位置。 从这两个链接中算法的翻译开始:https://en.wikipedia.org/wiki/Activation_function https://en.wikipedia.org/wiki/Activation_function https://stats.stackexchange.com/questions/115258/compressive-list-of-activation-functions-in-neural-networks-with-pros-cons https://stats.stackexchange.com/questions/115258/comprehensive-list-of-activation-functions-in-neural-networks-with-pros-cons

目标是拥有一个可通过 UI 轻松访问的激活类(包含函数及其派生类)。

编辑: 我的尝试

using UnityEngine;
using System.Collections;
using System;

///<summary>
///Activation Functions from:
///https://en.wikipedia.org/wiki/Activation_function
///https://stats.stackexchange.com/questions/115258/comprehensive-list-of-activation-functions-in-neural-networks-with-pros-cons
///D infront means the Deravitive of the function
///x is the input of one perceptron. a is the alpha value sometimes needed.
///</summary>
[System.Serializable]
public class Activation
{
    public ActivationType activationType;
    public Activation(ActivationType type)
    {
        activationType = type;
    }
    public double AFunction(double x)
    {
        switch(activationType)
        {
        case ActivationType.Identity:
            return Identity(x);
        case ActivationType.BinaryStep:
            return BinaryStep(x);
        case ActivationType.Logistic:
            return Logistic(x);
        case ActivationType.Tanh:
            return Tanh(x);
        case ActivationType.ArcTan:
            return ArcTan(x);
        case ActivationType.ReLU:
            return ReLU(x);
        case ActivationType.SoftPlus:
            return SoftPlus(x);
        case ActivationType.BentIdentity:
            return BentIdentity(x);
        case ActivationType.Sinusoid:
            return Sinusoid(x);
        case ActivationType.Sinc:
            return Sinc(x);
        case ActivationType.Gaussian:
            return Gaussian(x);
        case ActivationType.Bipolar:
            return Bipolar(x);
        case ActivationType.BipolarSigmoid:
            return BipolarSigmoid(x);
        }
        return 0;
    }
    public double ActivationDerivative(double x)
    {
        switch(activationType)
        {
        case ActivationType.Logistic:
            return DLogistic(x);
        case ActivationType.Tanh:
            return DTanh(x);
        case ActivationType.ArcTan:
            return DArcTan(x);
        case ActivationType.ReLU:
            return DReLU(x);
        case ActivationType.SoftPlus:
            return DSoftPlus(x);
        case ActivationType.BentIdentity:
            return DBentIdentity(x);
        case ActivationType.Sinusoid:
            return DSinusoid(x);
        case ActivationType.Sinc:
            return DSinc(x);
        case ActivationType.Gaussian:
            return DGaussian(x);
        case ActivationType.BipolarSigmoid:
            return DBipolarSigmoid(x);
        }
        return 0;
    }
    public double AFunction(double x, double a)
    {
        switch(activationType)
        {
        case ActivationType.PReLU:
            return PReLU(x,a);
        case ActivationType.ELU:
            return ELU(x,a);
        }
        return 0;
    }
    public double ActivationDerivative(double x, double a)
    {
        switch(activationType)
        {
        case ActivationType.PReLU:
            return DPReLU(x,a);
        case ActivationType.ELU:
            return DELU(x,a);
        }
        return 0;
    }
    public double Identity(double x)
    {
        return x;
    }

    public double BinaryStep(double x)
    {
        return x < 0 ? 0 : 1;
    }

    public double Logistic(double x)
    {
        return 1/(1+Math.Pow(Math.E,-x));
    }
    public double DLogistic(double x)
    {
        return Logistic(x)*(1-Logistic(x));
    }
    public double Tanh(double x)
    {
        return 2/(1+Math.Pow(Math.E, -(2*x)))-1;
    }
    public double DTanh(double x)
    {
        return 1-Math.Pow(Tanh(x),2);
    }
    public double ArcTan(double x)
    {
        return Math.Atan(x);
    }
    public double DArcTan(double x)
    {
        return 1/Math.Pow(x,2)+1;
    }
    //Rectified Linear Unit
    public double ReLU(double x)
    {
        return Math.Max(0,x);// x < 0 ? 0 : x;
    }
    public double DReLU(double x)
    {
        return Math.Max(0,1);// x < 0 ? 0 : x;
    }
    //Parameteric Rectified Linear Unit 
    public double PReLU(double x, double a)
    {
        return x < 0 ? a*x : x;
    }
    public double DPReLU(double x, double a)
    {
        return x < 0 ? a : 1;
    }
    //Exponential Linear Unit 
    public double ELU(double x, double a)
    {
        return x < 0 ? a*(Math.Pow(Math.E, x) - 1) : x;
    }
    public double DELU(double x, double a)
    {
        return x < 0 ? ELU(x, a)+a: 1;
    }
    public double SoftPlus(double x)
    {
        return Math.Log(Math.Exp(x)+1);
    }
    public double DSoftPlus(double x)
    {
        return Logistic(x);
    }
    public double BentIdentity(double x)
    {
        return (((Math.Sqrt(Math.Pow(x,2)+1))-1)/2)+x;
    }
    public double DBentIdentity(double x)
    {
        return (x/(2*Math.Sqrt(Math.Pow(x,2)+1)))+1;
    }
//  public float SoftExponential(float x)
//  {
//
//  }
    public double Sinusoid(double x)
    {
        return Math.Sin(x);
    }
    public double DSinusoid(double x)
    {
        return Math.Cos(x);
    }
    public double Sinc(double x)
    {
        return x == 0 ? 1 : Math.Sin(x)/x;
    }
    public double DSinc(double x)
    {
        return x == 0 ? 0 : (Math.Cos(x)/x)-(Math.Sin(x)/Math.Pow(x,2));
    }
    public double Gaussian(double x)
    {
        return Math.Pow(Math.E, Math.Pow(-x, 2));
    }
    public double DGaussian(double x)
    {
        return -2*x*Math.Pow(Math.E, Math.Pow(-x,2));
    }
    public double Bipolar(double x)
    {
        return x < 0 ? -1:1;
    }
    public double BipolarSigmoid(double x)
    {
        return (1-Math.Exp(-x))/(1+Math.Exp(-x));
    }
    public double DBipolarSigmoid(double x)
    {
        return 0.5 * (1 + BipolarSigmoid(x)) * (1 - BipolarSigmoid(x));
    }

    public double Scaler(double x, double min, double max)
    {
        return (x - min) / (max - min);
    }
}
public enum ActivationType
{
    Identity,
    BinaryStep,
    Logistic,
    Tanh,
    ArcTan,
    ReLU,
    PReLU,
    ELU,
    SoftPlus,
    BentIdentity,
    Sinusoid,
    Sinc,
    Gaussian,
    Bipolar,
    BipolarSigmoid
}

不确定我的数学计算是否正确,所以我不会将其发布为答案。 如果有人愿意进行错误检查,我可以将其作为答案。


我找到了这个:软指数激活函数 https://github.com/keras-team/keras/pull/3851/commits/232f77003f0357d3de31a0db0b1ac9c532dfb3ce

C# 转换:

public double SoftExponential(double x, double alpha = 0.0, double max_value = 0.0)
{

    // """Soft Exponential activation function by Godfrey and Gashler
    // See: https://arxiv.org/pdf/1602.01321.pdf
    // α == 0:  f(α, x) = x
    // α  > 0:  f(α, x) = (exp(αx)-1) / α + α
    // α< 0:  f(α, x) = -ln(1 - α(x + α)) / α
    // """

    if (alpha == 0)
        return x;
    else if (alpha > 0)
        return alpha + (Math.Exp(alpha * x) - 1.0) / alpha;
    else
        return -Math.Log(1 - alpha * (x + alpha)) / alpha;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

C# 中的激活函数列表 的相关文章

随机推荐

  • 如何在 SQL Server 2008 中重建视图

    我的数据库中有一个视图 有人用一个表中的 定义了该视图 我刚刚向该表添加了一个新列 我希望视图反映新列 除了重新执行视图创建脚本之外 还有其他方法重建视图吗 我正在寻找类似的东西sp 重新编译将重新编译存储过程 或者更准确地将其标记为在下次
  • Android webview无法渲染通过iframe嵌入的youtube视频

    这是关于在 Web 视图中使用最新的嵌入格式 iframe 加载 YouTube 视频 iframe 嵌入格式示例 在 Android 2 3 3 和 3 2 设备 HTC Desire 和 Asus Transformer 上测试代码 网
  • 不确定 unordered_map 是如何工作的

    我对 unordered map 的工作原理 存储桶是什么以及如何管理它们有点困惑 From 这篇博文 http codeforces com blog entry 21853 unordered map 是向量的向量 我的问题是 假设桶是
  • 如何获取较旧的 Xcode beta 版本?

    我已删除 Xcode 10 beta 3 并升级到 Xcode 10 beta 5 然而 由于this bug https stackoverflow com questions 51602785 xcode 10b5 duplicate
  • 如何在 Office VBA 编辑器中注释和取消注释代码块

    In the VBA editor of Office ALT F11 how do you comment or uncomment a block of code 在 VBA 编辑器中 转到View Toolbars Customise
  • Android Firebase setValue() 权限被拒绝

    这是在 firebase 上定义规则的方式 rules users read true user id write auth uid user id read true 我已经成功地使用 setValue 在我的注册活动中写入了新的用户信息
  • Windows Azure VM (Mac) 为 ios 设备构建 Ionic 应用程序

    我正在使用 Ionic 框架开发 Ionic2 Angular 应用程序 我对 Android 没有任何问题 我的问题是 我可以使用 Windows Azure VM Mac 为 ios 设备构建应用程序吗 I have Win 8 1 O
  • 避免 Xamarin 相机的“确定重试”按钮

    我正在使用来自的相机代码库https github com rasmuschristensen XamarinFormsImageGallery https github com rasmuschristensen XamarinForms
  • 用两个向量排序

    我想知道是否有可能 例如 vector
  • 计算 Pubsub 主题中未确认消息的数量

    我想在来自 pubsub 主题的所有消息都得到确认后执行一项操作 我尝试使用 Stackdriver 监控 API 来衡量 按云区域细分的未确认消息数 但不了解区域过滤器以及为什么需要它 在哪里可以查看我的主题使用的区域 并且由于某种未知的
  • 如何使用 JQL 检索特定状态的问题

    输入 url 或使用curl 运行 例如 https
  • list.count() 与 Counter() 性能

    在尝试查找字符串中一堆字符的频率时 为什么对 4 个不同的字符运行 string count character 4 次会比使用 collections Counter string 产生更快的执行时间 使用 time time 背景 给定
  • 如何在iOS 4中启用后台iPod控件来控制非iPod音乐?

    我想要完成的一个很好的例子是在最新版本的SpotifyiPhone应用程序 Pandora似乎有相同的功能 当 Spotify 在后台时 双击会打开 多任务坞 其中 iPod 控件 播放 暂停 前进等 允许控制 Spotify 的音乐播放
  • 升级到 Grails 2.3.0 时 RESTful 请求缺少参数

    我正在使用 Grails 和 RESTful 来开发我的 Web 应用程序 一切正常 直到我将应用程序升级到 Grails 2 3 这是我的 UrlMappings 我仍然正常发送请求 提交或做一些其他事情 但在 POST PUT 请求中
  • Bash:使用管道运算符时 Trap ERR 不起作用

    我试图将 stdout 和 stderr 中的所有内容记录到日志文件中 并仍然保留控制台 为此 我只是附加 tee a log file log对每一个命令 但是 如果脚本期间发生任何错误 我还想运行自定义命令 为此 我在脚本的开头添加了以
  • 警告:/etc/php/7.0/mods-available 下不存在模块 ini 文件

    我已经从 ubuntu 卸载了 php7 及其所有模块 当我尝试重新安装模块时 每个 php 模块都会出现以下错误 尽管模块已安装 但由于此错误 它未激活并且无法使用他们 有什么办法可以解决这个问题吗 每个模块的错误 安装时 Not rep
  • 来自数据库的实体生成器

    我需要在春天从现有数据库生成基于注释的实体 我尝试过骄傲 但生成的实体没有注释 我如何在基于骄傲的实体中生成注释 或者任何人都可以建议我一个好的实体生成器 我想说我也尝试过spring roo 您可以尝试 Telosys Tools 这是一
  • C++ 有什么方法可以以编程方式检测 POD 结构吗?

    我有存储 POD 结构的数据结构 每个实例化仅存储单个类型 因为它基本上是特定 POD 结构的数组 有时另一个开发人员 将修改这些结构之一 添加或修改数据类型 如果添加非 POD 元素 例如std string 数据结构在运行时崩溃 因为内
  • 如何禁用颤动开关

    在我的帮助屏幕中 我有这个开关 其目的是不执行任何操作 只是按原样显示 但我现在遇到的问题是 即使它没有做任何事情 用户也可以拖动开关 所以我试图弄清楚如何禁用它 以便没有人可以拖动开关按钮 return Container child C
  • C# 中的激活函数列表

    我可以在数学中找到激活函数列表 但在代码中却找不到 所以我想如果应该有这样一个列表的话 这将是代码中放置这样一个列表的正确位置 从这两个链接中算法的翻译开始 https en wikipedia org wiki Activation fu