Python的实现如下:
1 import numpy as np 2 import matplotlib.pyplot as plt 3 4 5 def sigmoid(x): 6 y = 1.0 / (1.0 + np.exp(-x)) 7 return y 8 9 10 def elu(x, a): 11 y = x.copy() 12 for i in range(y.shape[0]): 13 if y[i] < 0: 14 y[i] = a * (np.exp(y[i]) - 1) 15 return y 16 17 18 def lrelu(x, a): 19 y = x.copy() 20 for i in range(y.shape[0]): 21 if y[i] < 0: 22 y[i] = a * y[i] 23 return y 24 25 26 def relu(x): 27 y = x.copy() 28 y[y < 0] = 0 29 return y 30 31 32 def softplus(x): 33 y = np.log(np.exp(x) + 1) 34 return y 35 36 37 def softsign(x): 38 y = x / (np.abs(x) + 1) 39 return y 40 41 42 def tanh(x): 43 y = (1.0 - np.exp(-2 * x)) / (1.0 + np.exp(-2 * x)) 44 return y 45 46 47 x = np.linspace(start=-10, stop=10, num=100) 48 y_sigmoid = sigmoid(x) 49 y_elu = elu(x, 0.25) 50 y_lrelu = lrelu(x, 0.25) 51 y_relu = relu(x) 52 y_softplus = softplus(x) 53 y_softsign = softsign(x) 54 y_tanh = tanh(x) 55 56 tx = 6 57 ty = 0.9 58 59 plt.subplot(331) 60 plt.title('sigmoid') 61 plt.plot(x, y_sigmoid) 62 plt.grid(True) 63 plt.subplot(332) 64 plt.title('elu') 65 plt.plot(x, y_elu) 66 plt.grid(True) 67 plt.subplot(333) 68 plt.title('lrelu') 69 plt.plot(x, y_lrelu) 70 plt.grid(True) 71 plt.subplot(334) 72 plt.title('relu') 73 plt.plot(x, y_relu) 74 plt.grid(True) 75 plt.subplot(335) 76 plt.title('softplus') 77 plt.plot(x, y_softplus) 78 plt.grid(True) 79 plt.subplot(336) 80 plt.title('softsign') 81 plt.plot(x, y_softsign) 82 plt.grid(True) 83 plt.subplot(337) 84 plt.title('tanh') 85 plt.plot(x, y_tanh) 86 plt.grid(True) 87 plt.show()