Home > Software engineering >  Tensorflow Loops in Graph (Topological Sort Failed)
Tensorflow Loops in Graph (Topological Sort Failed)

Time:09-14

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.

CodePudding user response:

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)))
  • Related