pythontensorflowautoencoderactivation-functionrelu

The function for tensor value generates this Error: 'false_fn' must be callable


I am creating a function that takes a tensor value and returns the result by applying the following formulation, There are 3 conditions so I am using @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

The code generates the following error: 'false_fn' must be callable. I did many conversions, int to float and float to int but don't know what is the issue.


Solution

  • It should be something like this.

    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