InvalidArgumentError: Graph execution error: (NLP)

I have used this code for Compile and Train the Model. But I am facing “Graph execution error”. Can someone help with this i don’t know how to fix this I tried solve this this but nothing is working.

def simple_rnn(input_shape , output_sequence_length , english_vocab_size , french_vocab_size):

    learning_rate = 0.005
    
    model = Sequential()
    model.add(GRU(256, input_shape = input_shape[1:] , return_sequences=True))
    model.add(TimeDistributed(Dense(1024 , activation='relu')))
    model.add(Dropout(0.5))
    model.add(TimeDistributed(Dense(french_vocab_size , activation = 'softmax')))
    
    model.compile(loss = sparse_categorical_crossentropy,
                 optimizer = Adam(learning_rate),
                 metrics = ['accuracy'])
    return model

temp_x = pad(preprocess_english_sentence , max_french_sequence_length)
temp_x = temp_x.reshape((-1, preprocess_french_sentence.shape[-2],1))

simple_rnn_model = simple_rnn(
    temp_x.shape,
    max_french_sequence_length,
    english_vocab_size,
    french_vocab_size
)

simple_rnn_model.fit(temp_x , preprocess_french_sentence , batch_size = 1024 , epochs = 10 , validation_split = 0.2)

Output :

Epoch 1/10
108/108 ━━━━━━━━━━━━━━━━━━━━ 0s 2s/step - accuracy: 0.4565 - loss: 2.6450
---------------------------------------------------------------------------
InvalidArgumentError                      Traceback (most recent call last)
Cell In[35], line 26
     17 temp_x = temp_x.reshape((-1, preprocess_french_sentence.shape[-2],1))
     19 simple_rnn_model = simple_rnn(
     20     temp_x.shape,
     21     max_french_sequence_length,
     22     english_vocab_size,
     23     french_vocab_size
     24 )
---> 26 simple_rnn_model.fit(temp_x , preprocess_french_sentence , batch_size = 1024 , epochs = 10 , validation_split = 0.2)

File ~\anaconda3\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 ~\anaconda3\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 compile_loss/sparse_categorical_crossentropy/SparseSoftmaxCrossEntropyWithLogits/SparseSoftmaxCrossEntropyWithLogits 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\SHUBHAM\anaconda3\Lib\site-packages\ipykernel_launcher.py", line 17, in <module>

  File "C:\Users\SHUBHAM\anaconda3\Lib\site-packages\traitlets\config\application.py", line 1075, in launch_instance

  File "C:\Users\SHUBHAM\anaconda3\Lib\site-packages\ipykernel\kernelapp.py", line 701, in start

  File "C:\Users\SHUBHAM\anaconda3\Lib\site-packages\tornado\platform\asyncio.py", line 205, in start

  File "C:\Users\SHUBHAM\anaconda3\Lib\asyncio\windows_events.py", line 322, in run_forever

  File "C:\Users\SHUBHAM\anaconda3\Lib\asyncio\base_events.py", line 641, in run_forever

  File "C:\Users\SHUBHAM\anaconda3\Lib\asyncio\base_events.py", line 1987, in _run_once

  File "C:\Users\SHUBHAM\anaconda3\Lib\asyncio\events.py", line 88, in _run

  File "C:\Users\SHUBHAM\anaconda3\Lib\site-packages\ipykernel\kernelbase.py", line 534, in dispatch_queue

  File "C:\Users\SHUBHAM\anaconda3\Lib\site-packages\ipykernel\kernelbase.py", line 523, in process_one

  File "C:\Users\SHUBHAM\anaconda3\Lib\site-packages\ipykernel\kernelbase.py", line 429, in dispatch_shell

  File "C:\Users\SHUBHAM\anaconda3\Lib\site-packages\ipykernel\kernelbase.py", line 767, in execute_request

  File "C:\Users\SHUBHAM\anaconda3\Lib\site-packages\ipykernel\ipkernel.py", line 429, in do_execute

  File "C:\Users\SHUBHAM\anaconda3\Lib\site-packages\ipykernel\zmqshell.py", line 549, in run_cell

  File "C:\Users\SHUBHAM\anaconda3\Lib\site-packages\IPython\core\interactiveshell.py", line 3075, in run_cell

  File "C:\Users\SHUBHAM\anaconda3\Lib\site-packages\IPython\core\interactiveshell.py", line 3130, in _run_cell

  File "C:\Users\SHUBHAM\anaconda3\Lib\site-packages\IPython\core\async_helpers.py", line 129, in _pseudo_sync_runner

  File "C:\Users\SHUBHAM\anaconda3\Lib\site-packages\IPython\core\interactiveshell.py", line 3334, in run_cell_async

  File "C:\Users\SHUBHAM\anaconda3\Lib\site-packages\IPython\core\interactiveshell.py", line 3517, in run_ast_nodes

  File "C:\Users\SHUBHAM\anaconda3\Lib\site-packages\IPython\core\interactiveshell.py", line 3577, in run_code

  File "C:\Users\SHUBHAM\AppData\Local\Temp\ipykernel_19844\2338499933.py", line 26, in <module>

  File "C:\Users\SHUBHAM\anaconda3\Lib\site-packages\keras\src\utils\traceback_utils.py", line 117, in error_handler

  File "C:\Users\SHUBHAM\anaconda3\Lib\site-packages\keras\src\backend\tensorflow\trainer.py", line 344, in fit

  File "C:\Users\SHUBHAM\anaconda3\Lib\site-packages\keras\src\utils\traceback_utils.py", line 117, in error_handler

  File "C:\Users\SHUBHAM\anaconda3\Lib\site-packages\keras\src\backend\tensorflow\trainer.py", line 432, in evaluate

  File "C:\Users\SHUBHAM\anaconda3\Lib\site-packages\keras\src\backend\tensorflow\trainer.py", line 165, in one_step_on_iterator

  File "C:\Users\SHUBHAM\anaconda3\Lib\site-packages\keras\src\backend\tensorflow\trainer.py", line 154, in one_step_on_data

  File "C:\Users\SHUBHAM\anaconda3\Lib\site-packages\keras\src\backend\tensorflow\trainer.py", line 85, in test_step

  File "C:\Users\SHUBHAM\anaconda3\Lib\site-packages\keras\src\trainers\trainer.py", line 398, in _compute_loss

  File "C:\Users\SHUBHAM\anaconda3\Lib\site-packages\keras\src\trainers\trainer.py", line 366, in compute_loss

  File "C:\Users\SHUBHAM\anaconda3\Lib\site-packages\keras\src\trainers\compile_utils.py", line 618, in __call__

  File "C:\Users\SHUBHAM\anaconda3\Lib\site-packages\keras\src\trainers\compile_utils.py", line 659, in call

  File "C:\Users\SHUBHAM\anaconda3\Lib\site-packages\keras\src\losses\loss.py", line 60, in __call__

  File "C:\Users\SHUBHAM\anaconda3\Lib\site-packages\keras\src\losses\losses.py", line 27, in call

  File "C:\Users\SHUBHAM\anaconda3\Lib\site-packages\keras\src\losses\losses.py", line 1870, in sparse_categorical_crossentropy

  File "C:\Users\SHUBHAM\anaconda3\Lib\site-packages\keras\src\ops\nn.py", line 1559, in sparse_categorical_crossentropy

  File "C:\Users\SHUBHAM\anaconda3\Lib\site-packages\keras\src\backend\tensorflow\nn.py", line 671, in sparse_categorical_crossentropy

Received a label value of 345 which is outside the valid range of [0, 345).  Label values: 7 88 .... 15 42 0 0 0
	 [[{{node compile_loss/sparse_categorical_crossentropy/SparseSoftmaxCrossEntropyWithLogits/SparseSoftmaxCrossEntropyWithLogits}}]] [Op:__inference_one_step_on_iterator_14895]

I got the solution. I just had to increment "french_vocab_size "

1 Like