四种常见的激活函数的Python绘制

原文:https://blog.csdn.net/qq_33039859/article/details/79520092

四种常见的激活函数的Python绘制_第1张图片

# -*- coding: utf-8 -*-
"""
Created on Sun Mar 11 20:41:57 2018

@author: brucelau
"""

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(-10,10)
y_sigmoid = 1/(1+np.exp(-x))
y_tanh = (np.exp(x)-np.exp(-x))/(np.exp(x)+np.exp(-x))

fig = plt.figure()
# plot sigmoid
ax = fig.add_subplot(221)
ax.plot(x,y_sigmoid,label="Sigmoid", color="blue")
ax.set_ylim((-2,2))
ax.grid()
ax.set_title('(a) Sigmoid')

# plot tanh
ax = fig.add_subplot(222)
ax.plot(x,y_tanh,label="Tanh", color="red")
ax.set_ylim((-2,2))
ax.grid()
ax.set_title('(b) Tanh')

# plot relu
ax = fig.add_subplot(223)
y_relu = np.array([0*item  if item<0 else item for item in x ])
ax.plot(x,y_relu,label="ReLu", color="blue")
ax.grid()
ax.set_title('(c) ReLu')

#plot leaky relu
ax = fig.add_subplot(224)
y_relu = np.array([0.2*item  if item<0 else item for item in x ])
ax.plot(x,y_relu,label="Leaky ReLu", color="red")
ax.grid()
ax.set_title('(d) Leaky ReLu')

plt.tight_layout()
plt.show()

你可能感兴趣的:(python,深度学习基础)