且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在TensorFlow中使用Python制作分段激活函数?

更新时间:2023-11-23 21:50:58

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

In tensorflow it is easy to write your own activation function if it's include already existed ops, for your case you can use 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)