python 画sigmoid、tanh、relu和ELU等激活函数

 

1、前言

      写毕业论文时候画了激活函数的函数图,也是参考别人的代码做的,我将格式统一了一些,现在分享出来。

2、代码

      2.1 sigmoid和tanh

import math
import matplotlib.pyplot as plt
import numpy as np
import matplotlib as mpl
mpl.rcParams['axes.unicode_minus'] = False

def sigmoid(x):
    result = 1/(1+math.e**(-x))
    return result

fig = plt.figure(figsize=(10,4))
ax = fig.add_subplot(121)
x = np.linspace(-10,10)
y = sigmoid(x)

ax.spines['top'].set_color('none')  
ax.spines['right'].set_color('none')  

ax.xaxis.set_ticks_position('bottom')  
ax.spines['bottom'].set_position(('data',0))  
ax.set_xticks([-10,-5,0,5,10])  
ax.yaxis.set_ticks_position('left')  
ax.spines['left'].set_position(('data',0))  
ax.set_yticks([-1,-0.5,0.5,1])  

plt.plot(x,y,label = "Sigmoid",linestyle='-',color='blue')
plt.legend()

def tanh(x):
    #result = np.exp(x)-np.exp(-x)/np.exp(x)+np.exp(-x)
    result = (math.e**(x) - math.e**(-x)) / (math.e**(x) + math.e**(-x))
    return result

ax = fig.add_subplot(122)
x = np.linspace(-10,10)
y = tanh(x)

ax.spines['top'].set_color('none')  
ax.spines['right'].set_color('none')  

ax.xaxis.set_ticks_position('bottom')  
ax.spines['bottom'].set_position(('data',0)) 

ax.set_xticks([-10,-5,0,5,10])  
ax.yaxis.set_ticks_position('left')  
ax.spines['left'].set_position(('data',0))  
ax.set_yticks([-1,-0.5,0.5,1])  

plt.plot(x,y,label = "Tanh",linestyle='-',color='blue')
plt.legend()
plt.savefig('sigmoid and tanh.png',dpi=200)

      2.2 relu和ELU

import math
import matplotlib.pyplot as plt
import numpy as np
import matplotlib as mpl
mpl.rcParams['axes.unicode_minus'] = False

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

fig = plt.figure(figsize=(10,4))
ax = fig.add_subplot(121)
x = np.linspace(-10,10)
y = relu(x)

ax.spines['top'].set_color('none')  
ax.spines['right'].set_color('none')  

ax.xaxis.set_ticks_position('bottom')  
ax.spines['bottom'].set_position(('data',0))  
ax.set_xticks([-10,-5,0,5,10])  
ax.yaxis.set_ticks_position('left')  
ax.spines['left'].set_position(('data',0))  
ax.set_yticks([5,10])  

plt.plot(x,y,label = "Relu",linestyle='-',color='blue')
plt.legend()


def elu(x,alpha=1):
    a = x[x>0]
    b = alpha*(math.e**(x[x<0])-1)
    result=np.concatenate((b,a),axis=0)
    return result

ax = fig.add_subplot(122)
x = np.linspace(-10,10)
y = elu(x)

ax.spines['top'].set_color('none')  
ax.spines['right'].set_color('none')  

ax.xaxis.set_ticks_position('bottom')  
ax.spines['bottom'].set_position(('data',0))  
ax.set_xticks([-10,-5,0,5,10])  
ax.yaxis.set_ticks_position('left')  
ax.spines['left'].set_position(('data',0))  
ax.set_yticks([5,10])  

plt.plot(x,y,label = "ELU alpha=1",linestyle='-',color='blue')
plt.legend()
plt.savefig('Relu an ELU.png',dpi=200)

3、结果:

python 画sigmoid、tanh、relu和ELU等激活函数_第1张图片

python 画sigmoid、tanh、relu和ELU等激活函数_第2张图片

你可能感兴趣的:(Python)