我正在尝试使用以下代码构建自定义激活功能,但出现错误: “
TypeError: Using a `tf.Tensor` as a Python `bool` is not allowed. Use `if t is not None:` instead of `if t:` to test if a tensor is defined, and use TensorFlow ops such as tf.cond to execute subgraphs conditioned on the value of a tensor.
代码:
def proposed_tanh(x):
abs_x=tf.math.abs(x)
for i in range(0,1):#)len(tanh_list)-2):
cond0=tf.math.greater(x,0.00075)
print(cond0)
cond1=tf.math.equal(x,0.00075)
cond2=tf.math.greater_equal(abs_x,float(tanh_list[i][0]))
cond3=tf.math.less(abs_x,float(tanh_list[i+1][0]))
cond4=tf.math.greater(x,0)
y_neg=tf.math.negative(float(tanh_list[i][1]))
y_pos=tanh_list[i][1]
y=tf.cond(cond0,lambda:1,(cond1,lambda:-1,(cond2 and cond3 and cond4, lambda:y_pos, lambda:y_neg)))
#y=tf.cond(x>0.00045,lambda:1,lambda:5)
return y
有人可以帮我吗?