26
比方说,我有以下代码:如何在TensorFlow图中添加条件?
x = tf.placeholder("float32", shape=[None, ins_size**2*3], name = "x_input")
condition = tf.placeholder("int32", shape=[1, 1], name = "condition")
W = tf.Variable(tf.zeros([ins_size**2*3,label_option]), name = "weights")
b = tf.Variable(tf.zeros([label_option]), name = "bias")
if condition > 0:
y = tf.nn.softmax(tf.matmul(x, W) + b)
else:
y = tf.nn.softmax(tf.matmul(x, W) - b)
将在计算if
声明的工作(我不这么认为)?如果不是,我怎样才能将一个if
语句添加到TensorFlow计算图中?
谢谢你这么多的解释,详细! –
@mrry两个分支默认都执行了吗?我有tf.cond(c,lambda x:train_op1,lambda x:train_op2),并且每次执行cond时都会执行两次train_ops,而与c的值无关。难道我做错了什么? –
@PiotrDabkowski这是'tf.cond()'有时令人惊讶的行为,在[文档](https://www.tensorflow.org/api_docs/python/tf/cond)中被触及。简而言之,您需要创建您想要在各自的lambda表达式中有条件运行的操作。您在lambdas之外创建但在两个分支中引用的所有内容都将在两种情况下执行。 – mrry