model = Sequential() model.add(LSTM(units=64, inputshape=(maxlength, 1)))…
- User
model = Sequential() model.add(LSTM(units=64, input_shape=(max_length, 1))) model.add(Dense(units=10, activation='softmax'))
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
Reshape the input data for the LSTM
reshaped_data_train = np.reshape(padded_data_train, (padded_data_train.shape[0], max_length, 1)) reshaped_data_val = np.reshape(padded_data_val, (padded_data_val.shape[0], max_length, 1))
Train the model
model.fit(reshaped_data_train, target_train, validation_data=(reshaped_data_val, target_val), epochs=10, batch_size=32)
Fix the error
Epoch 1/10
InvalidArgumentError Traceback (most recent call last) Cell In[67], line 12 9 reshaped_data_val = np.reshape(padded_data_val, (padded_data_val.shape[0], max_length, 1)) 11 # Train the model ---> 12 model.fit(reshaped_data_train, target_train, validation_data=(reshaped_data_val, target_val), epochs=10, batch_size=32)
File ~\AppData\Local\anaconda3\Lib\site-packages\keras\src\utils\traceback_utils.py:70, in filter_traceback..error_handler(*args, **kwargs) 67 filtered_tb = _process_traceback_frames(e.traceback) 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_tbFile ~\AppData\Local\anaconda3\Lib\site-packages\tensorflow\python\eager\execute.py:60, in quick_execute(op_name, num_outputs, inputs, attrs, ctx, name) 53 # Convert any objects of type core_types.Tensor to Tensor. 54 inputs = [ 55 tensor_conversion_registry.convert(t) 56 if isinstance(t, core_types.Tensor) 57 else t 58 for t in inputs 59 ] ---> 60 tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name, 61 inputs, attrs, num_outputs) 62 except core._NotOkStatusException as e: 63 if name is not None:
InvalidArgumentError: Graph execution error:
Detected at node sparse_categorical_crossentropy/SparseSoftmaxCrossEntropyWithLogits/SparseSoftmaxCrossEntropyWithLogits defined at (most recent call last): File "", line 198, in _run_module_as_main
File "", line 88, in _run_code
File "C:\Users\snoale\AppData\Local\anaconda3\Lib\site-packages\ipykernel_launcher.py", line 17, in
File "C:\Users\snoale\AppData\Local\anaconda3\Lib\site-packages\traitlets\config\application.py", line 992, in launch_instance
File "C:\Users\snoale\AppData\Local\anaconda3\Lib\site-packages\ipykernel\kernelapp.py", line 711, in start
File "C:\Users\snoale\AppData\Local\anaconda3\Lib\site-packages\tornado\platform\asyncio.py", line 195, in start
File "C:\Users\snoale\AppData\Local\anaconda3\Lib\asyncio\base_events.py", line 607, in run_forever
- Assistant
The error message suggests that there is a dimension mismatch between the logits (output of the model) and the labels (target values).
This specific error occurs when the number of samples in the training dataset does not match the number of samples in the target dataset. In this case, the model expects 32 samples in each batch, but the target dataset has 320 samples.
To fix this error, ensure that the number of samples in the target dataset matches the number of samples in the training dataset. Here are a few possible solutions:
-
Validate the shape of the target dataset: Check the shape of
target_trainandtarget_valvariables. Make sure they have the same number of samples as the corresponding input data arraysreshaped_data_trainandreshaped_data_val. -
Check the preprocessing steps: Ensure that the data preprocessing steps, such as padding and reshaping, are correctly applied to both the input data and the target data.
-
Verify the data loading process: If the data is loaded from external files, check the data loading process to ensure that the correct target values are associated with each input sample.
-