如何使用张量作为范围运行循环? (在张量流中) [英] How can I run a loop with a tensor as its range? (in tensorflow)
本文介绍了如何使用张量作为范围运行循环? (在张量流中)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想要一个for循环,其迭代次数取决于张量值.例如:
I want to have a for loop that the number of its iterations is depend on a tensor value. For example:
for i in tf.range(input_placeholder[1,1]):
# do something
但是我遇到以下错误:
"TypeError:'Tensor'对象不可迭代"
我该怎么办?
推荐答案
To do this you will need to use the tensorflow while loop (tf.while_loop
) as follows:
i = tf.constant(0)
while_condition = lambda i: tf.less(i, input_placeholder[1, 1])
def body(i):
# do something here which you want to do in your loop
# increment i
return [tf.add(i, 1)]
# do the loop:
r = tf.while_loop(while_condition, body, [i])
这篇关于如何使用张量作为范围运行循环? (在张量流中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文