-->

如何使TensorFlow与Python分段激活功能?(How to make a piecewis

2019-10-29 08:36发布

我在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实现呢?

Answer 1:

在tensorflow很容易写自己的激活功能,如果它包括已经存在ops,作为你的情况下,你可以使用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)


文章来源: How to make a piecewise activation function with Python in TensorFlow?