TensorFlow if語句 tensor 和 非tensor 比較

import tensorflow as tf

condition = tf.placeholder(tf.int32, name="condition")

A = tf.constant(value=123)
B = tf.constant(value=321)

def func1():
    return A

def func2():
    return B

y = tf.cond(condition > 0, func1, func2) # tensor 和 非tensor 比較
 
sess = tf.Session()

feed_dict = {condition:1}
print(sess.run(y,feed_dict=feed_dict))

feed_dict = {condition:-1}
print(sess.run(y,feed_dict=feed_dict))

打印結果:
123
321

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章