激活函数公式、导数、图像笔记

绘图代码

import numpy as np
import matplotlib
import matplotlib.pyplot as plt
import torch
import torch.nn as nn
import torch.nn.functional as F


def draw_ax(ax, x, y, title):
    ax.plot(x, y)
    ax.spines['right'].set_color('none')  # 去掉右边的边框线
    ax.spines['top'].set_color('none')  # 去掉上边的边框线
    # 移动下边边框线,相当于移动 X 轴
    ax.xaxis.set_ticks_position('bottom')
    ax.spines['bottom'].set_position(('data', 0))
    # 移动左边边框线,相当于移动 y 轴
    ax.yaxis.set_ticks_position('left')
    ax.spines['left'].set_position(('data', 0))
    plt.title(title, fontsize=15)
    plt.grid(True)


X = np.arange(-20, 20, 0.05)
x1 = torch.from_numpy(X).requires_grad_(True)

# title = 'Sigmoid'
# y1 = torch.sigmoid(x1)
# title = 'tanh'
# y1 = torch.tanh(x1)
# title = 'ReLU'
# y1 = torch.relu(x1)
# title = 'Leaky ReLU'
# y1 = F.leaky_relu(x1, 0.1)
# title = 'Swish'
# y1 = x1 * torch.sigmoid(0.1*x1)
# title = 'Mish'
# y1 = x1 * F.softplus(x1).tanh()
title = 'SiLU'
y1 = F.silu(x1)

y2 = torch.autograd.grad(outputs=y1, inputs=x1, grad_outputs=torch.ones_like(x1))

Y1 = y1.detach().numpy()
Y2 = y2[0].detach().numpy()

matplotlib.rc('figure', figsize=(24, 7))
ax1 = plt.subplot(1, 2, 1)
draw_ax(ax1, X, Y1, title)
ax2 = plt.subplot(1, 2, 2)
draw_ax(ax2, X, Y2, f'Derivative of {title}')

# ax1.plot(X, Y1, label=r'$\beta = 0.1$')
# plt.show()
plt.savefig('SiLU.jpg')

1. Sigmoid

公式: S i g m o i d ( x ) = 1 1 + e − x {\rm{Sigmoid}}(x)=\frac{1}{1+e^{-x}} Sigmoid(x)=1+ex1
导数: S i g m o i d ′ ( x ) = S i g m o i d ( x ) ( 1 − S i g m o i d ( x ) ) {\rm{Sigmoid}}^{\prime}(x)={\rm{Sigmoid}}(x)(1-{\rm{Sigmoid}}(x)) Sigmoid(x)=Sigmoid(x)(1Sigmoid(x))

在这里插入图片描述

2. tanh

公式: t a n h ( x ) = e x − e − x e x + e − x {\rm{tanh}}(x)=\frac{e^x-e^{-x}}{e^x+e^{-x}} tanh(x)=ex+exexex
导数: t a n h ′ ( x ) = 1 − t a n h 2 ( x ) {\rm{tanh}}^{\prime}(x)=1-{\rm{tanh}}^2(x) tanh(x)=1tanh2(x)

在这里插入图片描述

3. ReLU

公式: R e L U ( x ) = m a x ( 0 , x ) {\rm{ReLU}}(x)={\rm{max}}(0,x) ReLU(x)=max(0,x)
导数:
R e L U ′ ( x ) = { 1 , if  x > 0 0 , if  x < 0 {\rm{ReLU}}^{\prime}(x)= \begin{cases} 1, & \text{if $x>0$} \\ 0, & \text{if $x<0$} \end{cases} ReLU(x)={1,0,if x>0if x<0

在这里插入图片描述

4. Leaky ReLU

公式: L e a k y R e L U ( x ) = m a x ( 0 , x ) + n e g a t i v e _ s l o p e ∗ m i n ( 0 , x ) {\rm{LeakyReLU}}(x)={\rm{max}}(0,x)+ {\rm{negative\_slope}}*{\rm{min}}(0,x) LeakyReLU(x)=max(0,x)+negative_slopemin(0,x)
导数:
L e a k y R e L U ′ ( x ) = { 1 , if  x > 0 n e g a t i v e _ s l o p e , if  x < 0 {\rm{LeakyReLU}}^{\prime}(x)= \begin{cases} 1, & \text{if $x>0$} \\ {\rm{negative\_slope}}, & \text{if $x<0$} \end{cases} LeakyReLU(x)={1,negative_slope,if x>0if x<0

在这里插入图片描述

5. Swish

公式: S w i s h ( x ) = x ∗ S i g m o i d ( β x ) {\rm{Swish}}(x)=x*{\rm{Sigmoid}}(\beta x) Swish(x)=xSigmoid(βx)

class Swish(nn.Module):
    def forward(self, x):
        return x * torch.sigmoid(x)

在这里插入图片描述

6. Mish

公式: M i s h ( x ) = x ∗ t a n h ( l n ( 1 + e x ) ) {\rm{Mish}}(x)=x*{\rm{tanh}}(ln(1+e^x)) Mish(x)=xtanh(ln(1+ex))

class Mish(nn.Module):
    def forward(self, x):
        return x * F.softplus(x).tanh()

在这里插入图片描述

7. SiLU

公式: S i L U ( x ) = x ∗ S i g m o i d ( x ) {\rm{SiLU}}(x)=x*{\rm{Sigmoid}}(x) SiLU(x)=xSigmoid(x)
在这里插入图片描述

8. FReLU

FReLU 是专门为视觉任务设计的激活函数,将ReLU和PReLU扩展为2D激活函数,论文地址

公式:
f ( x c , i , j ) = max ⁡ ( x c , i , j , T ( x c , i , j ) ) T ( x c , i , j ) = x c , i , j ω ⋅ p c ω x c , i , j ω ⋅ p c ω = ∑ i − 1 ≤ h ≤ i + 1 , j − 1 ≤ w ≤ j + 1 x c , h , w ⋅ p c , h , w \begin{array}{c} f\left(x_{c, i, j}\right)=\max \left(x_{c, i, j}, \mathbb{T}\left(x_{c, i, j}\right)\right) \\ \\ \mathbb{T}\left(x_{c, i, j}\right)=x_{c, i, j}^{\omega} \cdot p_{c}^{\omega} \\ \\ x_{c, i, j}^{\omega} \cdot p_{c}^{\omega}=\sum\limits_{i-1 \leq h \leq i+1, j-1 \leq w \leq j+1} x_{c, h, w} \cdot p_{c, h, w} \end{array} f(xc,i,j)=max(xc,i,j,T(xc,i,j))T(xc,i,j)=xc,i,jωpcωxc,i,jωpcω=i1hi+1,j1wj+1xc,h,wpc,h,w
其中, x c , i , j x_{c, i, j} xc,i,j 代表需要激活的像素, c , i , j c,i,j c,i,j 对应 channel 和 2D 位置。

公式较为晦涩抽象,看论文中的图比较直观:在这里插入图片描述
实际上就是将与 x x x 比大小的数 ( 0 , p x ) (0,px) (0,px) 替换为以 x x x 为中心的一个 3 × 3 3\times 3 3×3 卷积值,边角部分 padding 1。
为了防止混淆个人将其叫做激活核。
激活核的参数用高斯初始化,参与网络训练,对于 C C C 个通道的卷积输出,若用 3 × 3 3 \times 3 3×3 大小的激活核进行激活,则额外需要训练 C × 3 × 3 C \times 3 \times 3 C×3×3 个参数。文中尝试过平均池化和最大池化,都没有带参数的卷积效果好。

  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值