Guest User

Untitled

a guest
Dec 14th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.57 KB | None | 0 0
  1. ################################
  2. # 激活函數 + matplotlib圖
  3. ################################
  4. import numpy as np
  5. import matplotlib.pylab as plt
  6. import tensorflow as tf
  7.  
  8. # 創建輸入數據
  9. # (-7, 7)之間的等間隔的180個點
  10. x = np.linspace(-7, 7, 180)
  11.  
  12.  
  13. # 激動函數的原始實現
  14. def sigmoid(inputs):
  15. y = [1 / float(1 + np.exp(-x)) for x in inputs]
  16. return y
  17.  
  18.  
  19. def relu(inputs):
  20. y = [x * (x > 0) for x in inputs]
  21. return y
  22.  
  23.  
  24. def tanh(inputs):
  25. y = [(np.exp(x) - np.exp(-x)) / float(np.exp(x) + np.exp(-x))
  26. for x in inputs]
  27. return y
  28.  
  29.  
  30. def softplus(inputs):
  31. y = [np.log(1 + np.exp(x)) for x in inputs]
  32. return y
  33.  
  34.  
  35. # 經過TensorFlow的激活函數處理的各個y值
  36. y_sigmoid = tf.nn.sigmoid(x)
  37. y_relu = tf.nn.relu(x)
  38. y_tanh = tf.nn.tanh(x)
  39. y_softplus = tf.nn.softplus(x)
  40.  
  41. # 創建會話
  42. with tf.Session() as sess:
  43. y_sigmoid, y_relu, y_tanh, y_softplus = sess.run(
  44. [y_sigmoid, y_relu, y_tanh, y_softplus])
  45. print(y_sigmoid)
  46. print(y_relu)
  47. print(y_tanh)
  48. print(y_softplus)
  49.  
  50. # 創建各個激活函數的圖像
  51. plt.subplot(221)
  52. plt.plot(x, y_sigmoid, c='red', label='Sigmoid')
  53. plt.ylim(-0.2, 1.2)
  54. plt.legend(loc='best')
  55.  
  56. plt.subplot(222)
  57. plt.plot(x, y_relu, c='red', label='Relu')
  58. plt.ylim(-1, 6)
  59. plt.legend(loc='best')
  60.  
  61. plt.subplot(223)
  62. plt.plot(x, y_tanh, c='red', label='Tanh')
  63. plt.ylim(-1.3, 1.3)
  64. plt.legend(loc='best')
  65.  
  66. plt.subplot(224)
  67. plt.plot(x, y_softplus, c='red', label='Softplus')
  68. plt.ylim(-1, 6)
  69. plt.legend(loc='best')
  70.  
  71. # 顯示圖像
  72. plt.show()
Add Comment
Please, Sign In to add comment