import matplotlib.pyplot as plt import torch from matplotlib.pyplot import MultipleLocator import numpy as np import itertools import math import sys 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 一般都采用ReLU激活函数 def ReLU(x): return np.maximum(0, 0.1 * x) # ReLU函数的定义 # 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) y = [y1, y2, y3, y4, y5, y6] label = ['sigmoid', 'ReLU', 'tanh', 'SiLu', 'ELu', 'GELU'] # for index in zip(y, label): # print(index) # sys.exit(0) 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' ### 方式一 for i, j in itertools.zip_longest(y, label): plt.plot(x1, i, linewidth=2, label=j) ### 方式二 # 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=2, label='SiLU') # plt.plot(x1, y5, linewidth=2, label='ELU (α = {0})'.format(a)) # plt.plot(x1, y6, linestyle='--', linewidth=2, label='GELU (α = {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()