您的位置:首页 > 其它

tensorflow 复合逻辑‘且’和‘或’的实现

2017-10-12 11:54 856 查看
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))
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: