Bird
Raised Fist0
NLPml~10 mins

Model selection for tasks in NLP - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import the model used for text classification.

NLP
from sklearn.[1] import LogisticRegression
Drag options to blanks, or click blank then click option'
Acluster
Blinear_model
Censemble
Ddecomposition
Attempts:
3 left
💡 Hint
Common Mistakes
Choosing 'ensemble' which contains tree-based models.
Choosing 'cluster' which is for clustering algorithms.
2fill in blank
medium

Complete the code to create a model for sentiment analysis using a pretrained transformer.

NLP
from transformers import [1]
model = [1].from_pretrained('distilbert-base-uncased-finetuned-sst-2-english')
Drag options to blanks, or click blank then click option'
ADistilBertForSequenceClassification
BBertModel
CGPT2Model
DRobertaModel
Attempts:
3 left
💡 Hint
Common Mistakes
Using BertModel which is a base model without classification head.
Using GPT2Model which is for text generation.
3fill in blank
hard

Fix the error in the code to select the right model for named entity recognition (NER).

NLP
from transformers import AutoModelFor[1]
model = AutoModelFor[1].from_pretrained('dbmdz/bert-large-cased-finetuned-conll03-english')
Drag options to blanks, or click blank then click option'
ASequenceClassification
BQuestionAnswering
CTokenClassification
DMaskedLM
Attempts:
3 left
💡 Hint
Common Mistakes
Using SequenceClassification which is for whole sentence labels.
Using MaskedLM which is for language modeling.
4fill in blank
hard

Fill both blanks to create a pipeline for text summarization using Hugging Face.

NLP
from transformers import pipeline
summarizer = pipeline(task=[1], model=[2])
Drag options to blanks, or click blank then click option'
A'summarization'
B'translation_en_to_fr'
C'facebook/bart-large-cnn'
D'text-generation'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'translation_en_to_fr' which is for translation.
Using 'text-generation' which is for generating text, not summarizing.
5fill in blank
hard

Fill all three blanks to build and evaluate a simple text classification model using scikit-learn.

NLP
from sklearn.feature_extraction.text import [1]
from sklearn.linear_model import [2]
from sklearn.metrics import [3]

vectorizer = [1]()
X_train_vec = vectorizer.fit_transform(X_train)
model = [2]()
model.fit(X_train_vec, y_train)
y_pred = model.predict(vectorizer.transform(X_test))
score = [3](y_test, y_pred)
Drag options to blanks, or click blank then click option'
ACountVectorizer
BLogisticRegression
Caccuracy_score
DTfidfVectorizer
Attempts:
3 left
💡 Hint
Common Mistakes
Using TfidfVectorizer instead of CountVectorizer (both work but only one is correct here).
Using other metrics like precision instead of accuracy_score.

Practice

(1/5)
1. Which type of model is best suited for a task where you want to classify emails as spam or not spam?
easy
A. A regression model
B. A clustering model
C. A classification model
D. A dimensionality reduction model

Solution

  1. Step 1: Understand the task type

    Classifying emails as spam or not spam is a task where the output is a category or label.
  2. Step 2: Match task to model type

    Classification models are designed to assign categories to inputs, so they fit this task best.
  3. Final Answer:

    A classification model -> Option C
  4. Quick Check:

    Classification = Spam detection [OK]
Hint: Classification = sorting into groups or labels [OK]
Common Mistakes:
  • Confusing classification with regression
  • Choosing clustering which is unsupervised
  • Selecting dimensionality reduction which is for feature simplification
2. Which of the following is the correct way to import a pre-trained BERT model using the Hugging Face Transformers library in Python?
easy
A. from transformers import BertModel; model = BertModel.from_pretrained('bert-base-uncased')
B. import bert; model = bert.load('bert-base-uncased')
C. from transformers import BERT; model = BERT.load_pretrained('bert-base-uncased')
D. import transformers; model = transformers.Bert.load('bert-base-uncased')

Solution

  1. Step 1: Recall correct import syntax

    The Hugging Face library uses 'from transformers import BertModel' to import the BERT model class.
  2. Step 2: Use the correct method to load pre-trained weights

    The method to load pre-trained weights is 'from_pretrained' called on the model class.
  3. Final Answer:

    from transformers import BertModel; model = BertModel.from_pretrained('bert-base-uncased') -> Option A
  4. Quick Check:

    Correct import and method = from transformers import BertModel; model = BertModel.from_pretrained('bert-base-uncased') [OK]
Hint: Use 'from transformers import ModelName' and 'from_pretrained' method [OK]
Common Mistakes:
  • Using incorrect class names like BERT instead of BertModel
  • Calling non-existent methods like load_pretrained
  • Wrong import statements or missing 'from'
3. Consider the following Python code using scikit-learn for sentiment analysis. What will be the printed accuracy after training and testing the model?
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split

texts = ['I love this movie', 'This movie is bad', 'Amazing film', 'Terrible acting', 'Great plot', 'Worst movie ever']
labels = [1, 0, 1, 0, 1, 0]

X_train, X_test, y_train, y_test = train_test_split(texts, labels, test_size=0.33, random_state=42)

vectorizer = CountVectorizer()
X_train_vec = vectorizer.fit_transform(X_train)
X_test_vec = vectorizer.transform(X_test)

model = LogisticRegression()
model.fit(X_train_vec, y_train)

accuracy = model.score(X_test_vec, y_test)
print(round(accuracy, 2))
medium
A. 0.67
B. 1.0
C. 0.33
D. 0.5

Solution

  1. Step 1: Understand data split and labels

    With random_state=42 and test_size=0.33, the test set contains 2 samples: 'Amazing film' (label 1) and 'Worst movie ever' (label 0).
  2. Step 2: Predict and check accuracy

    Logistic Regression on simple text features predicts one sample correctly and one incorrectly, resulting in accuracy 0.5.
  3. Final Answer:

    0.5 -> Option D
  4. Quick Check:

    Accuracy = 1 correct / 2 total = 0.5 [OK]
Hint: Accuracy = correct predictions / total test samples [OK]
Common Mistakes:
  • Assuming perfect accuracy without checking data split
  • Confusing training accuracy with test accuracy
  • Ignoring random_state effect on split
4. You tried to train a text classification model but got an error: ValueError: Found input variables with inconsistent numbers of samples. What is the most likely cause?
medium
A. The learning rate is too high
B. The input texts and labels have different lengths
C. The model architecture is incorrect
D. The tokenizer is missing

Solution

  1. Step 1: Understand the error message

    The error says input variables have inconsistent sample counts, meaning the number of texts and labels differ.
  2. Step 2: Identify cause in data preparation

    If texts and labels lists have different lengths, model training fails because each input needs a matching label.
  3. Final Answer:

    The input texts and labels have different lengths -> Option B
  4. Quick Check:

    Mismatch in input and label counts causes this error [OK]
Hint: Check if inputs and labels have same number of samples [OK]
Common Mistakes:
  • Blaming model architecture for data size errors
  • Ignoring data preprocessing steps
  • Assuming learning rate or tokenizer causes this error
5. You want to build a model to generate text summaries from long articles. Which model type should you select for this task?
hard
A. Sequence-to-sequence model with attention
B. Binary classification model
C. Clustering model
D. Regression model

Solution

  1. Step 1: Understand the task requirements

    Text summarization requires converting a long sequence (article) into a shorter sequence (summary).
  2. Step 2: Identify suitable model architecture

    Sequence-to-sequence models with attention are designed to handle input and output sequences of different lengths effectively.
  3. Final Answer:

    Sequence-to-sequence model with attention -> Option A
  4. Quick Check:

    Summarization = sequence-to-sequence with attention [OK]
Hint: Summarization needs sequence-to-sequence models [OK]
Common Mistakes:
  • Choosing classification or regression which don't generate sequences
  • Selecting clustering which is unsupervised grouping
  • Ignoring the need for attention in long text tasks
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