#如果对数里面的值<0,将其置1,对数项为0
t1 = tf.cond(tf.less(x, 0), lambda: tf.assign(x, 1.), lambda: x)
import tensorflow as tf
假设t1是一个Tensor
t1 = …
定义条件函数
def condition():
# 当t1<0时执行的操作
return tf.constant(-1)
def body():
# 当t1>=0时执行的操作
return tf.constant(1)
使用tf.cond判断条件并执行操作
result = tf.cond(tf.less(t1, 0), condition, body)