激活函数在同一figure上的实现

import matplotlib.pyplot as plt
import torch
from matplotlib.pyplot import MultipleLocator
import numpy as np
import math

plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False

# sigmoid激活函数用的最少,但为二分类问题时,可作为输出层,(即当x趋于无穷大或无穷小时会使得导数趋于0,会出现梯度消失)

def sigmoid(x):
    return 1 / (1 + np.exp(-x))
# ReLU  
def ReLU(x):
    return np.maximum(0, 0.1 * x)  

# tanh
def tanh(x):
    return (np.exp(x) - np.exp(-x)) / (np.exp(x) + np.exp(-x))
# SiLu
def SiLu(x):
    return x/ (1 + np.exp(-x))

def ELu(x1):
    # v = np.linspace(-10, 10, 1000)
    # a = 1
    return np.where(x1 >= 0, x1, 1 * (np.exp(x1) - 1))
    # p_ = np.where(x1 >= 0, 1, p + 1)

    # displayPlot(xlim=[-2, 2], ylim=[-1.1, 1.1])

def GELU(x1):
    # v = np.linspace(-10, 10, 1000)
    # a = 1
    return 0.5 * x1 * (1 + tanh(np.sqrt(2/np.pi) * (x1 + 0.044715 * np.power(x1, 3))))

a = 1
x1 = np.arange(-10.0, 10.0, 0.1) 
y1 = sigmoid(x1)
y2 = ReLU(x1)
y3 = tanh(x1)
y4 = SiLu(x1)
y5 = ELu(x1)
y6 = GELU(x1)

plt.rc('font', family='Times New Roman', size=13)  # 全局中英文为字体“罗马字体”
# 更改x,y坐标轴的尺度标量区间
plt.xlim(-10, 10)
plt.ylim(-1, 1)
# plt.rcParams['xtick.direction'] = 'in'
# plt.rcParams['ytick.direction'] = 'in'
plt.plot(x1, y1, linewidth=2, label='sigmoid')
plt.plot(x1, y2, linewidth=2, label='ReLU')
plt.plot(x1, y3, linewidth=2, label='tanh')
plt.plot(x1, y4, linewidth=3, label='SiLU')
plt.plot(x1, y5, linewidth=3, label='ELU (α = {0})'.format(a))
plt.plot(x1, y6, linestyle='--', linewidth=3, label='GELU (α = {0})'.format(a))
# plt.plot(x1, Y, linestyle='--', label='Ours (α = {0})'.format(a))
# plt.plot(x1, y5_prime, '--', label='derivada')
plt.xlabel('X1')
plt.ylabel('Scores of Diff-act')
plt.grid(True)
plt.legend(loc="lower right")
"""去掉图上边框,与右边框"""
ax = plt.gca()
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
plt.show()

激活函数在同一figure上的实现_第1张图片

你可能感兴趣的:(python,开发语言)