几种基础的激活函数及其实现

几种基础的激活函数及其实现

说明:

神经元(Neuron)

以下为一个神经元:

z=w1x1+w2x2+w3x3+b

可以使用向量来表达:

z=wTx+b

  • x=[x1x2x3]: 输入(input)
  • w=[w1w2w3]: 权重(weights)
  • b:偏置(bias)

假设激活函数为f,那么输出y为:

y=f(x)

激活函数

Binary Threshold

y={1zk0z<k

def binary(z : np.array, k: float) -> np.array:
    """
    Function to execute the binary threshold activation
    
    Inputs:
        z : input dot product w*x + b
    Output:
        y : determined activation
    """
    return np.round(z >= k)
z = np.linspace(-4,4,num=100)

y = binary(z)

Sigmoid

y=1/(1+ez)

  • 如果z是一个很大的正数,那么 ez 趋近于 0, 然后 y 趋近于 1
  • 如果z是一个很大的负数,那么 ez 趋近于 无穷大, 然后 y 趋近于 0
  • 如果z=0,那么 ez=1,然后 y=12
def sigmoid(z : np.array) -> np.array:
    """
    Function to execute the sigmoid activation
    
    Inputs:
        z : input dot product w*x + b
    Output:
        y : determined activation
    """
    return 1/(1+np.exp(-z))
    
z = np.linspace(-5,5,num=100)
y = sigmoid(z)

sigmoid 激活函数常用于 binary classification problems

Softmax

Softmax激活函数适用于 Multiclass classification problems

如果有 k 个输出分类:

yi=ezij=1kezj

softmax是argmax函数的 smooth approximation

def softmax(z : np.array) -> np.array:
    """
    Function to execute the softmax activation
    
    Inputs:
        z : input dot product w*x + b
    Output:
        y : determined activation
    """
    return np.exp(z)/np.sum(np.exp(z))

ReLU

y={zz00z<0

def relu(z : np.array) -> np.array:
    """
    Function to execute the ReLU activation
    
    Inputs:
        z : input dot product w*x + b
    Output:
        y : determined activation
    """
    return np.where(z>=0,z,0)

z 为非正数时,输出y和梯度均为0,梯度为0会导致训练停止。

PReLU

y=max(0,z)+amin(0,z)

其中 a 是一个通过训练来学习的参数 (learnable parameter)。

相比于ReLU,即使 z 为负数,梯度也不会为0。

  • a=1y=|z|, 激活函数被称为 absolute value ReLU
  • a 为一个较小的正数,通常在 0.01 左右,激活函数被称为 leaky ReLU

Tanh

y=ezezez+ez

def tanh(z : np.array) -> np.array:
    """
    Function to execute the tanh activation
    
    Inputs:
        z : input dot product w*x + b
    Output:
        y : determined activation
    """
    return (np.exp(z) - np.exp(-z))/(np.exp(z)+np.exp(-z))
  • 当输入 z 是一个大的正数时, ez 趋近于0,yezez, 因此 y 趋近于 1
  • 当输入 z 是一个大的负数时, ez 趋近于0,yezez, 因此 y 趋近于 1
  • 当输入 z 为 0 时, ez=ez=1, 因此 y=0

SoftPlus

f(z)=loge(1+ez)

SoftPlus 可以看做是 ReLU 的 smooth approximation

dydz=f(z)=d(loge(1+ez))dzf(z)=ez1+ezf(z)=ezez1ez+ezezf(z)=11+ezf(z)=sigmoid(z)

其中应用了:

ddx(lnx)=1x

dydx=dydududx

另外:

f(z)=loge(1+ez)=log(1+ez)log(ez)+z=log(1+ezez)+z=log(1+ez)+z

def softplus(z : np.array) -> np.array:
    """
    Function to execute the softplus activation
    
    Inputs:
        z : input dot product w*x + b
    Output:
        y : determined activation
    """
    return np.log(1 + np.exp(-np.abs(z))) + np.maximum(z, 0)

Swish

f(z)=zsigmoid(z)=z(1+ez)

def swish(z : np.array) -> np.array:
    """
    Function to execute the swish activation
    
    Inputs:
        z : input dot product w*x + b
    Output:
        y : determined activation
    """
    return z/(1+np.exp(-z))
posted @   shizidushu  阅读(62)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
点击右上角即可分享
微信分享提示