tf.cond和tf.case

来源:互联网 发布:c语言编Newton 编辑:程序博客网 时间:2024/05/22 00:18

tensorflow的逻辑控制
关于tf.case理解的不是很好:
https://stackoverflow.com/questions/41910073/tensorflow-tf-case-f-default-should-do-nothing
待明确后再补充
关于tf.cond,这里给出几个示例,对我来说,例子就是快速明白用法的途径

import tensorflow as tfa=tf.constant(2)b=tf.constant(3)x=tf.constant(4)y=tf.constant(5)z = tf.multiply(a, b)result = tf.cond(x > y, lambda: tf.add(x, z), lambda: tf.square(y))with tf.Session() as session:    print(result.eval())

运行的结果是25
根据第7行代码,如果x>y为true,就执行tf.add(x, z), 否则就执行tf.square(y)
示例二:
这里写图片描述

运行的结果是:2
如果注释第8行,取消注释第9行,此时运行的结果是1

原创粉丝点击