Launch the interactive walkthrough to discover each component, including the 3D Q-Sphere Lab & Python in Colab.
Understand how mathematical loss functions measure prediction error and guide neural network optimization.
| Symbol / Term | Meaning | Beginner Interpretation |
|---|---|---|
| Ground truth (0 or 1) | Actual class label | |
| Predicted probability | Model confidence between 0 and 1 |
True label y=1. If model predicts high confidence 0.90, loss is small: -ln(0.90) = 0.105. If model confidently predicts 0.01, loss explodes: -ln(0.01) = 4.605!
Result: Confident errors are penalized 44x more severely
Network outputs continuous probabilities via Sigmoid or Softmax.
Evaluate negative log probability assigned to the ground-truth class.
Average loss scalars across the mini-batch to form scalar objective.
Problem: Evaluating classification across 1,000 competing categories.
Solution: Categorical Cross-Entropy computes loss based solely on the probability assigned to the correct category.
Problem: Predicting continuous steering wheel steering angle in degrees.
Solution: MSE loss penalizes angular deviations from human driver demonstrations.
Interactive activity component
Calculate MSE and Cross-Entropy loss using built-in PyTorch loss functions.
For Google Colab or Jupyter notebooks, run this shell command to install the required dependencies:
!pip install torch1 import torch 2 import torch.nn as nn
import torch β PyTorch library
import torch.nn as nn β Neural network loss modules
1 # 1. Mean Squared Error (Regression Example): 2 mse_loss = nn.MSELoss() 3 y_pred = torch.tensor([10.5, 20.0, 31.0]) 4 y_true = torch.tensor([10.0, 22.0, 30.0]) 5 loss_reg = mse_loss(y_pred, y_true) 6 print(f"MSE Loss: {loss_reg.item():.4f}") 7 8 # 2. Cross-Entropy Loss (Classification Example): 9 ce_loss = nn.CrossEntropyLoss() # Combines Softmax + NLLLoss internally 10 logits = torch.tensor([[2.0, 0.5, 0.1]]) # Unnormalized logits for 3 classes 11 target_class = torch.tensor([0]) # True answer is Class 0 12 loss_clf = ce_loss(logits, target_class) 13 print(f"Cross-Entropy Loss: {loss_clf.item():.4f}")
# 1. Mean Squared Error (Regression Example):
Regression loss
mse_loss = nn.MSELoss()
MSE loss criterion
y_pred = torch.tensor([10.5, 20.0, 31.0])
Continuous predictions
y_true = torch.tensor([10.0, 22.0, 30.0])
Ground truth targets
loss_reg = mse_loss(y_pred, y_true)
Compute MSE
print(f"MSE Loss: {loss_reg.item():.4f}")
Print MSE
# 2. Cross-Entropy Loss (Classification Example):
Classification loss
ce_loss = nn.CrossEntropyLoss() # Combines Softmax + NLLLoss internally
Cross-entropy criterion
logits = torch.tensor([[2.0, 0.5, 0.1]]) # Unnormalized logits for 3 classes
Model raw scores
target_class = torch.tensor([0]) # True answer is Class 0
Correct class index
loss_clf = ce_loss(logits, target_class)
Compute Cross-Entropy
print(f"Cross-Entropy Loss: {loss_clf.item():.4f}")
Print CE loss
Click "Copy for Colab", open Google Colab, paste into a single cell, and run.
1 # ========================================================= 2 # Computing Loss Functions in PyTorch 3 # Prepared for Google Colab (Run with Shift + Enter) 4 # ========================================================= 5 6 # Step 1: Install required packages 7 !pip install torch 8 9 # Step 2: Import libraries 10 import torch 11 import torch.nn as nn 12 13 # Step 3: Run Demonstration 14 # 1. Mean Squared Error (Regression Example): 15 mse_loss = nn.MSELoss() 16 y_pred = torch.tensor([10.5, 20.0, 31.0]) 17 y_true = torch.tensor([10.0, 22.0, 30.0]) 18 loss_reg = mse_loss(y_pred, y_true) 19 print(f"MSE Loss: {loss_reg.item():.4f}") 20 21 # 2. Cross-Entropy Loss (Classification Example): 22 ce_loss = nn.CrossEntropyLoss() # Combines Softmax + NLLLoss internally 23 logits = torch.tensor([[2.0, 0.5, 0.1]]) # Unnormalized logits for 3 classes 24 target_class = torch.tensor([0]) # True answer is Class 0 25 loss_clf = ce_loss(logits, target_class) 26 print(f"Cross-Entropy Loss: {loss_clf.item():.4f}")
Computes [(10.5-10)^2 + (20-22)^2 + (31-30)^2] / 3 = [0.25 + 4.0 + 1.0] / 3 = 1.75.
Applies internal Softmax and computes negative log-likelihood of class 0 (~0.28).
Regression targets and classification logits
MSE Loss: 1.7500 Cross-Entropy Loss: 0.2800
PyTorch loss modules evaluate numerical errors efficiently on GPU/CPU tensors with full autograd tracking.
CNN, MNIST dataset, backpropagation, loss functions, and optimization.
Test your understanding with these questions. Remember, this is for learning, not examination!