What is the purpose of a loss function in machine learning?
A loss function measures how well the model's predictions match the true values. It helps the model learn by showing how much it is wrong.
Click to reveal answer
beginner
What does MSELoss stand for and when is it used?
MSELoss stands for Mean Squared Error Loss. It is used for regression tasks where the goal is to predict continuous values.
Click to reveal answer
beginner
What type of problems is CrossEntropyLoss used for?
CrossEntropyLoss is used for classification problems, especially when predicting categories or classes.
Click to reveal answer
intermediate
How does MSELoss calculate the error?
MSELoss calculates the average of the squares of the differences between predicted and true values. Squaring makes bigger errors count more.
Click to reveal answer
intermediate
Why is CrossEntropyLoss suitable for classification?
CrossEntropyLoss compares the predicted probabilities with the true class labels using a formula that penalizes wrong predictions more when the model is confident but wrong.
Click to reveal answer
Which loss function is best for predicting house prices?
ABinary Cross Entropy
BMSELoss
CHinge Loss
DCrossEntropyLoss
✗ Incorrect
MSELoss is used for regression tasks like predicting house prices, which are continuous values.
CrossEntropyLoss is mainly used for:
AClassification problems
BRegression problems
CClustering problems
DDimensionality reduction
✗ Incorrect
CrossEntropyLoss is designed for classification tasks where the output is a category.
What does MSELoss penalize more?
ASmall errors
BCorrect predictions
CLarge errors
DRandom noise
✗ Incorrect
MSELoss squares the errors, so larger errors have a bigger penalty.
In PyTorch, which loss function expects raw scores (logits) as input?
AMSELoss
BHuber Loss
CL1Loss
DCrossEntropyLoss
✗ Incorrect
CrossEntropyLoss expects raw scores (logits) and applies softmax internally.
Which loss function would you use for a multi-class classification problem?
ACrossEntropyLoss
BMSELoss
CBinary Cross Entropy
DMean Absolute Error
✗ Incorrect
CrossEntropyLoss is suitable for multi-class classification problems.
Explain the difference between MSELoss and CrossEntropyLoss and when to use each.
Think about the type of output your model predicts: numbers or categories.
You got /4 concepts.
Describe how CrossEntropyLoss works with model outputs and true labels.
Consider how the loss handles probabilities and class labels.
You got /4 concepts.
Practice
(1/5)
1. Which loss function in PyTorch is best suited for predicting continuous values like house prices?
easy
A. torch.nn.MSELoss
B. torch.nn.CrossEntropyLoss
C. torch.nn.BCELoss
D. torch.nn.NLLLoss
Solution
Step 1: Understand the type of prediction
Predicting house prices is a regression problem where output is continuous.
Step 2: Match loss function to problem type
MSELoss calculates mean squared error, ideal for continuous outputs. CrossEntropyLoss is for classification, not regression.
Final Answer:
torch.nn.MSELoss -> Option A
Quick Check:
Regression uses MSELoss = C [OK]
Hint: Use MSELoss for continuous outputs, CrossEntropyLoss for classes [OK]
Common Mistakes:
Choosing CrossEntropyLoss for regression tasks
Confusing BCELoss with MSELoss
Using NLLLoss without log probabilities
2. Which of the following is the correct way to create a CrossEntropyLoss object in PyTorch?
easy
A. loss = torch.nn.CrossEntropy()
B. loss = torch.nn.MSELoss()
C. loss = torch.nn.CrossEntropyLoss()
D. loss = torch.CrossEntropyLoss()
Solution
Step 1: Recall PyTorch loss function syntax
Loss functions are in torch.nn and instantiated with parentheses.
Step 2: Identify correct class name and module
CrossEntropyLoss is the correct class name under torch.nn. Options A misses 'Loss' and C misses 'nn'.
Final Answer:
loss = torch.nn.CrossEntropyLoss() -> Option C
Quick Check:
Correct class and module = D [OK]
Hint: Use torch.nn.CrossEntropyLoss() exactly for classification loss [OK]
Common Mistakes:
Missing parentheses when creating loss object
Using torch.CrossEntropyLoss() instead of torch.nn.CrossEntropyLoss()
Confusing MSELoss with CrossEntropyLoss
3. What will be the output loss value when using torch.nn.MSELoss on predictions tensor([2.0, 3.0]) and targets tensor([1.0, 5.0])?
Hint: Square errors, then average for MSELoss output [OK]
Common Mistakes:
Forgetting to square the differences
Summing without averaging
Mixing up prediction and target order
4. Given this code snippet, what is the error?
import torch
loss_fn = torch.nn.CrossEntropyLoss()
pred = torch.tensor([[0.2, 0.8], [0.6, 0.4]])
target = torch.tensor([1, 0])
loss = loss_fn(pred, target)
print(loss.item())
medium
A. Loss function should be MSELoss for this target
B. Target tensor should be one-hot encoded
C. No error, code runs correctly
D. Predictions should be raw scores (logits), not probabilities
Solution
Step 1: Check input requirements for CrossEntropyLoss
CrossEntropyLoss expects raw scores (logits), not probabilities.
Step 2: Analyze given predictions
Predictions are probabilities (sum to 1), so applying CrossEntropyLoss directly causes incorrect loss calculation.
Final Answer:
Predictions should be raw scores (logits), not probabilities -> Option D
Quick Check:
CrossEntropyLoss needs logits = A [OK]
Hint: Use logits, not probabilities, with CrossEntropyLoss [OK]
Common Mistakes:
Passing probabilities instead of logits to CrossEntropyLoss
Using one-hot targets instead of class indices
Confusing MSELoss with CrossEntropyLoss for classification
5. You have a multi-class classification problem with 4 classes. Your model outputs raw scores (logits) of shape (batch_size, 4). Which loss function and target format should you use in PyTorch?
hard
A. Use torch.nn.MSELoss with target as one-hot vectors
B. Use torch.nn.CrossEntropyLoss with target as class indices (0 to 3)
C. Use torch.nn.BCELoss with target as class indices
D. Use torch.nn.CrossEntropyLoss with target as one-hot vectors
Solution
Step 1: Identify loss function for multi-class classification
CrossEntropyLoss is designed for multi-class classification with logits.
Step 2: Determine correct target format
CrossEntropyLoss expects target as class indices (integers), not one-hot vectors.
Final Answer:
Use torch.nn.CrossEntropyLoss with target as class indices (0 to 3) -> Option B
Quick Check:
CrossEntropyLoss + class indices = B [OK]
Hint: CrossEntropyLoss needs logits and integer class labels [OK]
Common Mistakes:
Passing one-hot vectors as targets to CrossEntropyLoss
Using MSELoss for classification
Using BCELoss for multi-class without sigmoid
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.