Tensorflow——tf.cond()的用法

定义格式

tf.cond(pred, fn1, fn2, name=None)
Return :either fn1() or fn2() based on the boolean predicate pred.(注意这里,也就是说'fnq'和‘fn2’是两个函数)

在TensorFlow中,tf.cond()类似于c语言中的if...else...,用来控制数据流向

例子

import tensorflow as tf
a=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())

输出
10

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容