It is impossible to force datasets.Dataset.to_tf_dataset() create tensors of the correct shape?!

I get this error when I start the program:

Traceback (most recent call last):
  File "C:\Users\max\Desktop\Sofia v1\main.py", line 134, in <module>
    chatbot.train_model("training_data.txt") 
  File "C:\Users\max\Desktop\Sofia v1\main.py", line 95, in train_model
    self.model.fit(X, y, epochs=10, batch_size=32, verbose=0)
  File "C:\Users\max\AppData\Local\Programs\Python\Python39\lib\site-packages\keras\src\utils\traceback_utils.py", line 122, in error_handler
    raise e.with_traceback(filtered_tb) from None
  File "C:\Users\max\AppData\Local\Programs\Python\Python39\lib\site-packages\keras\src\backend\tensorflow\nn.py", line 619, in sparse_categorical_crossentropy
    raise ValueError(
ValueError: Argument `output` must have rank (ndim) `target.ndim - 1`. Received: target.shape=(None, 100), output.shape=(None, 0)

this is part of my code:

def create_model(self):
        """Создает нейросеть для обучения."""
        model = Sequential()
        max_sequence_length = 100  # You've already defined this
        model.add(Dense(128, activation='relu', input_shape=(max_sequence_length,)))
        # Correct the output size:
        model.add(Dense(len(self.vocabulary), activation='softmax'))  
        optimizer = Adam(learning_rate=self.learning_rate)
        model.compile(loss='sparse_categorical_crossentropy', optimizer=optimizer, metrics=['accuracy'])
        return model

    def add_context(self, user_message, bot_response):
        """Добавляет контекст в список для обучения."""
        self.contexts.append((self.encode_sentence(user_message), self.encode_sentence(bot_response)))

    def train_model(self, filename):
        """Обучает нейросеть по данным из файла."""
        with open(filename, 'r', encoding='utf-8') as file:
            lines = file.readlines()
            for i in range(0, len(lines), 2):
                user_message = lines[i].strip()
                bot_response = lines[i + 1].strip()
                self.add_context(user_message, bot_response)

        # Преобразование данных для обучения
        X = [context[0] for context in self.contexts]
        y = [context[1] for context in self.contexts]

        # Дополнение последовательностей до одинаковой длины (100)
        X = pad_sequences(X, maxlen=100, padding='post', truncating='post') 
        y = pad_sequences(y, maxlen=100, padding='post', truncating='post')  

        # Обучение модели
        self.model.fit(X, y, epochs=10, batch_size=32, verbose=0)

I’m new to tensorflow and python so please keep it simple with me

Hi @Mazz1lla, The error is due to the mismatch between the target variable (labels) and the model’s output. Make sure that the model output shape matches the target shape. Thank You.