用自定义激活代替Sigmoid激活 [英] Replacing sigmoid activation with custom activation

查看:92
本文介绍了用自定义激活代替Sigmoid激活的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am experimenting replacing the Keras sigmoid function with a piecewise linear function defined as:

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作为张量?预先感谢您提供的所有帮助.

I researched this and learned that, I am treating the variable x as a simple python variable whereas it is a tensor. That is the reason it cannot be treated like a simple boolean variable. I also tried using the tensorflow cond method. How to treat and use x as tensor here? Thanks a ton in advance for all the help.

推荐答案

您的自定义激活被编写为单个浮点数的函数,但是您希望将其应用于整个张量.最好的方法是使用tf.where.像

Your custom activation is written as a function of a single floating point number but you want to apply it to a whole tensor. The best way to do that is to use tf.where. Something like

def custom_activation_4(x):
  orig = x
  x = tf.where(orig < -6, tf.zeros_like(x), x)
  x = tf.where(orig >= -6 and orig < -4, (0.0078*x + 0.049), x)
  x = tf.where(orig >= -4 and orig < 0, (0.1205*x + 0.5), x)
  x = tf.where(orig >= 0 and orig < 4, (0.1205*x + 0.5), x)
  x = tf.where(orig  >= 4 and orig < 6, (0.0078*x + 0.951), x)
  return tf.where(orig >= 6, 1, x)

这篇关于用自定义激活代替Sigmoid激活的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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