Skip to content Skip to sidebar Skip to footer

Runtimeerror: The Session Graph Is Empty. Add Operations To The Graph Before Calling Run()

I am training a neural network with keras, and since my dataset is very large I am using fit_generator to feed the data to the network. As the first argument of fit_generator I hav

Solution 1:

I found a way to handle it. What I found out was that printing tf.get_default_graph() in generator method and in main method (I mean before calling model.fit_generator) returns different graphs.

Why? I have no idea!

Anyway, I solved it by sending default graph as another argument to the function and introducing it to tf.Session(). Like this:

def generator(dataset_iterator, default_graph):
    with tf.Session(graph=default_graph) as sess:
        next_batch = dataset_iterator.get_next()

        whileTrue:
            img, label = sess.run(next_batch)
            # some process on labelyield img, label


# down in the code for training:
dataset_iterator = DatasetGenerator(...)
default_graph = tf.get_default_graph()
model.fit_generator(generator=generator(dataset_iterator, default_graph), ...)

I actually don't know if this is the most elegant way to solve the problem. Further improvements are greatly appreciated :)

Solution 2:

It's eager execution, disable it if you want to create an empty session.

import tensorflow as tf
tf.compat.v1.disable_eager_execution()

Post a Comment for "Runtimeerror: The Session Graph Is Empty. Add Operations To The Graph Before Calling Run()"