2

I try to dot last column of lookback dimension to prior lookback period on 4D tensor (Sample, Time-steps, Lookback,features) by using keras TimeDistributed layer. The model can run normally but it throw a warning that graph couldn't be sorted in topological order when I run Model.fit().

This forum say that it can mess up model training. So what can I do to prevent this happen?

ENV:

  1. Tensorflow-GPU 1.15.0
  2. CUDA V10.0.130
  3. python 3.6.5
  4. Keras 2.3.1
  5. Keras-Applications 1.0.8
  6. Keras-Preprocessing 1.1.0
import numpy as np
from keras.models import Model
from keras.layers import Input, TimeDistributed
import keras
# Dot layer
class Dot(keras.layers.Layer):
    def __init__(self, **kwargs):
        super(Dot, self).__init__(**kwargs)

    def call(self, x):

        ht, hT = x[:,:-1,:],x[:,-1:,:]
        ml = tf.multiply(ht, hT)

        # I believe problem come from reduce_sum
        dot = tf.reduce_sum(ml, axis=-1)
        return dot

    def compute_output_shape(self, input_shape):

        return (None,input_shape[1]-1)

num_fea = 11
num_lookback = 5
time_step = 3
sample = 2

# create model
input = Input(shape=(time_step,num_lookback,num_fea))
dot = Dot()
output = TimeDistributed(dot)(input)

M = Model(inputs=[input], outputs=[output])
M.compile(keras.optimizers.Adam(learning_rate=0.0001), loss='mse')

# create test data
data = np.arange(num_lookback*num_fea).reshape((num_lookback,num_fea))
data = np.broadcast_to(data,shape=(sample,time_step,num_lookback,num_fea))
y = np.ones(shape=(sample,time_step,num_lookback-1))

# fit model to demonstrate error
M.fit(x=data,y=y, batch_size=2, epochs=10)

Warning log

2020-03-05 08:36:17.558396: E tensorflow/core/grappler/optimizers/dependency_optimizer.cc:697] Iteration = 1, topological sort failed with message: The graph couldn't be sorted in topological order.
2020-03-05 08:36:17.558777: E tensorflow/core/grappler/optimizers/meta_optimizer.cc:533] layout failed: Invalid argument: The graph couldn't be sorted in topological order.
2020-03-05 08:36:17.559302: E tensorflow/core/grappler/optimizers/meta_optimizer.cc:533] model_pruner failed: Invalid argument: MutableGraphView::MutableGraphView error: node 'loss/time_distributed_1_loss/mean_squared_error/weighted_loss/concat' has self cycle fanin 'loss/time_distributed_1_loss/mean_squared_error/weighted_loss/concat'.
2020-03-05 08:36:17.560121: E tensorflow/core/grappler/optimizers/meta_optimizer.cc:533] remapper failed: Invalid argument: MutableGraphView::MutableGraphView error: node 'loss/time_distributed_1_loss/mean_squared_error/weighted_loss/concat' has self cycle fanin 'loss/time_distributed_1_loss/mean_squared_error/weighted_loss/concat'.
2020-03-05 08:36:17.560575: E tensorflow/core/grappler/optimizers/meta_optimizer.cc:533] arithmetic_optimizer failed: Invalid argument: The graph couldn't be sorted in topological order.
2020-03-05 08:36:17.560853: E tensorflow/core/grappler/optimizers/dependency_optimizer.cc:697] Iteration = 0, topological sort failed with message: The graph couldn't be sorted in topological order.
2020-03-05 08:36:17.561141: E tensorflow/core/grappler/optimizers/dependency_optimizer.cc:697] Iteration = 1, topological sort failed with message: The graph couldn't be sorted in topological order.
Ronakrit W.
  • 693
  • 4
  • 8

1 Answers1

0

you can consider using TensorFlow 2.x version.

I have migrated/upgraded your code and verify that it works on google colab. You can try to look here for more info on how to migrate your code to Tensorflow 2.x

Kindly refer to the code below

import numpy as np
import tensorflow as tf
from tensorflow.keras.models import Model
from tensorflow.keras.layers import Input, TimeDistributed
#import keras
# Dot layer
class Dot(tf.keras.layers.Layer):
    def __init__(self, **kwargs):
        super(Dot, self).__init__(**kwargs)

    def call(self, x):

        ht, hT = x[:,:-1,:],x[:,-1:,:]
        ml = tf.multiply(ht, hT)

        # I believe problem come from reduce_sum
        dot = tf.reduce_sum(ml, axis=-1)
        return dot

    def compute_output_shape(self, input_shape):

        return (None,input_shape[1]-1)

num_fea = 11
num_lookback = 5
time_step = 3
sample = 2

# create model
input = Input(shape=(time_step,num_lookback,num_fea))
dot = Dot()
output = TimeDistributed(dot)(input)

M = Model(inputs=[input], outputs=[output])
M.compile(optimizer='adam', loss='mse')

# create test data
data = np.arange(num_lookback*num_fea).reshape((num_lookback,num_fea))
data = np.broadcast_to(data,shape=(sample,time_step,num_lookback,num_fea))
y = np.ones(shape=(sample,time_step,num_lookback-1))

# fit model to demonstrate error
M.fit(x=data,y=y, batch_size=2, epochs=10)
TF_Support
  • 1,794
  • 1
  • 7
  • 16
  • Thanks for your code but this is just a portion of my code. If I change TF version, I have to change all of my 1000 line of code to TF2 and debug it again. – Ronakrit W. Mar 19 '20 at 13:07
  • Hi @RonakritW. You can refer to the below explanation regarding that warnings https://stackoverflow.com/questions/52607063/tensorflow-warning-the-graph-couldnt-be-sorted-in-topological-order. – TF_Support Mar 20 '20 at 01:21
  • I have seen that post, but I couldn't understand where is the loop in this Implementation. – Ronakrit W. Mar 20 '20 at 01:34
  • The problem might be this ```ml = tf.multiply(ht, hT)``` . You can also check link https://github.com/tensorflow/tensorflow/issues/24816 – TF_Support Mar 20 '20 at 10:57
  • I also have seen this but it doesn't help, I have spent 1 weeks to find the solution, I have seen almost all available answer but non of them suggest that there is a solution. Second, multiply ht and hT does not creating a loop, if it do in the backend please show me the code or something to proof it. – Ronakrit W. Mar 20 '20 at 16:36
  • Any news on this discussion? – user3352632 Sep 30 '20 at 22:37