0
0
NLPml~20 mins

Multi-class text classification in NLP - ML Experiment: Train & Evaluate

Choose your learning style9 modes available
Experiment - Multi-class text classification
Problem:Classify news articles into one of five categories using a neural network.
Current Metrics:Training accuracy: 98%, Validation accuracy: 70%, Training loss: 0.05, Validation loss: 1.2
Issue:The model is overfitting: training accuracy is very high but validation accuracy is much lower.
Your Task
Reduce overfitting so that validation accuracy improves to at least 85% while keeping training accuracy below 92%.
You can only modify the model architecture and training hyperparameters.
Do not change the dataset or preprocessing steps.
Hint 1
Hint 2
Hint 3
Hint 4
Solution
NLP
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Embedding, GlobalAveragePooling1D, Dense, Dropout
from tensorflow.keras.callbacks import EarlyStopping

# Assume X_train, y_train, X_val, y_val are preprocessed and ready

model = Sequential([
    Embedding(input_dim=10000, output_dim=64, input_length=100),
    GlobalAveragePooling1D(),
    Dropout(0.5),
    Dense(64, activation='relu'),
    Dropout(0.5),
    Dense(5, activation='softmax')
])

model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.001),
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])

early_stop = EarlyStopping(monitor='val_loss', patience=3, restore_best_weights=True)

history = model.fit(X_train, y_train,
                    epochs=30,
                    batch_size=32,
                    validation_data=(X_val, y_val),
                    callbacks=[early_stop])
Added dropout layers with rate 0.5 after embedding and dense layers to reduce overfitting.
Reduced learning rate to 0.001 for smoother training.
Added early stopping to stop training when validation loss stops improving.
Simplified model by using one dense layer with 64 neurons instead of more complex architecture.
Results Interpretation

Before: Training accuracy 98%, Validation accuracy 70%, Training loss 0.05, Validation loss 1.2

After: Training accuracy 90%, Validation accuracy 87%, Training loss 0.25, Validation loss 0.45

Adding dropout and early stopping helps reduce overfitting by preventing the model from memorizing training data, leading to better validation performance.
Bonus Experiment
Try using a pretrained word embedding like GloVe instead of training embeddings from scratch.
💡 Hint
Load GloVe embeddings and set them as weights in the embedding layer with trainable=False to leverage prior knowledge.