scratch lenet(11): C语言实现 squashing function

news2024/10/6 10:28:28

文章目录

    • 1. 目的
    • 2. Sigmoidal Function
      • 2.1 S2 用到 Sigmoidal Function
      • 2.2 Sigmoidal Function 的定义
    • 3. Squashing Function
      • 3.1 改用 Sigmoid Suahsing function 术语
      • 3.2 具体到 hyperlolic tangent 这一 squahsing function
    • 4. Squahsing function 的实现
    • References

1. 目的

弄清 LeNet-5 的论文原文 LeCun-98.pdf 中的 squashing function 的公式, 并用 C语言实现。

论文原文说的很拗口,其实就是 f ( a ) = 1.7159   tanh ⁡ ( 2 3 a ) f(a) = 1.7159 \ \tanh(\frac{2}{3}a) f(a)=1.7159 tanh(32a) 这个公式。本着溯源复刻的精神,首先从论文源码一点点抠出来它是怎么表述的(麻烦),然后给出 C 语言的实现(简单)。

2. Sigmoidal Function

2.1 S2 用到 Sigmoidal Function

在 LeCun-98.pdf1 论文中对 S2 层的介绍中,提到了 sigmoidal function:

Layer S2 is a sub-sampling layer with 6 feature maps of size 14x14. Each unit in each feature map is connected to a 2x2 neighborhood in the corresponding feature map in C1. The four inputs to a unit in S2 are added, then multiplied by a trainable coefficient, and added to a trainable bias. The result is passed through a sigmoidal function. The 2x2 receptive fields are non-overlapping, therefore feature maps in S2 have half the number of rows and column as feature maps in C1. Layer S2 has 12 trainable parameters and 5880 connections.

也就是说, 至少对于 S2 层而言, 计算过程的最后一步是用到了 sigmoidal function 的:
sum = x 11 + x 12 + x 21 + x 22 a = sum × c o e f f + bias x = f ( a ) ,   sigmoidal function \text{sum} = x_{11} + x_{12} + x_{21} + x_{22} \\ a = \text{sum} \times coeff + \text{bias} \\ x = f(a), \ \ \text{sigmoidal function} sum=x11+x12+x21+x22a=sum×coeff+biasx=f(a),  sigmoidal function

2.2 Sigmoidal Function 的定义

在论文 Approximation by Superpositions of a Sigmoidal Function2 里有所定义:

Definition. We say that σ \sigma σ is sigmoidal if
σ ( t ) = { 1 as  t → + ∞ 0 as  t → − ∞ \sigma(t)=\left\{ \begin{aligned} 1 & & \text{as} \ t \rightarrow +\infty \\ 0 & & \text{as} \ t \rightarrow -\infty \end{aligned} \right. σ(t)={10as t+as t

即:当自变量 t t t 趋向于正无穷时,因变量 σ ( t ) \sigma(t) σ(t) 趋向于1; 并且当自变量 t t t 趋向于负无穷时, 因变量 σ ( t ) \sigma(t) σ(t) 趋向于0, 那么函数 σ ( t ) \sigma(t) σ(t) 称为 Sigmoidal Function.

例如
σ ( x ) = 1 1 + e − x \sigma(x) = \frac{1}{1 + e^{-x}} σ(x)=1+ex1
是一个具体的 sigmoidal function, 曲线如下:
在这里插入图片描述

3. Squashing Function

3.1 改用 Sigmoid Suahsing function 术语

论文1 并没有一直用 sigmoidal function 这个术语, 而是转而用 sigmoid squashing function 这一术语。在对 F6 层的结构进行说明时,提到了 sigmoid squahsing function:

Layer F6, contains 86 units (the reason for this number comes from the design of the output layer, explained below) and is fully connected to C6. It has 10164 trainable parameters.
As in classical neural networks, units in layers up to F6 compute a dot product between their input vector and their weight vector, to which a bias is added. This weighted sum, denoted a i a_i ai for unit i, is then passed through a sigmoid squashing function to produce the state of unit i, denoted by x i x_i xi:
x i = f ( a i ) x_i = f(a_i) xi=f(ai)

到这里,猜测 sigmoidal function 和 sigmoid squashing function 含义相同。

3.2 具体到 hyperlolic tangent 这一 squahsing function

继续看原文1, 提到:

The squashing function is a scaled hyperbolic tangent:
f ( a ) = A tanh ⁡ ( S a ) f(a) = A \tanh(Sa) f(a)=Atanh(Sa)

这里其实作者有点偷换概念了, tanh ⁡ ( x ) \tanh(x) tanh(x) 并不满足 sigmoidal function 的定义,它在 x 趋向于 − ∞ -\infty 时,极限值为 -1 而不是 0, 曲线如下:
tanh ⁡ ( x ) = e x − e − x e x + e − x \tanh(x) = \frac{e^x - e^{-x}}{e^x + e^{-x}} tanh(x)=ex+exexex

在这里插入图片描述

作者给出的解释, 在 Appendix A 部分(P41)

For out simulation, we use A = 1.7159 and S = 2 3 S=\frac{2}{3} S=32. With this choice of parameters, the equalities f ( 1 ) = 1 f(1) = 1 f(1)=1 and f ( − 1 ) = − 1 f(-1) = -1 f(1)=1 are satisfied. The rationale behind this is that the overall gain of the squashing transformation is around 1 in normal operatiing conditions, and the interpretation of the state of the network is simplified. Moreover, the absolute value of the second derivative of f f f is maximum at +1 and -1, which improves the convergence torwards the end of the learning session. This particular choice of parameters is merely a convenience, and does not affect the result.

也就是说,作者用破坏了原本 Sigmoidal function 定义的函数 tanh 的目的是, 二阶导数在 x=1 和 x=-1 时能取到最大的绝对值,能让 learning 过程加速收敛,并且不影响结果。

P.S. LeCun-98b 3 再次给出了这个公式和解释:
在这里插入图片描述

StackOverFlow 上有人问过问题 4:
https://math.stackexchange.com/questions/1755711/why-is-there-a-factor-of-1-7159-with-the-tanh-function-used-in-neural-network-ac

4. Squahsing function 的实现

我们现在知道了 squahsing function 的具体形式5 6
f ( a ) = 1.7159   tanh ⁡ ( 2 3 a ) f(a) = 1.7159 \ \tanh(\frac{2}{3}a) f(a)=1.7159 tanh(32a)

tanh ⁡ ( x ) \tanh(x) tanh(x) 的计算在 scratch lenet(9): C语言实现tanh的计算7 有给出过, 使用的是高斯连分数公式做近似。

得到的 squashing function 的 C语言实现如下:

// compute hyperbolic tangent by Continued Fraction formula, found by Gauss in 1812.
// https://math.stackexchange.com/a/107295
static double m_tanh(double x)
{
    double s = x * x;
    double y = 9 + s / 11;
    y = 7 + s / y;
    y = 5 + s / y;
    y = 3 + s / y;
    y = 1 + s / y;
    y = x / y;
    return y;
}

/// f(a) = A tanh(Sa)
/// c.f. LeCun-98.pdf formula(6) in page 8
double squashing(double a)
{
    const double A = 1.7159;
    const double S = 2.0 / 3.0; /// c.f. LeCun-98.pdf page 41
    double res = A * m_tanh(S * a);
    return res;
}

在这里插入图片描述

References


  1. lecun-98.pdf ↩︎ ↩︎ ↩︎

  2. Approximation by Superpositions of a Sigmoidal Function ↩︎

  3. lecun-98b.pdf ↩︎

  4. Why is there a factor of 1.7159 with the tanh function used in neural network activation? ↩︎

  5. 论文笔记:Gradient-Based Learning Applied to Document Recognition ↩︎

  6. 論文筆記 - LeCun 1998 - Gradient-Based Learning Applied to Document Recognition ↩︎

  7. scratch lenet(9): C语言实现tanh的计算 ↩︎

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/680132.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

设计模式之观察者模式笔记

设计模式之观察者模式笔记 说明Observer(观察者)目录观察者模式示例类图抽象主题角色类抽象观察者类具体主题角色类具体的观察者角色类测试类 说明 记录下学习设计模式-观察者模式的写法。JDK使用版本为1.8版本。 Observer(观察者) 意图:定义对象间的一种一对多的依赖关系&a…

Gradle构建系统macOS安装与使用

1.打开gradle.org并点击安装 2.先决条件 ,确认安装JDK1.8或者更高版本已安装 在终端输入brew install gradle进行安装 安装成功如下: 查看安装版本号gradle -v 使用gradle 1.创建目录demo并进入该目录 mkdir demo cd demo 2.gradle init 使用Gradle开始构建 输入2开始构建应…

DevOps系列文章之 docker插件实现多实例部署(IDEA插件)

1. Docker的安装以及开启远程访问 1.1 安装 # 检查虚拟机内核版本,必须是3.10及以上 uname -r # 安装docker yum install docker # 输入y确认安装 # 启动docker systemctl start docker # 查看docker版本 docker -v # 开机启动docker systemctl enable docker # 停…

Golang学习日志 ━━ gin-vue-admin换机重新配置的记录,很愚蠢,很傻瓜,很机械...自己使用

最近一直在弄AI,没时间搞gva,所以有点忘记了,代码升级管它呢,全部重来一遍~ 一、备份保存 根据经验和个人喜好,我特别不喜欢在框架下把一个应用分散在module、api、service等等目录下,这种目录分配方案将把…

图上作业法

目录 交通示意图的表示方法 图上作业法 (1)对流 (2)迂回 物资调运问题的图上作业法 交通路线不成圈 交通路线成圈 交通示意图的表示方法 交通示意图是用来表明收发点的大致位置、收发量、交通路线长度的图形。 图形表示…

java mail发送、接收邮件

java mail接收邮件 1、引入java mail依赖 <dependency><groupId>org.eclipse.angus</groupId><artifactId>angus-mail</artifactId><version>2.0.2</version> </dependency>2、编写代码 注意&#xff1a;下述代码中的服务器…

从BNO055传感器获取IMU数据-2

在前面的文章 从BNO055传感器获取IMU数据-1 中介绍了BNO055传感器&#xff0c;今天继续讲解应用示例。 传感器与Arduino接口 我从某宝购买了固定在带有支持组件的开发板上的 BNO055 传感器。从 Digi-Key 或贸泽购买 BNO055 并将其焊接到 7.54.4mm 28 引脚 LGA 至 DIP 转换器上…

Groovy基础

Groovy基础 学Groovy有什么用&#xff1f;&#xff1f;&#xff1f;一、Groovy简介二、Mac安装Groovy1、使用homebrew安装或官网下载2、配置环境变量3、重新加载环境变量 二、Groov基本语法三、更多特性和扩展四、Groovy简单使用1、Groovy中的字符串及三大语句结构2、Groovy类与…

1带你入门MATLAB图像处理图像类型转换(附matlab程序)

1.简述 学习目标&#xff1a; 图像类型的转换 常用图像格式 图像格式&#xff1a;是存储图像采用的文件格式。不同的操作系统、不同的图像处理软件&#xff0c;所支持的图像格式都有可能不同。 在实际应用中经常会遇到的图像格式有&#xff1a;BMP、GIF、TIFF、PCX、JPEG、P…

FFmpeg5.0源码阅读——avformat_open_input

摘要&#xff1a;本文主要描述了FFmpeg中用于打开文件接口avformat_open_input的具体调用流程&#xff0c;详细描述了该接口被调用时所作的具体工作。   关键字&#xff1a;ffmpeg、avformat_open_input   注意&#xff1a;读者需要了解FFmpeg的基本使用流程&#xff0c;以…

力扣动态规划专题(五)子序列问题 不连续子序列与连续子序列 步骤及C++实现

文章目录 300.最长递增子序列674.最长连续递增子序列动态规划贪心算法 718. 最长重复子数组二维dp数组一维dp数组 1143.最长公共子序列1035.不相交的线53. 最大子序和动态规划贪心算法 300.最长递增子序列 步骤 确定dp数组以及下标的含义 dp[i]&#xff1a;i之前&#xff08;包…

【数据结构】单链表 创建 插入 删除 查找 完整代码

3.1 单链表 3.1.1 定义 注&#xff1a; 元素离散的分布在存储空间中&#xff0c;所以单链表是非随机存取的存储结构。 即不能直接找到表中某个特定的结点&#xff0c;需要从表头开始遍历&#xff0c;依次查找。 定义的代码 typedef struct LNode {ElemType data;//每个节点存放…

第三章 处理机调度与死锁

目录 一、调度的概念、层次 2.1 调度的基本概念 2.2 调度的三个层次 2.2.1 高级调度 2.2.2 低级调度 2.2.3 中级调度 2.2.3.1 进程的挂起态 2.2.4 三层调度的联系、对比 二、进程调度的时机、切换与过程、方式 2.1 进程调度的时机 2.2 进程调度的方式 2.2.1 非抢占…

计网复习题

一、单项选择题 OSI参考模型的物理层负责&#xff08;&#xff09;。 A&#xff0e;格式化报文 B&#xff0e;为数据选择通过网络的路由(网络层) C&#xff0e;定义连接到介质的特性 D&#xff0e;提供远程文件访问能力(应用层) 下列选项中&#xff0c;不属于网络体系结构中所…

常用git操作总结

文章目录 一、git 分支命名规范&#xff08;1&#xff09;master 主分支&#xff08;2&#xff09;develop 开发分支&#xff08;3&#xff09;feature 分支&#xff08;一般简写为feat&#xff09;&#xff08;4&#xff09;hotfix 分支&#xff08;一般简写为fix&#xff09;…

如何用canvas实现一个富文本编辑器

富文本编辑器相信大家都用过&#xff0c;相关的开源项目也很多&#xff0c;虽然具体的实现不一样&#xff0c;但是大部分都是使用DOM实现的&#xff0c;但其实还有一种实现方式&#xff0c;那就是使用HTML5的canvas&#xff0c;本文会带大家使用canvas简单实现一个类似Word的富…

分布式系统学习第四天 fastcgi学习

目录 1. Nginx作为web服务器处理请求 2. http协议复习 3. fastCGI 3.1 CGI 3.3 fastCGI和spawn-fcgi安装 3.4 nginx && fastcgi 3.5我的总结 其他知识点 1. Nginx作为web服务器处理请求 nginx不能处理动态请求 因此把请求发送给fastCGI对动态请求进行处理 静态…

区块链基础之密码学及安全技术

1.2 密码学及安全技术 1.2.1 密码学知识 1.2.1.1 Hash函数 Hash(哈希) 哈希函数是一类数学函数&#xff0c;可以在有限合理的时间内&#xff0c;将任意长度的消息压缩为 固定长度的输出值&#xff0c;并且是不可逆的。其输出值称为哈希值&#xff0c;也称为散列值。 哈希算法…

越权漏洞学习-做你做不了的事情

&#xff08;一&#xff09;、什么是越权漏洞 1、了解越权漏洞&#xff1a; 越权漏洞是指一个用户或者一个攻击者通过利用系统中某一漏洞&#xff0c;可以获得超过其正常权限的权限。也就是说&#xff0c;越权漏洞会使攻击者能够执行未经授权的操作或访问受保护的资源 简单来…