【发布时间】:2019-12-14 04:50:34
【问题描述】:
我正在尝试使用tf.while_loop 并行化循环。正如here 所建议的那样,parallel_iterations 参数在急切模式中没有任何区别。所以我尝试用tf.function 包裹tf.while_loop。但是,添加装饰器后,迭代变量的行为发生了变化。
例如,这段代码有效。
result = np.zeros(10)
iteration = tf.constant(0)
c = lambda i: tf.less(i, 10)
def print_fun(iteration):
result[iteration] = iteration
iteration += 1
return (iteration,)
tf.while_loop(c, print_fun, [iteration])
如果我添加装饰器,就会出现错误。
result = np.zeros(10)
iteration = tf.constant(0)
c = lambda i: tf.less(i, 10)
def print_fun(iteration):
result[iteration] = iteration
iteration += 1
return (iteration,)
@tf.function
def run_graph():
iteration = tf.constant(0)
tf.while_loop(c, print_fun, [iteration])
run_graph()
在调试过程中,我发现变量iteration 从张量变为占位符。这是为什么?我应该如何修改代码以消除该错误?
谢谢。
【问题讨论】:
标签: python tensorflow tensorflow2.0