如何在 TensorFlow 图中添加 if 条件?
How to add if condition in a TensorFlow graph?
假设我有以下代码:
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
语句会在计算中起作用吗(我不这么认为)?如果没有,如何在 TensorFlow 计算图中添加一条 if
语句?
if
语句在这里不起作用是正确的,因为条件是在图形构造时评估的,而您可能希望条件取决于在运行时提供给占位符的值. (事实上,它总是走第一个分支,因为 condition > 0
求值为 Tensor
,即 "truthy" in Python。)
为了支持条件控制流,TensorFlow 提供了 tf.cond()
运算符,它根据布尔条件评估两个分支之一。为了向您展示如何使用它,我将重写您的程序,以便 condition
是一个标量 tf.int32
值(为简单起见):
x = tf.placeholder(tf.float32, shape=[None, ins_size**2*3], name="x_input")
condition = tf.placeholder(tf.int32, shape=[], name="condition")
W = tf.Variable(tf.zeros([ins_size**2 * 3, label_option]), name="weights")
b = tf.Variable(tf.zeros([label_option]), name="bias")
y = tf.cond(condition > 0, lambda: tf.matmul(x, W) + b, lambda: tf.matmul(x, W) - b)
张量流 2.0
TF 2.0 introduces a feature called AutoGraph 允许您将 python 代码 JIT 编译为 Graph 执行。这意味着您可以使用 python 控制流语句(是的,这包括 if
语句)。从文档中,
AutoGraph supports common Python statements like while
, for
, if
,
break
, continue
and return
, with support for nesting. That means you
can use Tensor expressions in the condition of while
and if
statements, or iterate over a Tensor in a for
loop.
您需要定义一个函数来实现您的逻辑并用 tf.function
对其进行注释。这是文档中的修改示例:
import tensorflow as tf
@tf.function
def sum_even(items):
s = 0
for c in items:
if tf.equal(c % 2, 0):
s += c
return s
sum_even(tf.constant([10, 12, 15, 20]))
# <tf.Tensor: id=1146, shape=(), dtype=int32, numpy=42>
假设我有以下代码:
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
语句会在计算中起作用吗(我不这么认为)?如果没有,如何在 TensorFlow 计算图中添加一条 if
语句?
if
语句在这里不起作用是正确的,因为条件是在图形构造时评估的,而您可能希望条件取决于在运行时提供给占位符的值. (事实上,它总是走第一个分支,因为 condition > 0
求值为 Tensor
,即 "truthy" in Python。)
为了支持条件控制流,TensorFlow 提供了 tf.cond()
运算符,它根据布尔条件评估两个分支之一。为了向您展示如何使用它,我将重写您的程序,以便 condition
是一个标量 tf.int32
值(为简单起见):
x = tf.placeholder(tf.float32, shape=[None, ins_size**2*3], name="x_input")
condition = tf.placeholder(tf.int32, shape=[], name="condition")
W = tf.Variable(tf.zeros([ins_size**2 * 3, label_option]), name="weights")
b = tf.Variable(tf.zeros([label_option]), name="bias")
y = tf.cond(condition > 0, lambda: tf.matmul(x, W) + b, lambda: tf.matmul(x, W) - b)
张量流 2.0
TF 2.0 introduces a feature called AutoGraph 允许您将 python 代码 JIT 编译为 Graph 执行。这意味着您可以使用 python 控制流语句(是的,这包括 if
语句)。从文档中,
AutoGraph supports common Python statements like
while
,for
,if
,break
,continue
andreturn
, with support for nesting. That means you can use Tensor expressions in the condition ofwhile
andif
statements, or iterate over a Tensor in afor
loop.
您需要定义一个函数来实现您的逻辑并用 tf.function
对其进行注释。这是文档中的修改示例:
import tensorflow as tf
@tf.function
def sum_even(items):
s = 0
for c in items:
if tf.equal(c % 2, 0):
s += c
return s
sum_even(tf.constant([10, 12, 15, 20]))
# <tf.Tensor: id=1146, shape=(), dtype=int32, numpy=42>