import tensorflow as tf
n1 = tf.constant(2)
n2 = tf.constant(3)
n3 = tf.constant(4)
n4 = tf.constant(5)
def true_fn1():
return tf.constant(11)
def false_fn1():
return tf.constant(22)
def true_fn():
return tf.cond(n3<n4,true_fn1,false_fn1)
def false_fn():
return tf.constant(33)
r = tf.cond(n1<n2,true_fn,false_fn)
sess = tf.Session()
print(sess.run(r))
print结果11
相当于实现了if n1<n2 and n3<n4:
后来发现,用 &
和 |
就行了
import tensorflow as tf
n1 = tf.constant(True,tf.bool)
n2 = tf.constant(False,tf.bool)
r1 = n1 | n2
r2 = n1 & n2
sess = tf.Session()
print(sess.run(r1))
print(sess.run(r2))
import tensorflow as tf
n1 = tf.constant(1)>tf.constant(0)
n2 = tf.constant(1)<tf.constant(0)
r1 = n1 | n2
r2 = n1 & n2
sess = tf.Session()
print(sess.run(r1))
print(sess.run(r2))