1. # -*- coding:utf-8 -*-
    2. from matplotlib import pyplot as plt
    3. import numpy as np
    4. import mpl_toolkits.axisartist as axisartist
    5. def sigmoid(x):
    6. return 1. / (1 + np.exp(-x))
    7. def tanh(x):
    8. return (np.exp(x) - np.exp(-x)) / (np.exp(x) + np.exp(-x))
    9. def relu(x):
    10. return np.where(x<0,0,x)
    11. def prelu(x):
    12. return np.where(x<0,0.5*x,x)
    13. def plot_sigmoid():
    14. x = np.arange(-10, 10, 0.1)
    15. y = sigmoid(x)
    16. fig = plt.figure()
    17. # ax = fig.add_subplot(111)
    18. ax = axisartist.Subplot(fig,111)
    19. ax.spines['top'].set_color('none')
    20. ax.spines['right'].set_color('none')
    21. # ax.spines['bottom'].set_color('none')
    22. # ax.spines['left'].set_color('none')
    23. ax.axis['bottom'].set_axisline_style("-|>",size=1.5)
    24. ax.spines['left'].set_position(('data', 0))
    25. ax.plot(x, y)
    26. plt.xlim([-10.05, 10.05])
    27. plt.ylim([-0.02, 1.02])
    28. plt.tight_layout()
    29. plt.savefig("sigmoid.png")
    30. plt.show()
    31. def plot_tanh():
    32. x = np.arange(-10, 10, 0.1)
    33. y = tanh(x)
    34. fig = plt.figure()
    35. ax = fig.add_subplot(111)
    36. ax.spines['top'].set_color('none')
    37. ax.spines['right'].set_color('none')
    38. # ax.spines['bottom'].set_color('none')
    39. # ax.spines['left'].set_color('none')
    40. ax.spines['left'].set_position(('data', 0))
    41. ax.spines['bottom'].set_position(('data', 0))
    42. ax.plot(x, y)
    43. plt.xlim([-10.05, 10.05])
    44. plt.ylim([-1.02, 1.02])
    45. ax.set_yticks([-1.0, -0.5, 0.5, 1.0])
    46. ax.set_xticks([-10, -5, 5, 10])
    47. plt.tight_layout()
    48. plt.savefig("tanh.png")
    49. plt.show()
    50. def plot_relu():
    51. x = np.arange(-10, 10, 0.1)
    52. y = relu(x)
    53. fig = plt.figure()
    54. ax = fig.add_subplot(111)
    55. ax.spines['top'].set_color('none')
    56. ax.spines['right'].set_color('none')
    57. # ax.spines['bottom'].set_color('none')
    58. # ax.spines['left'].set_color('none')
    59. ax.spines['left'].set_position(('data', 0))
    60. ax.plot(x, y)
    61. plt.xlim([-10.05, 10.05])
    62. plt.ylim([0, 10.02])
    63. ax.set_yticks([2, 4, 6, 8, 10])
    64. plt.tight_layout()
    65. plt.savefig("relu.png")
    66. plt.show()
    67. def plot_prelu():
    68. x = np.arange(-10, 10, 0.1)
    69. y = prelu(x)
    70. fig = plt.figure()
    71. ax = fig.add_subplot(111)
    72. ax.spines['top'].set_color('none')
    73. ax.spines['right'].set_color('none')
    74. # ax.spines['bottom'].set_color('none')
    75. # ax.spines['left'].set_color('none')
    76. ax.spines['left'].set_position(('data', 0))
    77. ax.spines['bottom'].set_position(('data', 0))
    78. ax.plot(x, y)
    79. plt.xticks([])
    80. plt.yticks([])
    81. plt.tight_layout()
    82. plt.savefig("prelu.png")
    83. plt.show()
    84. if __name__ == "__main__":
    85. plot_sigmoid()
    86. plot_tanh()
    87. plot_relu()
    88. plot_prelu()