【问题标题】:How to make a piecewise activation function with Python in TensorFlow?如何在 TensorFlow 中使用 Python 制作分段激活函数?
【发布时间】:2018-01-27 21:52:55
【问题描述】:

我的 CNN 中的活动函数具有以下形式:

abs(X)< tou  f = 1.716tanh(0.667x)
x >= tou     f = 1.716[tanh(2tou/3)+tanh'(2tou/3)(x-tou)]
x <= -tou    f = 1.716[tanh(-2tou/3)+tanh'(-2tou/3)(x+tou)]

tou 是一个常数。

因此,在 TensorFlow 中可以创建自己的激活函数。我不想用 C++ 编写它并重新编译整个 TensorFlow。

如何使用TensorFlow中提供的功能来实现?

【问题讨论】:

  • 这三个条件似乎不一致。例如,当 x==tou 时会发生什么?
  • 第三个条件是错误的。我再次编辑了它。请试一试。谢谢~

标签: python tensorflow neural-network activation-function


【解决方案1】:

在 tensorflow 中,如果它包含已存在的操作,则很容易编写自己的激活函数,对于您的情况,您可以使用 tf.case

f = tf.case({tf.less(tf.abs(x), tou): lambda: 7.716 * tf.tanh(0.667 * x),
         tf.greater_equal(x, tou): lambda: 1.716 * tf.tanh(2 * tou / 3) + 1.716 * tf.tanh(2 * tou / 3) * (x - tou)},
        default=lambda: 1.716 * tf.tanh(-2 * tou / 3) + 1.716 * tf.tanh(-2 * tou / 3) * (x + tou), exclusive=True)

【讨论】:

  • 第三个条件错误。我再次编辑了它。请看一看。谢谢~
猜你喜欢
  • 1970-01-01
  • 2020-12-07
  • 2022-01-01
  • 2019-04-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-11
  • 1970-01-01
相关资源
最近更新 更多