I am working on an Image classification problem with ImageDataGenerator from the keras library. I am getting the below mentioned error InvalidArgumentError Traceback (most recent call last) Cell In[32], line 1 ----> 1 model.fit(train_generator, 2 validation_data=val_generator, 3 epochs=10, 4 callbacks=[lr]) File c:\Users\LENOVO\AppData\Local\Programs\Python\Python312\Lib\site-packages\keras\src\utils\traceback_utils.py:122, in filter_traceback.<locals>.error_handler(*args, **kwargs) 119 filtered_tb = _process_traceback_frames(e.__traceback__) 120 # To get the full stack trace, call: 121 # `keras.config.disable_traceback_filtering()` --> 122 raise e.with_traceback(filtered_tb) from None 123 finally: 124 del filtered_tb File c:\Users\LENOVO\AppData\Local\Programs\Python\Python312\Lib\site-packages\tensorflow\python\eager\execute.py:53, 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: 56 if name is not None: InvalidArgumentError: Graph execution error: Detected at node LogicalAnd_3 defined at (most recent call last): File "<frozen runpy>", line 198, in _run_module_as_main File "<frozen runpy>", line 88, in _run_code File "C:\Users\LENOVO\AppData\Roaming\Python\Python312\site-packages\ipykernel_launcher.py", line 18, in <module> File "C:\Users\LENOVO\AppData\Roaming\Python\Python312\site-packages\traitlets\config\application.py", line 1075, in launch_instance File "C:\Users\LENOVO\AppData\Roaming\Python\Python312\site-packages\ipykernel\kernelapp.py", line 739, in start File "C:\Users\LENOVO\AppData\Roaming\Python\Python312\site-packages\tornado\platform\asyncio.py", line 205, in start File "c:\Users\LENOVO\AppData\Local\Programs\Python\Python312\Lib\asyncio\base_events.py", line 641, in run_forever File "c:\Users\LENOVO\AppData\Local\Programs\Python\Python312\Lib\asyncio\base_events.py", line 1986, in _run_once File "c:\Users\LENOVO\AppData\Local\Programs\Python\Python312\Lib\asyncio\events.py", line 88, in _run File "C:\Users\LENOVO\AppData\Roaming\Python\Python312\site-packages\ipykernel\kernelbase.py", line 545, in dispatch_queue File "C:\Users\LENOVO\AppData\Roaming\Python\Python312\site-packages\ipykernel\kernelbase.py", line 534, in process_one File "C:\Users\LENOVO\AppData\Roaming\Python\Python312\site-packages\ipykernel\kernelbase.py", line 437, in dispatch_shell File "C:\Users\LENOVO\AppData\Roaming\Python\Python312\site-packages\ipykernel\ipkernel.py", line 362, in execute_request File "C:\Users\LENOVO\AppData\Roaming\Python\Python312\site-packages\ipykernel\kernelbase.py", line 778, in execute_request File "C:\Users\LENOVO\AppData\Roaming\Python\Python312\site-packages\ipykernel\ipkernel.py", line 449, in do_execute File "C:\Users\LENOVO\AppData\Roaming\Python\Python312\site-packages\ipykernel\zmqshell.py", line 549, in run_cell File "C:\Users\LENOVO\AppData\Roaming\Python\Python312\site-packages\IPython\core\interactiveshell.py", line 3075, in run_cell File "C:\Users\LENOVO\AppData\Roaming\Python\Python312\site-packages\IPython\core\interactiveshell.py", line 3130, in _run_cell File "C:\Users\LENOVO\AppData\Roaming\Python\Python312\site-packages\IPython\core\async_helpers.py", line 128, in _pseudo_sync_runner File "C:\Users\LENOVO\AppData\Roaming\Python\Python312\site-packages\IPython\core\interactiveshell.py", line 3334, in run_cell_async File "C:\Users\LENOVO\AppData\Roaming\Python\Python312\site-packages\IPython\core\interactiveshell.py", line 3517, in run_ast_nodes File "C:\Users\LENOVO\AppData\Roaming\Python\Python312\site-packages\IPython\core\interactiveshell.py", line 3577, in run_code File "C:\Users\LENOVO\AppData\Local\Temp\ipykernel_29788\1250957458.py", line 1, in <module> File "c:\Users\LENOVO\AppData\Local\Programs\Python\Python312\Lib\site-packages\keras\src\utils\traceback_utils.py", line 117, in error_handler File "c:\Users\LENOVO\AppData\Local\Programs\Python\Python312\Lib\site-packages\keras\src\backend\tensorflow\trainer.py", line 328, in fit File "c:\Users\LENOVO\AppData\Local\Programs\Python\Python312\Lib\site-packages\keras\src\backend\tensorflow\trainer.py", line 175, in function File "c:\Users\LENOVO\AppData\Local\Programs\Python\Python312\Lib\site-packages\keras\src\backend\tensorflow\trainer.py", line 125, in multi_step_on_iterator File "c:\Users\LENOVO\AppData\Local\Programs\Python\Python312\Lib\site-packages\keras\src\backend\tensorflow\trainer.py", line 106, in one_step_on_data File "c:\Users\LENOVO\AppData\Local\Programs\Python\Python312\Lib\site-packages\keras\src\backend\tensorflow\trainer.py", line 77, in train_step File "c:\Users\LENOVO\AppData\Local\Programs\Python\Python312\Lib\site-packages\keras\src\trainers\trainer.py", line 492, in compute_metrics File "c:\Users\LENOVO\AppData\Local\Programs\Python\Python312\Lib\site-packages\keras\src\trainers\compile_utils.py", line 333, in update_state File "c:\Users\LENOVO\AppData\Local\Programs\Python\Python312\Lib\site-packages\keras\src\trainers\compile_utils.py", line 20, in update_state File "c:\Users\LENOVO\AppData\Local\Programs\Python\Python312\Lib\site-packages\keras\src\metrics\confusion_metrics.py", line 522, in update_state File "c:\Users\LENOVO\AppData\Local\Programs\Python\Python312\Lib\site-packages\keras\src\metrics\metrics_utils.py", line 592, in update_confusion_matrix_variables File "c:\Users\LENOVO\AppData\Local\Programs\Python\Python312\Lib\site-packages\keras\src\metrics\metrics_utils.py", line 565, in weighted_assign_add File "c:\Users\LENOVO\AppData\Local\Programs\Python\Python312\Lib\site-packages\keras\src\ops\numpy.py", line 3535, in logical_and File "c:\Users\LENOVO\AppData\Local\Programs\Python\Python312\Lib\site-packages\keras\src\backend\tensorflow\numpy.py", line 1506, in logical_and Incompatible shapes: [1,256] vs. [1,64] [[{{node LogicalAnd_3}}]] [Op:__inference_multi_step_on_iterator_119488] ``` My code looks like ``` datagen = ImageDataGenerator(rescale=1./255, validation_split=0.2) train_generator = datagen.flow_from_directory( alzeihmers, target_size=(120, 120), batch_size=64, class_mode='sparse', subset='training' ) val_generator = datagen.flow_from_directory( alzeihmers, target_size=(120, 120), batch_size=64, class_mode='sparse', subset='validation' ) model = tf.keras.models.Sequential([ tf.keras.layers.InputLayer(shape=(120, 120, 3)), tf.keras.layers.Conv2D(32, (3, 3), activation='relu'), tf.keras.layers.MaxPooling2D((2, 2)), tf.keras.layers.Conv2D(64, (3, 3), activation='relu'), tf.keras.layers.MaxPooling2D((2, 2)), tf.keras.layers.Conv2D(128, (3, 3), activation='relu'), tf.keras.layers.MaxPooling2D((2, 2)), tf.keras.layers.Flatten(), tf.keras.layers.Dense(128, activation='relu'), tf.keras.layers.Dropout(0.5), tf.keras.layers.Dense(4, activation='softmax') ]) model.compile( optimizer = tf.keras.optimizers.Adam(learning_rate= 1e-4), loss = tf.keras.losses.SparseCategoricalCrossentropy(), metrics = ['accuracy', 'precision', 'recall'] ) The error only occurs when using class_mode sparse in the ImageDataGenerator but works absolutely fine with categorical. What could be the problem and its subsequent solution? Continue reading...