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?
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])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With