Tensorflow Loops in Graph (Topological Sort Failed)


I have a model that I’ve created in Tensorflow (using Keras).

In pseudo-code, without writing too much of the data-specific ops, it is roughly:

iter_count = tf.keras.layers.Dense(1)(input)

#scale iter_count between 1 and int_max

for i in tf.range(iter_count):

    inputvariation = tf.concat([input, i])
    box = tf.keras.layers.Dense(4)(inputvariation)

    #append to TensorArray

#stack and return TensorArray   

However, this outputs an error that the model can’t be sorted into topological order. Does this mean I can’t run loops in my model?

Some errors:

E tensorflow/core/grappler/optimizers/dependency_optimizer.cc:771] Iteration = 0, topological sort failed with message: The graph couldn’t be sorted in topological order.

W tensorflow/core/common_runtime/process_function_library_runtime.cc:941] Ignoring multi-device function optimization failure: INVALID_ARGUMENT: The graph couldn’t be sorted in topological order.


Not sure what you are trying to do but it should be possible:

import tensorflow as tf

inputs = tf.keras.layers.Input((5, ))
iter_count = tf.keras.layers.Dense(1)(inputs)

for i in tf.range(tf.shape(iter_count)[-1]):

    # if you want to get the actual output of the Dense layer **iter_count**
    # try i = itercount[:, i]
    inputvariation = tf.concat([inputs, tf.cast(tf.repeat(i, repeats=tf.shape(inputs)[0])[..., None], dtype=tf.float32)], axis=-1)
    box = tf.keras.layers.Dense(4)(inputvariation)

model = tf.keras.Model(inputs, box)

model(tf.random.normal((1, 5)))

Answered By – AloneTogether

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published