如何在TensorFlow中使用Python制作分段激活函数? [英] How to make a piecewise activation function with Python in TensorFlow?

查看:398
本文介绍了如何在TensorFlow中使用Python制作分段激活函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们的CNN中的活动函数的格式为:

The active function in my CNN has the form:

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.

So, in TensorFlow it is possible to make your own activation function. I don't want to write it in C++ and recompile the whole of TensorFlow.

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

How can I use the function available in TensorFlow to achieve it?

推荐答案

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

这篇关于如何在TensorFlow中使用Python制作分段激活函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆