• 当激活值大于0时为1,否则为0
  • 由于导数在 x=0 时,不连续,所以不可以用于梯度下降训练
  1. # Customize an threshold activation function
  2. def threshold(x):
  3. cond = tf.less(x, tf.zeros(tf.shape(x), dtype=x.dtype))
  4. out = tf.where(cond, tf.zeros(tf.shape(x)), tf.ones(tf.shape(x)))
  5. return out
  6. # plot
  7. x = np.linspace(-1, 1, 50)
  8. out = threshold(x)
  9. tf.compat.v1.disable_eager_execution()
  10. sess = tf.compat.v1.Session()
  11. with tf.compat.v1.Session() as sess:
  12. y = sess.run(tf.constant(out))
  13. plt.xlabel(\'X\')
  14. plt.ylabel(\'Y\')
  15. plt.title(\'Threshold Activation Function\')
  16. plt.plot(x, y)
  17. plt.show()

Graph

  • tensorflow内置了 tf.sigmoid()实现
  • 输出值范围 (0, 1)
  • 该函数在两端导数为趋近于0,故会出现梯度消失(梯度弥散现象)问题,使得样本训练和优化变得越发困难
  1. # plot
  2. x = np.linspace(-10, 10, 50)
  3. out = tf.sigmoid(x)
  4. tf.compat.v1.disable_eager_execution()
  5. sess = tf.compat.v1.Session()
  6. with tf.compat.v1.Session() as sess:
  7. y = sess.run(tf.constant(out))
  8. plt.xlabel(\'X\')
  9. plt.ylabel(\'Y\')
  10. plt.title(\'Sigmoid Activation Function\')
  11. plt.plot(x, y)
  12. plt.show()

Graph

  • 负值输入为0,正值输入时,输出值和输入值相同

  • 负值输入为0的特性(稀疏激活)使得计算量大大减少

  • 避免了梯度弥散,是大部分网络模型的首选激活函数

  1. # plot
  2. x = np.linspace(-10, 10, 50)
  3. out = tf.nn.relu(x)
  4. tf.compat.v1.disable_eager_execution()
  5. sess = tf.compat.v1.Session()
  6. with tf.compat.v1.Session() as sess:
  7. y = sess.run(tf.constant(out))
  8. plt.xlabel(\'X\')
  9. plt.ylabel(\'Y\')
  10. plt.title(\'Sigmoid Activation Function\')
  11. plt.plot(x, y)
  12. plt.show()

Graph

  • 函数输出值范围 [0, 1]
  • 所有输出值和为1,故改激活函数被广泛用于分类任务网络模型的输出层
  1. # plot
  2. x = np.linspace(-10, 10, 50)
  3. out = tf.nn.softmax(x)
  4. tf.compat.v1.disable_eager_execution()
  5. sess = tf.compat.v1.Session()
  6. with tf.compat.v1.Session() as sess:
  7. y = sess.run(tf.constant(out))
  8. plt.xlabel(\'X\')
  9. plt.ylabel(\'Y\')
  10. plt.title(\'Softmax Activation Function\')
  11. plt.plot(x, y)
  12. plt.show()

Graph

  • ReLU函数由于其函数特性,而具有单侧抑制性,同时避免了梯度弥散的出现,因此在多层网络模型中都表现良好,所以在深层网络模型中,最常用的激活函数还是ReLU

版权声明:本文为xpengp原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/xpengp/p/14049702.html