Practice
import torch
optimizer = torch.optim.SGD(model.parameters(), lr=0.1)
for epoch in range(10):
for data, target in dataloader:
optimizer.zero_grad()
output = model(data)
loss = loss_fn(output, target)
loss.backward()
optimizer.step()
print(optimizer.param_groups[0]['lr'])What will be printed after the loop?Solution
Step 1: Understand the code.
The learning rate is set to 0.1 and not changed in the loop.Step 2: Check for learning rate updates.
SGD without a scheduler keeps the learning rate constant.Final Answer:
Option C -> Option CQuick Check:
SGD without scheduler keeps lr constant. [check]
from tokenizers import Tokenizer
from tokenizers.models import BPE
from tokenizers.pre_tokenizers import Whitespace
# Initialize a BPE tokenizer
bpe_tokenizer = Tokenizer(BPE())
bpe_tokenizer.pre_tokenizer = Whitespace()
# Train the tokenizer
bpe_tokenizer.train(["hello world", "hello there"], vocab_size=10)
# Encode a sentence
output = bpe_tokenizer.encode("hello world")
print(output.tokens)Solution
Step 1: Understand BPE tokenization with whitespace pre-tokenization.
BPE with whitespace pre-tokenization will initially split on spaces.Step 2: Analyze the output of the encode method.
The sentence 'hello world' is tokenized as ['hello', 'world'] due to whitespace splitting.Final Answer:
Option D -> Option DQuick Check:
BPE with whitespace splits on spaces first. [OK]
Solution
Step 1: Identify common issues in logistic regression.
Feature scaling is crucial for convergence in logistic regression, especially with gradient-based optimization.Step 2: Eliminate wrong options.
The model is overfitting due to too many features. is less likely as logistic regression is less prone to overfitting. The model is underfitting because of insufficient data. is incorrect because underfitting is more about model complexity. The learning rate is too high, causing divergence. is incorrect as logistic regression typically uses small learning rates.Final Answer:
Option A -> Option AQuick Check:
Check feature scaling for convergence issues [OK]
Solution
Step 1: Identify the symptom.
Bias against a minority group indicates potential data imbalance.Step 2: Analyze the options.
Imbalanced data (B) is a common cause of bias. Other options relate to model complexity or training dynamics, not bias directly.Final Answer:
Option A -> Option AQuick Check:
Imbalanced data often leads to bias. [OK]
Solution
Step 1: Identify the key trade-off in production.
In production, inference latency is crucial as it affects user experience.Step 2: Eliminate less relevant trade-offs.
Model accuracy vs. training time is more relevant during development, not deployment. Data size vs. interpretability and feature engineering vs. hyperparameter tuning are less critical in production.Final Answer:
Option B -> Option BQuick Check:
Latency impacts user experience in production. [OK]
