当前位置: 首页 > news >正文

神经网络常见激活函数 14-Mish函数

文章目录

    • Mish
      • 函数+导函数
      • 函数和导函数图像
      • 优缺点
      • PyTorch 中的 Mish 函数
      • TensorFlow 中的 Mish 函数

Mish

  • 论文

    https://arxiv.org/pdf/1908.08681

函数+导函数

  • Mish函数
    Mish(x)=x⋅tanh⁡⁣(softplus(x))=x⋅tanh⁡⁣(ln⁡⁣(1+ex))\begin{aligned} \text{Mish}(x) &= x \cdot \tanh\!\bigl(\text{softplus}(x)\bigr) \\ &= x \cdot \tanh\!\Bigl(\ln\!\bigl(1+e^{x}\bigr)\Bigr) \end{aligned} Mish(x)=xtanh(softplus(x))=xtanh(ln(1+ex))

  • Mish函数导数

    已知:
    $$
    \frac{d}{dx}\tanh(x) =1- \rm tanh ^2(x) \[2mm]

    \frac{d}{dx}\operatorname{Softplus}(x)=\sigma(x)=\frac{1}{1+e^{-x}}
    $$
    参考:

    神经网络常见激活函数 2-tanh函数(双曲正切)

    则:
    $$
    \begin{aligned}
    \frac{\mathrm{d}}{\mathrm{d}x}\text{Mish}(x)
    &= x \cdot \tanh!\Bigl(\ln!\bigl(1+e^{x}\bigr)\Bigr)\

    &=\frac{\mathrm{d}}{\mathrm{d}x}x\cdot\tanh\bigl(\ln(1+e^{x})\bigr) + x \cdot \frac{\mathrm{d}}{\mathrm{d}x}\tanh\bigl(\ln(1+e^{x})\bigr) \[2mm]

    &=\tanh\bigl(\ln(1+e^{x})\bigr) + x \cdot\bigl(1-\tanh2(\ln(1+e{x})\bigr)\cdot\frac{1}{1+e^{-x}}\
    &=\tanh\bigl(\ln(1+e^{x})\bigr) + x \cdot\bigl(1-\tanh2(\ln(1+e{x})\bigr)\cdot\sigma(x)
    \end{aligned}
    $$


函数和导函数图像

  • 画图

    import numpy as np
    from matplotlib import pyplot as pltdef mish(x):"""Mish(x) = x * tanh(softplus(x))"""sp = np.log(1 + np.exp(x))          # softplus(x)return x * np.tanh(sp)def mish_derivative(x):"""Mish'(x) = tanh(softplus(x)) + x * (1 - tanh²(softplus(x))) * sigmoid(x)"""sp = np.log(1 + np.exp(x))          # softplus(x)t  = np.tanh(sp)                    # tanh(softplus(x))s  = 1 / (1 + np.exp(-x))           # sigmoid(x)return t + x  * (1 - t ** 2) * sx = np.linspace(-4, 4, 1000)
    y = mish(x)
    y1 = mish_derivative(x)plt.figure(figsize=(12, 8))
    ax = plt.gca()
    plt.plot(x, y, label='Mish')
    plt.plot(x, y1, label='Derivative', linestyle='--')
    plt.title('Mish Activation Function and its Derivative')ax.spines['right'].set_color('none')
    ax.spines['top'].set_color('none')
    ax.xaxis.set_ticks_position('bottom')
    ax.spines['bottom'].set_position(('data', 0))
    ax.yaxis.set_ticks_position('left')
    ax.spines['left'].set_position(('data', 0))plt.legend(loc='upper left')
    plt.savefig('./mish.jpg',dpi=300)
    plt.show()
    

    mish


优缺点

  • Mish 的优点

    1. 平滑无断点:Mish 函数在整个实数域内连续可导,有助于稳定的梯度流,缓解梯度消失问题。
    2. 非单调性:负半轴有一段“下凹再回升”的曲线,有助于梯度流动,提升网络的表达能力。
    3. 无上界正值:正值部分无饱和区,避免梯度消失,适合深层网络,有有下界(≈ −0.31)。
    4. 实验性能:在 ImageNet、COCO 等多个基准上,Mish 常优于 ReLU、Swish 等激活函数。(并非绝对)
  • Mish 的缺点

    1. 计算开销大:相比 ReLU,需要额外计算 softplus、tanh 与乘法,推理延迟略高。
    2. 显存占用:反向传播需缓存中间结果,显存开销高于 ReLU。
    3. 并非万能:在某些轻量级或实时任务中,性能提升可能无法抵消额外计算成本,需要实验验证。

PyTorch 中的 Mish 函数

  • 代码

    import torch
    import torch.nn.functional as F# 固定随机种子
    torch.manual_seed(1024)      # CPU
    if torch.cuda.is_available():torch.cuda.manual_seed_all(42)   # GPU,如果有x = torch.randn(2,dtype=torch.float32)
    mish_x = mish(x)print(f"x:\n{x}")
    print(f"mish_x:\n{mish_x}")
    """输出示例"""
    x:
    tensor([-1.4837,  0.2671])
    mish_x:
    [-0.29912564  0.18258688]
    

TensorFlow 中的 Mish 函数

  • 环境

    python: 3.10.9
    tensorflow: 2.19.0

  • 代码

    import tensorflow as tfdef mish(x):return x * tf.math.tanh(tf.math.softplus(x))# 生成随机张量
    x = tf.constant([-1.4837, 0.2671], dtype=tf.float32)
    mish_x = mish(x)print(f"x:\n{x.numpy()}")
    print(f"mish_x:\n{mish_x.numpy()}")"""输出示例"""
    x:
    [-1.4837  0.2671]
    mish_x:
    [-0.29912373  0.18255362]
    

http://www.dtcms.com/a/283634.html

相关文章:

  • AI学习笔记三十二:YOLOv8-CPP-Inference测试(Linux版本)
  • CDSS系统升级“可视化解释-智能反馈-临床语言“三位一体设计架构设计分析
  • 「Chrome 开发环境快速屏蔽 CORS 跨域限制详细教程」*
  • lua(xlua)基础知识点记录二
  • Oracle数据泵详解——让数据迁移像“点外卖”一样简单​
  • 数据库管理-第349期 Oracle DB 23.9新特性一览(20250717)
  • python与正则:前后向断言、分组,以及案例练习
  • Xss-labs 1-8关的初步通关
  • 【Linux系统】进程地址空间
  • 时序数据库选型指南 —— 为什么选择 Apache IoTDB?
  • Qt Quick 粒子系统详解
  • 数据呈现高阶技巧:散点图与桑基图的独特价值
  • 从零开始学 Linux 系统安全:基础防护与实战应用
  • 12.9 Mixtral-8x7B核心技术解密:如何用1/3参数实现4倍推理速度碾压LLaMA2?
  • 取消office word中的段落箭头标记
  • 多方案对比分析:后端数据加密策略及实践
  • 菜单权限管理
  • 【Linux】LVS(Linux virual server)
  • zabbix安装(docker-compose)
  • 若依框架开启注册功能全流程指南
  • I3C Host Adapter Pro+ (3)
  • 36.在列表或字典中查找匹配项
  • CrewAI与LangGraph:下一代智能体编排平台深度测评
  • 数据分析全流程指南:从明确目标到数据呈现的实操方法论
  • Kiro vs Cursor: AI IDE 终极对比指南
  • github不能访问怎么办
  • mac OS上docker安装zookeeper
  • 3t车用手动卧式千斤顶设计含8张CAD图纸PDF图
  • 有n棍棍子,棍子i的长度为ai,想要从中选出3根棍子组成周长尽可能长的三角形。请输出最大的周长,若无法组成三角形则输出0。
  • 优先队列的实现