-
Notifications
You must be signed in to change notification settings - Fork 0
/
Flow Control
41 lines (33 loc) · 1.84 KB
/
Flow Control
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#tf.while_loop
参数
condition参数:callable returning a boolean scalar tensor
loop_vars参数:一个list或者nested list of tensor。会被传递给cond和body。`cond` and `body` both take as many arguments as there are `loop_vars`
body参数: callable returning a (possibly nested) tuple, namedtuple or list of tensors of the same arity (length and structure) and types as `loop_vars`
返回值
The output tensors for the loop variables,当cond返回是False时while_loop返回
如果loop_vars的length是1,则返回一个tensor,如果length > 1,则返回一个list
内部机制
while_loop只调用了body和condition函数各一次,将body和condition的节点构成的子图外加一些其它的op黏在一起,形成一个graph flow that
repeats `body` until `cond` returns false。这个graph flow只会在session里运行的时候才会循环的去执行body里的计算。
#tf.cond
参数
pred:一个scaler
ture_fn: pred = true 时执行这个函数并返回结果
false_fn: pred = false 时执行这个函数并返回结果
`true_fn` and `false_fn` must have the same non-zero number and type of outputs
返回值
lists of output tensors
内部机制
`cond` calls `true_fn` and `false_fn` *exactly once* (inside the
call to `cond`, and not at all during `Session.run()`). `cond`
stitches together the graph fragments created during the `true_fn` and
`false_fn` calls with some additional graph nodes to ensure that the right
branch gets executed depending on the value of `pred`.
#tf.where
参数
condition:一个和x有同样形状,或者第一维度相匹配的tensor
x:condition对应位置为true时返回的值
y:condition对应位置为false时返回的值
返回值
当x, y 都 non-None时,返回 A Tensor with the same type and shape as x, y
当x,y都为空时:返回 A Tensor with shape (num_true, dim_size(condition))