前后端分离体育馆使用预约平台系统|SpringBoot+Vue+MyBatis+MySQL完整源码+部署教程
2026/8/9 4:36:29
// 激活函数.h —— 激活函数(SiLU/Sigmoid/Softplus)声明// 用途:门控 RMSNorm 与 SwiGLU 等算子依赖的标量激活函数,以及逐元素向量版本#pragmaonce// 引入基础类型(浮点别名)#include"公共/基础定义.h"// 激活SiLU:Sigmoid Linear Unit(Swish 的 β=1 特例)// 公式:$$ \mathrm{SiLU}(x) = \frac{x}{1 + e^{-x}} $$// 纯文本:SiLU(x) = x / (1 + e^(-x))// 含义:x 接近 0 时非线性过渡,负值渐近于 0(非饱和),正值近似恒等;// 用于 SwiGLU 的门控分支与门控 RMSNorm 的门控因子浮点 激活SiLU(浮点 x);// 激活Sigmoid:逻辑斯蒂函数// 公式:$$ \sigma(x) = \frac{1}{1 + e^{-x}} $$// 纯文本:Sigmoid(x) = 1 / (1 + e^(-x))// 含义:输出压缩到 (0,1),用于概率与门控信号的平滑映射浮点 激活Sigmoid(浮点 x);// 激活Softplus:平滑 ReLU(ReLU 的可微近似)// 公式:$$ \mathrm{softplus}(x) = \ln(1 + e^{x}) $$// 纯文本:Softplus(x) = ln(1 + e^x)// 数值稳定实现:x > 20 ? x : log1p(exp(x)),避免大 x 时 exp(x) 上溢浮点 激活Softplus(浮点 x);// 激活SiLU向量:对一段向量逐元素施加 激活SiLU// 参数:输入 = 源向量起点;输出 = 目标向量起点(可与 输入 相同实现就地,调用方传独立缓冲即可)// 数量 = 要处理的元素个数void激活SiLU向量(constfloat*输入,float*输出,size_t 数量);// 激活函数.cpp —— 激活函数(SiLU/Sigmoid/Softplus)实现// 用途:为归一化算子与后续 SwiGLU 提供标量与向量激活函数#include"内核/归一化/激活函数.h"// 引入标准头:指数 exp 与对数 log1p(数值稳定 Softplus)#include<cmath>// 激活SiLU:Sigmoid Linear Unit// 公式:SiLU(x) = x / (1 + e^(-x))// 实现:直接按公式用 exp 计算,x 为负值时 exp(-x) 增大但仍在浮点范围内浮点 激活SiLU(浮点 x){returnx/(1.0f+std::exp(-x));}// 激活Sigmoid:逻辑斯蒂函数// 公式:Sigmoid(x) = 1 / (1 + e^(-x))浮点 激活Sigmoid(浮点 x){return1.0f/(1.0f+std::exp(-x));}// 激活Softplus:平滑 ReLU// 公式:Softplus(x) = ln(1 + e^x)// 数值稳定:x > 20 时 e^x 已远超浮点精度,直接返回 x(ln(e^x)=x),避免上溢浮点 激活Softplus(浮点 x){if(x>20.0f){returnx;}// log1p(y) = ln(1+y),比 log(1+y) 更精确(避免 1+e^x 灾难性消减)returnstatic_cast<浮点>(std::log1p(std::exp(x)));}// 激活SiLU向量:逐元素施加 激活SiLUvoid激活SiLU向量(constfloat*输入,float*输出,size_t 数量){for(size_t i=0;i<数量;++i){输出[i]=激活SiLU(输入[i]);}}