假设我有以下代码:
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 的计算图中呢?
tf.cond()
有时会出人意料的行为,相关文档已经涉及(请参见https://www.tensorflow.org/api_docs/python/tf/cond)。简而言之,您需要在各自的lambda函数内创建您想要有条件地运行的操作。您在lambda函数外面创建但在任一分支中引用的所有内容都将在两种情况下执行。 - mrry