Tensorflow Stuck Into Endless Loop Using Tf.while_loop()
Steps to reproduce I am using TensorFlow to implement a network that needs to use tf.while_loop() import tensorflow as tf import numpy as np class model(object): def __init__(s
Solution 1:
TL;DR: You cannot store tensors that were created in the loop body for later use, because that breaks some assumptions about how the loop is structured.
In general, the condition()
and body()
functions must not have side effects.
Indeed, it is unlikely that your program has the intended behavior: TensorFlow will execute the body()
function once, to build the necessary graph structure, so z
will only contain one element after running model.__init__()
.
Instead, you must construct z
incrementally in the loop body, using tf.concat()
and producing the value as a loop variable:
starter = tf.constant(0)
z_initial = tf.constant([], dtype=tf.int32)
defbody(hops, z_prev):
hops = tf.add(hops, 1)
z_next = tf.concat(0, [z_prev, tf.expand_dims(hops, 0)])
return hops, z_next
defcondition(hops, z):
return tf.logical_and(tf.less(tf.gather(
argmax_ep_gate_array_concat, hops), story_len), tf.less(hops, tf.constant(20)))
self.gate_index, self.z = tf.while_loop(condition,body,[starter, z_initial])
Post a Comment for "Tensorflow Stuck Into Endless Loop Using Tf.while_loop()"