tf.cond和if-else有什么区别?
import tensorflow as tf
x = 'x'
y = tf.cond(tf.equal(x, 'x'), lambda: 1, lambda: 0)
with tf.Session() as sess:
print(sess.run(y))
x = 'y'
with tf.Session() as sess:
print(sess.run(y))
Run Code Online (Sandbox Code Playgroud)
import tensorflow as tf
x = tf.Variable('x')
y = tf.cond(tf.equal(x, 'x'), lambda: 1, lambda: 0)
init = tf.global_variables_initializer()
with tf.Session() as sess:
init.run()
print(sess.run(y))
tf.assign(x, 'y')
with tf.Session() as sess:
init.run()
print(sess.run(y))
Run Code Online (Sandbox Code Playgroud)
输出都是1
.
这是否意味着只有tf.placeholder可以工作,而不是所有张量,例如tf.variable?我什么时候应该选择if-else条件以及何时使用tf.cond?他们之间有什么不同?
归档时间: |
|
查看次数: |
12147 次 |
最近记录: |