Sigmoid、Tanh、ReLU、Leaky_ReLU、SiLU、Mish函数python实现

Sigmoid、Tanh、ReLU、Leaky_ReLU、SiLU、Mish函数

import numpy as np
import matplotlib.pyplot as plt

def Sigmoid(x):
    y = 1/(1+np.exp(-x))
    y = np.sum(y)
    print(y)
    
    # 画图
    xx = np.arange(-10, 10, 0.1)
    yy = 1/(1+np.exp(-xx))
    dy = 1/(1+np.exp(-xx))*(1-1/(1+np.exp(-xx)))

    plt.plot(xx, yy, label="Sigmoid")
    plt.plot(xx, dy, label="Sigmoid'")
    plt.xlabel("x")
    plt.ylabel("y")
    plt.legend()
    plt.show()


def Tanh(x):
    y = (np.exp(x)-np.exp(-x))/(np.exp(x)+np.exp(-x))
    y = np.sum(y)
    print(y)
    
    # 画图
    xx = np.arange(-10, 10, 0.1)
    yy = (np.exp(xx)-np.exp(-xx))/(np.exp(xx)+np.exp(-xx))
    dy = 1-((np.exp(xx)-np.exp(-xx))/(np.exp(xx)+np.exp(-xx)))**2
    
    plt.plot(xx, yy, label="Tanh")
    plt.plot(xx, dy, label="Tanh'")
    plt.xlabel("x")
    plt.ylabel("y")
    plt.legend()
    plt.show()


def ReLU(x):
    y = np.sum(x[x>0])
    print(y)
    
    # 画图
    xx = np.arange(-10, 10, 0.1)
    yy = np.where(xx>0, xx, 0)
    dy = np.where(xx>0, 1, 0)

    plt.plot(xx, yy, label="ReLU")
    plt.plot(xx, dy, label="ReLU'")
    plt.xlabel("x")
    plt.ylabel("y")
    plt.legend()
    plt.show()


def Leaky_ReLU(x):
    y = np.where(x>0, x, 0.01*x)
    y = np.sum(y)
    print(y)
    
    # 画图
    xx = np.arange(-10, 10, 0.1)
    yy = np.where(xx>0, xx, 0.01*xx)
    dy = np.where(xx>0, 1, 0.01)
    
    plt.plot(xx, yy, label="Leaky_ReLU")
    plt.plot(xx, dy, label="Leaky_ReLU'")
    plt.xlabel("x")
    plt.ylabel("y")
    plt.legend()
    plt.show()
    

def SiLU(x):
    y = x*(1/(1+np.exp(-x)))
    y = np.sum(y)
    
    # 画图
    xx = np.arange(-10,10,0.1)
    yy = xx*(1/(1+np.exp(-xx)))
    dy = 1/(1+np.exp(-xx))*(1+xx*(1-1/(1+np.exp(-xx))))
    
    plt.plot(xx, yy, label="SiLU")
    plt.plot(xx, dy, label="SiLU'")
    plt.xlabel("x")
    plt.ylabel("y")
    plt.legend()
    plt.show()
    
    # ReLU 对比   
    ReLU_y = np.where(xx>0, xx, 0)
    plt.plot(xx, yy, label="SiLU")
    plt.plot(xx, ReLU_y, label="ReLU")
    plt.xlabel("x")
    plt.ylabel("y")
    plt.legend()
    plt.show()
    
    # Sigmoid 对比  SiLU导数和Sigmoid激活函数
    Sigmoid_dy = 1/(1+np.exp(-xx))
    plt.plot(xx, dy, label="SiLU'")
    plt.plot(xx, Sigmoid_dy, label="Sigmoid'")
    plt.xlabel("x")
    plt.ylabel("y")
    plt.legend()
    plt.show()
    
    
def tanh(x):
    return (np.exp(x)-np.exp(-x))/(np.exp(x)+np.exp(-x))

def softplus(x):
    return np.log(1+np.exp(x))

def Mish(x):
    y = x*tanh(softplus(x))
    y = np.sum(y)
    print(y)

    # 画图
    xx = np.arange(-10, 10, 0.1)
    yy = xx*tanh(softplus(xx))

    Omega = 4*(xx+1)+4*np.exp(2*xx)+np.exp(3*xx)+(4*xx+6)*np.exp(xx)
    Delta = 2*np.exp(xx)+np.exp(2*xx)+2
    dy = (np.exp(xx)*Omega)/Delta**2

    plt.plot(xx, yy, label="Mish")
    plt.xlabel("x")
    plt.ylabel("y")
    plt.legend()
    plt.show()

    plt.plot(xx, dy, label="Mish'")
    plt.xlabel("x")
    plt.ylabel("y")
    plt.legend()
    plt.show()


if __name__ == "__main__":
    x = np.random.randn(10)
    print(x)
    Sigmoid(x)
    Tanh(x)
    ReLU(x)
    Leaky_ReLU(x)
    SiLU(x)
    Mish(x)
  • 3
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值