keras-yolo3代码里看到的,具体用法如下
K.control_flow_ops.while_loop(cond, body, loop_vars),其中cond是条件判断函数,返回True或False; body是循环体;loop_vars是传入cond和body的参数列表。
cond和body入参的个数和类型一致,起始值就是loop_vars里的值。body返回的值和传入的值个数和类型一致。
以下两段代码是等价的
i = 0
while i < 10:
i += 1
print(i)
from keras import backend as K
import tensorflow as tf
tf.enable_eager_execution()
def c(i):
return i < 10
def b(i):
i += 1
return i
r = K.control_flow_ops.while_loop(c, b, [0])
print(r)
以下也是等价的
from keras import backend as K
import tensorflow as tf
tf.enable_eager_execution()
x = 0
y = 3
while x < y:
x += 1
print(x)
def loop_body(x, y):
x += 1
return x, y
x, y = K.control_flow_ops.while_loop(lambda x, y: x < y, loop_body, [0, 3])
print(x, y)