Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I run a loop with a tensor as its range? (in tensorflow)

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

However I get the following error:

"TypeError: 'Tensor' object is not iterable"

What should I do?

like image 885
Poorya Pzm Avatar asked Sep 07 '25 14:09

Poorya Pzm


1 Answers

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])
like image 123
patapouf_ai Avatar answered Sep 10 '25 06:09

patapouf_ai