Skip to content Skip to sidebar Skip to footer

Tensor(..., Shape=(), Dtype=int64) Must Be From The Same Graph As Tensor(..., Shape=(), Dtype=resource) Keras

I'm trying to use Keras to run a Conv2D net to read a set a folder that contain the hand-gesture images from 20bn Jester I know a Conv2D probably won't work, but I want to get some

Solution 1:

The problem is that you are resetting your default graph before training the model:

tf.reset_default_graph()  # <-- remove this line
model.fit_generator(train_it, steps_per_epoch=16, validation_data=valid_it, validation_steps=8)

The problem is as follows. You reset first the default graph at the beginning, which, unless your actual script has more code before that, doesn't really make any difference, since the default graph is empty at that point. Then you make your model, and it creates operations on the new default graph. That new default graph is the one you get later with:

graph = tf.get_default_graph()

The problem is later, after clearing the Keras session twice (which doesn't have any effect either), you reset the default graph again. When you call fit, the training process starts, and some new graph objects of the Keras model are created. Since your default graph has changed it (because you reset it), those new objects are created in a different graph from the rest of objects, which causes the error. I think you still could reset the graph and have it work if you use the former default graph as default during the training:

tf.reset_default_graph()
with graph.as_default():  # Use former default graph as default
    model.fit_generator(train_it, steps_per_epoch=16, validation_data=valid_it, validation_steps=8)

I'm not completely sure if the Keras default session will always work (since it might have been created for the new default graph), but I think it should... In any case, if you for whatever reason you want to have your Keras model isolated in its own graph, instead of resetting the graph you can do it as follows:

with tf.Graph().as_default() as graph:  # Make a new graph and use it as default# Make dataset# Make model# Train

Post a Comment for "Tensor(..., Shape=(), Dtype=int64) Must Be From The Same Graph As Tensor(..., Shape=(), Dtype=resource) Keras"