在控制流和条件语句中使用张量 [英] Using Tensors in control flow and conditional statements

查看:27
本文介绍了在控制流和条件语句中使用张量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对如何在控制流语句中使用张量(没有会话)感兴趣.

I'm interested how I can use a Tensor (without a session) in control flow statements.

我的意思的一个例子是:

One example of what I mean by this is:

myTensor = tf.where(myOtherTensor) # shape == [None, 2]
numLoops = tf.shape(myTensor)[0] # Results in tensor of shape [1]
for i in xrange(numLoops):
     # ...

我可以将 numLoops(张量)传递给 xrange() 吗?如果没有,还有其他方法可以做到这一点吗?

Can I pass numLoops (a tensor) to xrange()? If not, is there another way to do this?

另一个例子是:

myTensor = tf.in_top_k(myOtherTensor1, myOtherTensor2, 5) # Tensor of bools
myCondition = myTensor[0] # Results in tensor of shape [1] of type bool
if myCondition:
     # ...

<小时>

我的问题:张量(没有特定会话)可以按上述方式使用吗?


My Question: Can Tensors (without a specific session) be used in the ways outlined above?

如果我有一个会话,我可以简单地评估这些单元素张量,然后使用它们.如果我直到运行时才知道具体值,那么必须有一种使用这些值的方法.

If I had a session I could simply evaluate those single-element tensors and then use them. In the event that I don't know the specific value until runtime, there must be a way to use the values.

我可以预见的问题:也许循环会使生成图形变得不可能,因为您不知道包含的操作将执行多少次?但是在运行时存储循环的操作并简单地应用它们正确的次数似乎微不足道.

Problems I could foresee: maybe the loop would make generating the graph impossible as you don't know how many times the contained operations will be executed? But it would seem trivial to store the loop's operations and simply apply them the correct number of times, at runtime.

如果有任何不清楚的地方,请告诉我,我会提供更多细节.

If anything lacks clarity, let me know and I'll provide more detail.

推荐答案

TensorFlow 在 tensorflow.python.ops.control_flow_ops 模块.(注意这些操作的接口可能会发生变化!使用风险自负!)

TensorFlow contains experimental support for in-graph control flow in the tensorflow.python.ops.control_flow_ops module. (N.B. The interfaces to these ops are subject to change! Use at your own risk!)

有条件的支持(您的第二种情况)将出现在下一个版本中,并且最近已添加到公共 API(如果您从源代码构建).它目前正在 TensorFlow 附带的一些库中使用,例如 RNN cell(当序列长度已知时使用它来提前停止).

Conditional support (your second case) will appear in the next release, and was recently added to the public API (if you build from source). It's currently being used in some of the libraries that ship with TensorFlow, such as the RNN cell (which use it for early stopping when the sequence length is known).

tf) 运算符采用布尔值 Tensor 作为谓词和两个 lambda 表达式;并返回一个或多个值.

The tf.cond() operator takes a boolean Tensor, which acts as the predicate, and two lambda expressions; and returns one or more values.

你的程序看起来像:

if_true = lambda: ...  # Value to return if `myCondition` is true
if_false = lambda: ...  # Value to return if `myCondition` is false

result = tf.cond(myCondition, if_true, if_false)

<小时>

迭代(您的第一种情况)可以使用 While() 高阶运算符.本质上,您可以将程序编写为(以伪代码形式):


Iteration (your first case) can be handled using the While() higher-order operator. Essentially, you would write your program as (in pseudocode):

i = 0
while i < tf.rank(myTensor):
  # ...

...大致表示为:

from tensorflow.python.ops import control_flow_ops as cfo

i = tf.constant(0)
condition = lambda i: tf.less(i, tf.rank(myTensor))
body = lambda x: ...
inital_accumulator = ...  # will be bound to `x` in first iteration of `body`.

# `result` will contain the value returned from the final iteration of `body`.
result = cfo.While(condition, body, [initial_accumulator])

请注意,While() 运算符要求您为所有循环变量指定初始值(在本例中仅为 initial_accumulator).循环常量将像在普通 Python lambda 中一样被捕获.

Note that the While() operator requires you to specify initial values for all of the loop variables (just initial_accumulator in this case). Loop constants will be captured as in a normal Python lambda.

这篇关于在控制流和条件语句中使用张量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆