激活函数:logistic、Tanh、Relu、leaky relu、ELU的图像及python绘制代码

#绘制激活函数代码
import numpy as np
import matplotlib.pyplot as plt

# 定义激活函数

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

def tanh(x):
    return np.tanh(x)

def relu(x):
    return np.maximum(0, x)

def leaky_relu(x, alpha=0.01):
    return np.where(x >= 0, x, alpha * x)

def elu(x, alpha=1.0):
    return np.where(x >= 0, x, alpha * (np.exp(x) - 1))

# 绘制激活函数图像

x = np.linspace(-10, 10, 1000)

# Logistic
plt.figure(figsize=(6, 4))
plt.plot(x, logistic(x), label='Logistic')
plt.plot(x, tanh(x), label='Tanh')
#plt.title('Logistic Activation Function')
plt.title('Logistic & Tanh')
plt.xlabel('x')
plt.ylabel('f(x)')
plt.legend()
plt.grid(True)
plt.show()

# Tanh
plt.figure(figsize=(6, 4))
plt.plot(x, tanh(x), label='Tanh')
plt.title('Tanh Activation Function')
plt.xlabel('x')
plt.ylabel('f(x)')
plt.legend()
plt.grid(True)
plt.show()

# ReLU
plt.figure(figsize=(6, 4))
plt.plot(x, relu(x), label='ReLU')
plt.title('ReLU Activation Function')
plt.xlabel('x')
plt.ylabel('f(x)')
plt.legend()
plt.grid(True)
plt.show()

# Leaky ReLU
plt.figure(figsize=(6, 4))
plt.plot(x, leaky_relu(x), label='Leaky ReLU')
plt.title('Leaky ReLU Activation Function')
plt.xlabel('x')
plt.ylabel('f(x)')
plt.legend()
plt.grid(True)
plt.show()

# ELU
plt.figure(figsize=(6, 4))
plt.plot(x, elu(x), label='ELU')
plt.title('ELU Activation Function')
plt.xlabel('x')
plt.ylabel('f(x)')
plt.legend()
plt.grid(True)
plt.show()

 

 

 

 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值