loss function

Measures the difference between model predictions and true labels. The training process minimizes this value using optimization algorithms.

Syntax

ai-fundamentals
MSE = mean((y_pred - y_true)^2)
CrossEntropy = -sum(y_true * log(y_pred))

Example

ai-fundamentals
# PyTorch loss functions:
import torch.nn as nn

# Regression:
mse_loss = nn.MSELoss()
loss = mse_loss(predictions, targets)

# Classification:
ce_loss = nn.CrossEntropyLoss()
loss = ce_loss(logits, labels)

# Binary classification:
bce_loss = nn.BCEWithLogitsLoss()
loss = bce_loss(logits, labels.float())