【发布时间】:2019-07-13 22:47:26
【问题描述】:
我一直在尝试将平方非线性激活函数函数实现为 keras 模型的自定义激活函数。这是此列表中的第 10 个函数https://en.wikipedia.org/wiki/Activation_function。
我尝试使用 keras 后端,但我没有使用我需要的多个 if else 语句,所以我也尝试使用以下内容:
import tensorflow as tf
def square_nonlin(x):
orig = x
x = tf.where(orig >2.0, (tf.ones_like(x)) , x)
x = tf.where(0.0 <= orig <=2.0, (x - tf.math.square(x)/4), x)
x = tf.where(-2.0 <= orig < 0, (x + tf.math.square(x)/4), x)
return tf.where(orig < -2.0, -1, x)
如您所见,我需要评估 4 个不同的子句。但是当我尝试编译 Keras 模型时,我仍然得到错误:
Using a `tf.Tensor` as a Python `bool` is not allowed
有人可以帮我在 Keras 中使用它吗?非常感谢。
【问题讨论】:
-
我觉得你需要用
tf.greater(x, 2.0)等函数替换比较语句。 -
我已经尝试过了,它仍然给出了同样的错误。还有其他想法吗?
标签: tensorflow keras backend keras-layer activation-function