Search code examples
pythontensorflowdeep-learning

Deep Learning Model Training Issue


I met an issue during deep learning model building and training process when I worked with Jupyter Notebook in VS code. I tried to train the deep learning model that I built using Tensorflow using model.fit method, but the issue happened.

Step1: Data Generation

import pandas as pd
import numpy as np

#Generate dummy data
np.random.seed(0)
n_products = 1000

#Dummy categories
categories = ['electronics', 'clothing', 'home', 'sports']

#Generate random products
data = {
    'name': [f'Product[i]' for i in range(n_products)],
    'description': [' '.join(np.random.choice(['good', 'awesome', 'excellent', 'fantastic'], size=10)) for _ in range(n_products)],
    'price': np.random.randint(10, 1000, size=n_products),
    'category': np.random.choice(categories, size=n_products)
}

#Create DataFrame
df = pd.DataFrame(data)
print(df)

Step2:Data preprocessing

from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder
from tensorflow.keras.preprocessing.text import Tokenizer
from tensorflow.keras.preprocessing.sequence import pad_sequences

#Tokenize text attributes
max_words = 1000
max_len = 100

tokenizer = Tokenizer(num_words = max_words)
tokenizer.fit_on_texts(df['description'])

X_text = tokenizer.texts_to_sequences(df['description'])
X_text = pad_sequences(X_text, maxlen = max_len)

#Encode categorical attribute
label_encoder = LabelEncoder()
df['category_encoded'] = label_encoder.fit_transform(df['category'])

#Split data into train and test sets
X_train_text, X_test_text, X_train_cat, X_test_cat, y_train, y_test = train_test_split(X_text, df['category_encoded'], df['price'], test_size = 0.2, random_state=42)
print("Done")
print(len(X_train_text))
print(y_train)

Step3: Model Building and Training using Tensorflow

from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Embedding, Flatten, Dense

#Define the model
model = Sequential([
    Embedding(input_dim = max_words, output_dim = 64),
    Flatten(),
    Dense(64, activation='relu'),
    Dense(4, activation='softmax') #4 categories
])

#Compile the model
model.compile(optimizer='adam',
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])

#Train the model
history = model.fit(X_train_text, y_train,
                    epochs=10,
                    batch_size=32,
                    validation_data=(X_test_text, y_test))

I got the error at step3 which is like below:

Epoch 1/10
---------------------------------------------------------------------------
InvalidArgumentError                      Traceback (most recent call last)
Cell In[13], line 18
     
    
 13 model.compile(optimizer='adam',
     14               loss='sparse_categorical_crossentropy',
     15               metrics=['accuracy'])
     17 #Train the model
---> 18 history = model.fit(X_train_text, y_train,
     19                     epochs=10,
     20                     batch_size=32,
     21                     validation_data=(X_test_text, y_test))


File c:\Users\Administrator\AppData\Local\Programs\Python\Python311\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\Administrator\AppData\Local\Programs\Python\Python311\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)


  File "c:\Users\Administrator\AppData\Local\Programs\Python\Python311\Lib\site-packages\keras\src\backend\tensorflow\nn.py", line 638, in sparse_categorical_crossentropy

Received a label value of 981 which is outside the valid range of [0, 4).  Label values: 799 555 895 885 300 152 513 436 397 525 907 616 981 939 147 724 851 857 465 953 710 764 630 521 77 234 744 169 963 916 472 237
     [[{{node compile_loss/sparse_categorical_crossentropy/SparseSoftmaxCrossEntropyWithLogits/SparseSoftmaxCrossEntropyWithLogits}}]] [Op:__inference_one_step_on_iterator_3817]
Output is truncated. View as a scrollable element or open in a text editor. Adjust cell output settings...

I thought there was an problem at data preprocessing, but I don't have idea how to solve this.


Solution

  • You got mix up an order of variables here:

    X_train_text, X_test_text, X_train_cat, X_test_cat, y_train, y_test = train_test_split(X_text, df['category_encoded'], df['price'], test_size = 0.2, random_state=42)
    

    It should be:

    X_train_text, X_test_text, y_train, y_test, X_train_cat, X_test_cat = train_test_split(X_text, df['category_encoded'], df['price'], test_size = 0.2, random_state=42)