Sigmoid,ReLu,Tanh激活函数及其导数Python详解


 

import numpy as np
import matplotlib.pyplot as plt

inputs = np.arange(-10,10,0.1)
def sigmoid(x):
     return 1.0/(1+np.exp(-x))

sigmoid_outputs = sigmoid(inputs)

plt.subplot(321)
plt.plot(inputs,sigmoid_outputs)
plt.xlabel("Sigmoid_Inputs")
plt.ylabel("Sigmoid_Outputs")
#plt.show()

def sigmoid_deriv(x):
    return  (1.0/(1 + np.exp(-x))) * (1 - ( 1.0/(1+np.exp(-x))))
plt.subplot(322)
sigmoid_de_outputs=sigmoid_deriv(inputs)
plt.plot(inputs,sigmoid_de_outputs)
plt.xlabel("Sigmoid_de_Inputs")
plt.ylabel("Sigmoid_de_Outputs")



def ReLU(x):
    x2 = (np.abs(x) + x) / 2.0
    return x2

ReLU_outputs = ReLU(inputs)
plt.subplot(323)
plt.plot(inputs,ReLU_outputs)
plt.xlabel("ReLU_inputs")
plt.ylabel("ReLU_outputs")
#plt.show()

def ReLU_deriv(x):
    return np.where(x > 0, 1, 0)

ReLU_de_outputs = ReLU_deriv(inputs)
plt.subplot(324)
plt.plot(inputs,ReLU_de_outputs)
plt.xlabel("ReLU_de_inputs")
plt.ylabel("ReLU_de_outputs")


def Tanh(x):
    return (np.exp(x) - np.exp(-x))/(np.exp(-x) + np.exp(x))

Tanh_outputs =  Tanh(inputs)
plt.subplot(325)
plt.plot(inputs,Tanh_outputs)
plt.xlabel("Tanh_inputs")
plt.ylabel("Tanh_outputs")
#plt.show()

def Tanh_deriv(x):
    y = (np.exp(x) - np.exp(-x))/(np.exp(-x) + np.exp(x))
    return 1 - y * y

Tanh_de_outputs=Tanh_deriv(inputs)
plt.subplot(326)
plt.plot(inputs,Tanh_de_outputs)
plt.xlabel("Tanh_de_inputs")
plt.ylabel("Tanh_de_outputs")
plt.show()

 

Results:


 

  • 3
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值