Bird
Raised Fist0
ML Pythonml~20 mins

Why NLP processes human language in ML Python - Experiment to Prove It

Choose your learning style10 modes available
Experiment - Why NLP processes human language
Problem:We want to teach a computer to understand simple human sentences and classify their meaning, like recognizing if a sentence is positive or negative.
Current Metrics:Training accuracy: 98%, Validation accuracy: 70%
Issue:The model is overfitting: it performs very well on training data but poorly on new, unseen sentences.
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 change the dataset.
Hint 1
Hint 2
Hint 3
Solution
ML Python
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Embedding, LSTM, Dense, Dropout
from tensorflow.keras.preprocessing.text import Tokenizer
from tensorflow.keras.preprocessing.sequence import pad_sequences
from tensorflow.keras.callbacks import EarlyStopping

# Sample data
sentences = [
    'I love this movie',
    'This film is terrible',
    'What a great day',
    'I hate this song',
    'This is amazing',
    'I dislike this book',
    'Such a wonderful experience',
    'This is bad',
    'I enjoy this',
    'This is awful'
]
labels = [1, 0, 1, 0, 1, 0, 1, 0, 1, 0]  # 1=positive, 0=negative

# Tokenize and pad
tokenizer = Tokenizer(num_words=1000, oov_token='<OOV>')
tokenizer.fit_on_texts(sentences)
sequences = tokenizer.texts_to_sequences(sentences)
padded = pad_sequences(sequences, padding='post', maxlen=5)

# Split data
X_train, X_val = padded[:8], padded[8:]
y_train, y_val = labels[:8], labels[8:]

# Build model with dropout and smaller LSTM
model = Sequential([
    Embedding(input_dim=1000, output_dim=16, input_length=5),
    LSTM(16, return_sequences=False),
    Dropout(0.5),
    Dense(8, activation='relu'),
    Dropout(0.5),
    Dense(1, activation='sigmoid')
])

model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])

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

# Train
history = model.fit(X_train, y_train, epochs=30, batch_size=2, validation_data=(X_val, y_val), callbacks=[early_stop], verbose=0)

# Evaluate
train_loss, train_acc = model.evaluate(X_train, y_train, verbose=0)
val_loss, val_acc = model.evaluate(X_val, y_val, verbose=0)

print(f'Training accuracy: {train_acc*100:.2f}%')
print(f'Validation accuracy: {val_acc*100:.2f}%')
Added Dropout layers after LSTM and Dense layers to reduce overfitting.
Reduced LSTM units from a larger number to 16 to simplify the model.
Added EarlyStopping callback to stop training when validation loss stops improving.
Kept batch size small and limited epochs to prevent over-training.
Results Interpretation

Before: Training accuracy: 98%, Validation accuracy: 70% (overfitting)

After: Training accuracy: 90%, Validation accuracy: 87% (better generalization)

Adding dropout and early stopping helps the model not memorize training data too much, so it performs better on new sentences. This shows how to reduce overfitting in NLP tasks.
Bonus Experiment
Try using a simpler model like a Dense neural network without LSTM and compare the results.
💡 Hint
Replace the LSTM layer with a Flatten layer followed by Dense layers and observe if the model still learns sentence sentiment.

Practice

(1/5)
1. Why do we use Natural Language Processing (NLP) in computers?
easy
A. To store large amounts of data
B. To make computers run faster
C. To help computers understand and work with human language
D. To improve computer hardware

Solution

  1. Step 1: Understand the purpose of NLP

    NLP is designed to let computers understand human language, like English or Spanish.
  2. Step 2: Compare options to NLP's goal

    Only To help computers understand and work with human language matches this goal. Other options relate to speed, storage, or hardware, which NLP does not focus on.
  3. Final Answer:

    To help computers understand and work with human language -> Option C
  4. Quick Check:

    NLP purpose = understanding language [OK]
Hint: NLP = computers + human language understanding [OK]
Common Mistakes:
  • Confusing NLP with hardware improvements
  • Thinking NLP speeds up computers
  • Mixing NLP with data storage tasks
2. Which of the following is the correct way to split a sentence into words in Python for NLP?
easy
A. sentence.add()
B. sentence.join()
C. sentence.remove()
D. sentence.split()

Solution

  1. Step 1: Recall Python string methods for splitting text

    The method split() breaks a string into a list of words by spaces.
  2. Step 2: Check other options for correctness

    join() combines words, add() and remove() are not string methods.
  3. Final Answer:

    sentence.split() -> Option D
  4. Quick Check:

    Split text = split() method [OK]
Hint: Split text into words with split() [OK]
Common Mistakes:
  • Using join() instead of split()
  • Trying to add or remove on strings
  • Confusing string methods with list methods
3. What will be the output of this Python code snippet used in NLP?
text = "I love AI"
words = text.split()
print(len(words))
medium
A. 3
B. 1
C. 4
D. Error

Solution

  1. Step 1: Split the text into words

    The string "I love AI" split by spaces gives the list ["I", "love", "AI"].
  2. Step 2: Count the number of words

    The list has 3 elements, so len(words) returns 3.
  3. Final Answer:

    3 -> Option A
  4. Quick Check:

    Words count = 3 [OK]
Hint: Count words by splitting and using len() [OK]
Common Mistakes:
  • Counting characters instead of words
  • Expecting 4 because of spaces
  • Thinking split() returns a string
4. Find the error in this NLP code snippet:
sentence = "Hello world"
words = sentence.split
print(words)
medium
A. Wrong variable name for sentence
B. Missing parentheses after split method
C. print() cannot print lists
D. split() should be replaced with join()

Solution

  1. Step 1: Check method usage

    The method split needs parentheses to be called: split().
  2. Step 2: Verify other options

    Variable name is correct, print() can print lists, and join() is not for splitting.
  3. Final Answer:

    Missing parentheses after split method -> Option B
  4. Quick Check:

    Method call needs () [OK]
Hint: Remember to add () when calling methods [OK]
Common Mistakes:
  • Forgetting parentheses on method calls
  • Confusing split() with join()
  • Thinking print can't show lists
5. You want to build a simple NLP system to detect if a sentence expresses happiness or sadness. Which step is most important to start with?
hard
A. Break the sentence into words and check for happy or sad words
B. Train a computer to run faster
C. Store the sentence in a database without changes
D. Change the sentence to uppercase only

Solution

  1. Step 1: Understand the goal of sentiment detection

    Detecting happiness or sadness means analyzing the meaning of words in the sentence.
  2. Step 2: Identify the first NLP step

    Breaking the sentence into words allows checking for keywords that show emotion.
  3. Final Answer:

    Break the sentence into words and check for happy or sad words -> Option A
  4. Quick Check:

    Sentiment detection starts with word analysis [OK]
Hint: Start by splitting text to find emotion words [OK]
Common Mistakes:
  • Focusing on speed instead of meaning
  • Ignoring word-level analysis
  • Changing case without purpose
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard