Graph execution error:

history = model.fit(train, epochs=100, validation_data = (test))
getting below error

InvalidArgumentError Traceback (most recent call last)
/tmp/ipykernel_785/1287905627.py in <cell line: 2>()
1 #model train for 100 epochs
----> 2 history = model.fit(train, epochs=100, validation_data = (test))

/usr/local/lib/python3.10/site-packages/keras/src/utils/traceback_utils.py in error_handler(*args, **kwargs)
68 # To get the full stack trace, call:
69 # tf.debugging.disable_traceback_filtering()
—> 70 raise e.with_traceback(filtered_tb) from None
71 finally:
72 del filtered_tb

/usr/local/lib/python3.10/site-packages/tensorflow/python/eager/execute.py in quick_execute(op_name, num_outputs, inputs, attrs, ctx, name)
51 try:
52 ctx.ensure_initialized()
—> 53 tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name,
54 inputs, attrs, num_outputs)
55 except core._NotOkStatusException as e:

InvalidArgumentError: Graph execution error:

Detected at node ‘gradient_tape/categorical_crossentropy/mul/BroadcastGradientArgs’ defined at (most recent call last):
File “/usr/local/lib/python3.10/runpy.py”, line 196, in _run_module_as_main
return _run_code(code, main_globals, None,
File “/usr/local/lib/python3.10/runpy.py”, line 86, in _run_code
exec(code, run_globals)
File “/usr/local/lib/python3.10/site-packages/ipykernel_launcher.py”, line 17, in
app.launch_new_instance()
File “/usr/local/lib/python3.10/site-packages/traitlets/config/application.py”, line 1043, in launch_instance
app.start()
File “/usr/local/lib/python3.10/site-packages/ipykernel/kernelapp.py”, line 725, in start
self.io_loop.start()
File “/usr/local/lib/python3.10/site-packages/tornado/platform/asyncio.py”, line 215, in start
self.asyncio_loop.run_forever()
File “/usr/local/lib/python3.10/asyncio/base_events.py”, line 595, in run_forever
self._run_once()
File “/usr/local/lib/python3.10/asyncio/base_events.py”, line 1881, in _run_once
handle._run()
File “/usr/local/lib/python3.10/asyncio/events.py”, line 80, in _run
self._context.run(self._callback, *self._args)
File “/usr/local/lib/python3.10/site-packages/ipykernel/kernelbase.py”, line 513, in dispatch_queue
await self.process_one()
File “/usr/local/lib/python3.10/site-packages/ipykernel/kernelbase.py”, line 502, in process_one
await dispatch(*args)
File “/usr/local/lib/python3.10/site-packages/ipykernel/kernelbase.py”, line 409, in dispatch_shell
await result
File “/usr/local/lib/python3.10/site-packages/ipykernel/kernelbase.py”, line 729, in execute_request
reply_content = await reply_content
File “/usr/local/lib/python3.10/site-packages/ipykernel/ipkernel.py”, line 422, in do_execute
res = shell.run_cell(
File “/usr/local/lib/python3.10/site-packages/ipykernel/zmqshell.py”, line 540, in run_cell
return super().run_cell(*args, **kwargs)
File “/usr/local/lib/python3.10/site-packages/IPython/core/interactiveshell.py”, line 2975, in run_cell
result = self._run_cell(
File “/usr/local/lib/python3.10/site-packages/IPython/core/interactiveshell.py”, line 3030, in _run_cell
return runner(coro)
File “/usr/local/lib/python3.10/site-packages/IPython/core/async_helpers.py”, line 78, in pseudo_sync_runner
coro.send(None)
File “/usr/local/lib/python3.10/site-packages/IPython/core/interactiveshell.py”, line 3257, in run_cell_async
has_raised = await self.run_ast_nodes(code_ast.body, cell_name,
File “/usr/local/lib/python3.10/site-packages/IPython/core/interactiveshell.py”, line 3473, in run_ast_nodes
if (await self.run_code(code, result, async
=asy)):
File “/usr/local/lib/python3.10/site-packages/IPython/core/interactiveshell.py”, line 3553, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File “/tmp/ipykernel_785/1287905627.py”, line 2, in <cell line: 2>
history = model.fit(train, epochs=100, validation_data = (test))
File “/usr/local/lib/python3.10/site-packages/keras/src/utils/traceback_utils.py”, line 65, in error_handler
return fn(*args, **kwargs)
File “/usr/local/lib/python3.10/site-packages/keras/src/engine/training.py”, line 1742, in fit
tmp_logs = self.train_function(iterator)
File “/usr/local/lib/python3.10/site-packages/keras/src/engine/training.py”, line 1338, in train_function
return step_function(self, iterator)
File “/usr/local/lib/python3.10/site-packages/keras/src/engine/training.py”, line 1322, in step_function
outputs = model.distribute_strategy.run(run_step, args=(data,))
File “/usr/local/lib/python3.10/site-packages/keras/src/engine/training.py”, line 1303, in run_step
outputs = model.train_step(data)
File “/usr/local/lib/python3.10/site-packages/keras/src/engine/training.py”, line 1084, in train_step
self.optimizer.minimize(loss, self.trainable_variables, tape=tape)
File “/usr/local/lib/python3.10/site-packages/keras/src/optimizers/optimizer.py”, line 543, in minimize
grads_and_vars = self.compute_gradients(loss, var_list, tape)
File “/usr/local/lib/python3.10/site-packages/keras/src/optimizers/optimizer.py”, line 276, in compute_gradients
grads = tape.gradient(loss, var_list)
Node: ‘gradient_tape/categorical_crossentropy/mul/BroadcastGradientArgs’
Incompatible shapes: [32,3] vs. [32,7688]
[[{{node gradient_tape/categorical_crossentropy/mul/BroadcastGradientArgs}}]] [Op:__inference_train_function_4776]

Hi @Rohini_Patil

Welcome to the TensorFlow Forum!

The given information is not sufficient to understand the issue. Could you please share some reproducible code along with dataset type and shape to replicate the error and help you fixing the issue. Thank you.

1 Like

##import all the require labraries
#import keras_cv
import tensorflow as tf
from tensorflow import keras
from keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras import models, layers
from tensorflow.keras.optimizers.legacy import SGD

dataflow = ImageDataGenerator(rescale = 1.0 / 255.0 )

#import training data from file
train = dataflow.flow_from_directory(‘1577957291_deeplearningwithkerasandtensorflow/data/train’, class_mode = ‘categorical’)

#import test data from file
test = dataflow.flow_from_directory(“1577957291_deeplearningwithkerasandtensorflow/data/test”, class_mode = ‘categorical’)

#build model according to
model = models.Sequential()

#Input layer ● Convolutional layer 1 with 32 filters of kernel size[5,5]
model.add( layers.Conv2D( 32, (5, 5 ), activation = ‘relu’, padding = ‘same’, input_shape = (256, 256 , 3 )))

#Pooling layer 1 with pool size[2,2] and stride
model.add(layers.MaxPooling2D(2,2))

#Convolutional layer 2 with 64 filters of kernel size[5,5]
model.add(layers.Conv2D(64, (5, 5 ), activation = ‘relu’))

#Pooling layer 2 with pool size[2,2] and stride 2
model.add(layers.MaxPooling2D((2,2)))

#Dense layer whose output size is fixed in the hyper parameter: fc_size=32
model.add(layers.Dense(32, activation = ‘relu’))
model.add(layers.Dense(2, activation = ‘softmax’))

#Dropout layer with dropout probability 0.4 Predict the class by doing a softmax on the output of the dropout layers. This should be followed by training and evaluation:
model.add(layers.Dropout(0.4))
model.add(layers.Flatten())

#For the training step, define the loss function and minimize it
sgd_opt = tf.keras.optimizers.legacy.SGD(learning_rate = 0.001)

#compiling model
history=model.compile(optimizer=sgd_opt, loss = ‘binary_crossentropy’, metrics = [‘accuracy’])

#model train for 100 epochs
history=model.fit(train, validation_data = test, epochs=100)

For model.fit I am getting below error

InvalidArgumentError Traceback (most recent call last)
/tmp/ipykernel_278/348276030.py in <cell line: 2>()
1 #model train for 100 epochs
----> 2 history=model.fit(train, validation_data = test, epochs=100, verbose=1)

/usr/local/lib/python3.10/site-packages/keras/src/utils/traceback_utils.py in error_handler(*args, **kwargs)
68 # To get the full stack trace, call:
69 # tf.debugging.disable_traceback_filtering()
—> 70 raise e.with_traceback(filtered_tb) from None
71 finally:
72 del filtered_tb

/usr/local/lib/python3.10/site-packages/tensorflow/python/eager/execute.py in quick_execute(op_name, num_outputs, inputs, attrs, ctx, name)
51 try:
52 ctx.ensure_initialized()
—> 53 tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name,
54 inputs, attrs, num_outputs)
55 except core._NotOkStatusException as e:

InvalidArgumentError: Graph execution error:

@Rohini_Patil

First of all, You need to swap few layers in the model as below:

#build model according to
model = models.Sequential()
model.add( layers.Conv2D( 32, (5, 5 ), activation = 'relu', padding = 'same', input_shape = (256, 256 , 3 )))
model.add(layers.MaxPooling2D(2,2))
model.add(layers.Conv2D(64, (5, 5 ), activation = 'relu'))
model.add(layers.MaxPooling2D((2,2)))
model.add(layers.Dropout(0.4)) #<-----
model.add(layers.Flatten()) #<------
model.add(layers.Dense(32, activation = 'relu')) #<------
model.add(layers.Dense(2, activation='softmax')) #<------

Second, Please use categorical_crossentropy() instead of binary_crossentropy() as you have categorical classes in your dataset and try running the code again.

Let us know if the issue still persists. Thank you.

Hi Renu,

Thank you for your time and help…but unfortunately I am facing same issue

Please provide us the details like which system OS, tensorflow version you are using along with the dataset type and shape which can be the reason for the error.