用自定义激活替换 sigmoid 激活

我正在尝试用定义为的分段线性函数替换 Keras sigmoid 函数:


def custom_activation_4(x):

if x < -6:

    return 0

elif x >= -6 and x < -4:

    return (0.0078*x + 0.049)

elif x >= -4 and x < 0:

    return (0.1205*x + 0.5)

elif x >= 0 and x < 4:

    return (0.1205*x + 0.5)

elif x >= 4 and x < 6:

    return (0.0078*x + 0.951)

else:

    return 1;

当我尝试将其运行为:


classifier_4.add(Dense(output_dim = 18, init = 'uniform', activation = custom_activation_4, input_dim = 9))

编译器抛出一个错误说:


Using a `tf.Tensor` as a Python `bool` is not allowed.

我对此进行了研究并了解到,我将变量 x 视为一个简单的 python 变量,而它是一个张量。这就是它不能被视为一个简单的布尔变量的原因。我也尝试使用tensorflow cond方法。这里如何处理和使用 x 作为张量?非常感谢您提供的所有帮助。


哆啦的时光机
浏览 224回答 2
2回答
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python