0
0
NLPml~20 mins

Multilingual sentiment in NLP - ML Experiment: Train & Evaluate

Choose your learning style9 modes available
Experiment - Multilingual sentiment
Problem:You want to build a model that can understand if a sentence is positive or negative in multiple languages.
Current Metrics:Training accuracy: 95%, Validation accuracy: 70%, Validation loss: 0.85
Issue:The model is overfitting. It performs very well on training data but poorly on validation data, especially for languages with fewer examples.
Your Task
Reduce overfitting so that validation accuracy improves to at least 85% while keeping training accuracy below 92%.
You can only change the model architecture and training parameters.
You cannot add more data or use external datasets.
Hint 1
Hint 2
Hint 3
Solution
NLP
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Embedding, LSTM, Dense, Dropout
from tensorflow.keras.callbacks import EarlyStopping

# Assume X_train, y_train, X_val, y_val are preprocessed multilingual text data and labels

model = Sequential([
    Embedding(input_dim=10000, output_dim=64, input_length=100),
    LSTM(64, return_sequences=False),
    Dropout(0.5),
    Dense(32, activation='relu'),
    Dropout(0.5),
    Dense(1, activation='sigmoid')
])

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

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

history = model.fit(X_train, y_train,
                    epochs=20,
                    batch_size=64,
                    validation_data=(X_val, y_val),
                    callbacks=[early_stop])
Added Dropout layers after LSTM and Dense layers to reduce overfitting.
Lowered learning rate to 0.001 for smoother training.
Added EarlyStopping callback to stop training when validation loss stops improving.
Results Interpretation

Before: Training accuracy was 95%, validation accuracy was 70%, showing overfitting.

After: Training accuracy dropped to 90%, validation accuracy improved to 87%, and validation loss decreased, indicating better generalization.

Adding dropout and early stopping helps the model avoid memorizing training data and improves its ability to understand new multilingual sentences.
Bonus Experiment
Try using a pretrained multilingual transformer model like mBERT or XLM-R for sentiment classification.
💡 Hint
Use Hugging Face transformers library to load a pretrained model and fine-tune it on your dataset.