keras + tensorflow中的高级自定义激活功能

时间:2018-03-21 17:16:05

标签: python tensorflow keras activation-function

def newactivation(x):
    if x>0:
        return K.relu(x, alpha=0, max_value=None)
    else :
        return x * K.sigmoid(0.7* x)

get_custom_objects().update({'newactivation': Activation(newactivation)})

我正在尝试将此激活功能用于我的keras模型,但我很难找到要替换的内容

if x>0:

我得到的错误:

  

文件   “/usr/local/lib/python3.4/dist-packages/tensorflow/python/framework/ops.py”   第614行,在布尔中       引发TypeError(“不允许使用tf.Tensor作为Python bool。”

     

TypeError:不允许使用tf.Tensor作为Python bool。使用if >t is not None:代替if t:来测试是否定义了张量,并且>使用TensorFlow操作(例如tf.cond)来执行以>张量值为基础的子图。

有人能说清楚吗?

4 个答案:

答案 0 :(得分:4)

if x > 0没有意义,因为x > 0是张量,而不是布尔值。

要在Keras中使用keras.backend.switch执行条件语句。

例如你的

if x > 0:
   return t1
else:
   return t2

会变成

keras.backend.switch(x > 0, t1, t2)

答案 1 :(得分:3)

尝试类似:

<div class="package">
  <!-- card -->
  <div class="card-left">
    <div class="col-img">
      <img src="https://www.box.com/sites/default/files/img/social/box1200x630.jpg"> </div>
    <div class="col-content">
      <h3>Fake Room Package for Testing</h3>
      <h5> This is a custom headline! </h5>
      <p> </p>
      <p>Spicy jalapeno bacon ipsum dolor amet andouille t-bone tail, shankle meatloaf jowl jerky. Tongue shank cupim, sirloin flank pork chop capicola. Short ribs short loin doner corned beef pastrami. Leberkas beef jowl venison burgdoggen pork chop chuck
        pork belly bresaola turducken ham hock sirloin kevin porchetta rump. Alcatra shank pig tenderloin, cow meatloaf strip steak prosciutto. Ground round meatball fatback shankle strip steak pork chop shank cupim tail beef alcatra beef ribs.</p>
      <p></p> <a class="btn purple" href="#" target="_blank"><span>Check Availability</span></a>
    </div>
  </div>
</div>
<div class="package">
  <div class="card-left">
    <div class="col-img">
      <img src="https://www.box.com/sites/default/files/img/social/box1200x630.jpg"> </div>
    <div class="col-content">
      <h3>Fake Room Package for Testing</h3>
      <h5> This is a custom headline! </h5>
      <p> </p>
      <p>Spicy jalapeno bacon ipsum dolor amet andouille t-bone tail, shankle meatloaf jowl jerky. Tongue shank cupim, sirloin flank pork chop capicola. Short ribs short loin doner corned beef pastrami. Leberkas beef jowl venison burgdoggen pork chop chuck
        pork belly bresaola turducken ham hock sirloin kevin porchetta rump. Alcatra shank pig tenderloin, cow meatloaf strip steak prosciutto. Ground round meatball fatback shankle strip steak pork chop shank cupim tail beef alcatra beef ribs.</p>
      <p></p> <a class="btn purple" href="#" target="_blank"><span>Check Availability</span></a>
    </div>
  </div>
</div>

x不是python变量,它是一个Tensor,它将在模型运行时保存一个值。只有在评估op时才知道x的值,因此需要通过TensorFlow(或Keras)来评估条件。

答案 2 :(得分:0)

您可以评估张量,然后检查条件

    while(True):
        date=input('\nEnter Date (YYYYMMDD) : ')
        if(date=='0'):
            break
        ...#your work here

答案 3 :(得分:0)

受到ed从18年3月21日在17:28的先前回答的启发 嘲笑。这对我有用。 tf.cond

def custom_activation(x):
    return tf.cond(tf.greater(x, 0), lambda: ..., lambda: ....)