【问题标题】:The function for tensor value generates this Error: 'false_fn' must be callable张量值的函数生成此错误:\'false_fn\' 必须是可调用的
【发布时间】:2023-01-29 22:05:09
【问题描述】:

我正在创建一个函数,该函数采用张量值并通过应用以下公式返回结果,有 3 个条件,所以我使用 @tf.functions。

def Spa(x):
    x= tf.convert_to_tensor(float(x), dtype=tf.float32)
    p= tf.convert_to_tensor(float(0.05), dtype=tf.float32)
    
    p_dash=x
    K = p*logp_dash
    Ku=K.sum(Ku)
    
    Ku= tf.convert_to_tensor(float(Ku), dtype=tf.float32)
    

    y= tf.convert_to_tensor(float(0), dtype=tf.float32)
    def a(): return tf.constant(0)

    r = tf.case([(tf.less(x, y), a), (tf.greater(x, Ku), a)], default=x, exclusive=False)
    return r

该代码生成以下错误:'false_fn' 必须是可调用的。我做了很多转换,int 到 float 和 float 到 int 但不知道是什么问题。

【问题讨论】:

  • must be callable 意味着它必须是函数(或具有定义的 () 运算符的东西)。
  • def y(): return tf.constant(0) y = y() y = tf.convert_to_tensor(y, dtype=tf.int32) 添加这个还是报错

标签: python tensorflow autoencoder activation-function relu


【解决方案1】:

它应该是这样的。

x = tf.where(x < 0, tf.zeros_like(x), x)
p = 0.05
p_hat = x
KL_divergence = p * (tf.math.log(p / p_hat)) + (1 - p) * (tf.math.log(1 - p / 1 - p_hat))
x = tf.where(x < KL_divergence, tf.zeros_like(x), x)
return x

【讨论】:

    【解决方案2】:

    tf.case x 应该是一个函数而不是张量。作为described in TF page

    def f1(): return tf.constant(17)
    def f2(): return tf.constant(23)
    def f3(): return tf.constant(-1)
    r = tf.case([(tf.less(x, y), f1), (tf.greater(x, z), f2)], default=f3, exclusive=True)
    

    可以看到这里的f3也是一个函数。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多