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

2024-09-27 00:17:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我的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++编写它,重新编译整个TysFooSur.在

如何使用TensorFlow中可用的函数来实现它?在


Tags: 函数tensorflowabscnn形式常量tanhtou
1条回答
网友
1楼 · 发布于 2024-09-27 00:17:43

在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)

相关问题 更多 >

    热门问题