Based on the MLS-C01 exam guideReport an error
┌──────────────── 1 step ────────────────┐
Data →│ Sample batch → Forward → Compute loss → Backprop (gradients) → Update weights │→ Next step
└──────────────────────────────────────────────┘
↑batch size ↑activation fn ↑vanishing/exploding ↑learning rate/schedule
Batch size : Number of samples used in one update → affects memory, GPU utilization, generalization
Learning rate : How far weights move in one step → separates divergence from stalling
Gradient : Multiplied back through the layers, vanishing to 0 (vanishing) or blowing up (exploding)
1 epoch : One full pass over the training data = (number of samples / batch size) stepsThe number of samples used in one weight update.
| Batch size | Advantages | Disadvantages |
|---|---|---|
| Small (e.g., 32) | Noise helps generalization, lower memory | Frequent updates can be slow, noisy |
| Large (e.g., 1024+) | Better GPU utilization, stable updates | Higher memory, may miss flat minima and generalize worse |
| Combination | Convergence speed | Memory | Generalization | When to choose |
|---|---|---|---|---|
| Small batch + small learning rate | Slow | Low | Generally good | GPU memory is tight and stability comes first |
| Small batch + large learning rate | Unstable | Low | Risk of divergence | Not recommended (worst mix of noise + big steps) |
| Large batch + large learning rate | Fast | High | Needs tuning | Raising throughput on large GPUs (linear scaling rule of thumb) |
| Large batch + small learning rate | Very slow | High | Risk of stalling | The classic mistake of raising only the batch without raising the learning rate |
One view is that the gradient noise of small batches helps the model escape sharp minima of the loss surface and head toward flatter regions that generalize better. Conversely, a very large batch gives accurate, stable gradients but loses this exploratory quality, so generalization can drop. That is why batch size is not merely a speed issue but an optimization-versus-generalization trade-off.
The learning rate is how far the weights move in one step. It is the most sensitive hyperparameter in training.
Too large → loss diverges or oscillates (NaN loss)
Too small → convergence is very slow and gets stuck in local minima
Right → decreases steadily| Schedule | Behavior | When to use |
|---|---|---|
| Step decay | Decreases by a fixed ratio every fixed number of epochs | When you need simple, predictable decay |
| Exponential decay | Decreases gradually, exponentially | When you want a smooth, continuous reduction |
| Cosine annealing | Decreases smoothly along a cosine curve | When you want to preserve fine adjustment in the later stages |
| Warmup | Starts with a small learning rate and gradually raises it | Suppresses early instability — common in large models and Transformers |
Distinguishing signals:
These are problems caused by gradients being multiplied during backpropagation in deep neural networks.
| Problem | Symptom | Response |
|---|---|---|
| Vanishing gradient | Early layers barely learn, loss stalls | ReLU-family activations, batch normalization, residual connections (ResNet), proper weight initialization |
| Exploding gradient | Loss NaN/diverges, weights surge | Gradient clipping, lower learning rate, batch normalization |
| Symptom | Most likely cause | First remedy | Response often used as a wrong option |
|---|---|---|---|
| Loss is NaN within a few steps | Learning rate too high / exploding gradients | Lower learning rate + gradient clipping | Reduce batch to 1, remove all normalization |
| Loss just oscillates widely | Learning rate too high | Lower learning rate, introduce a schedule (cosine/step) | Just adding epochs |
| Loss flat from the start, early-layer weights frozen | Vanishing gradients | ReLU family, batch normalization, residual connections, initialization | Replace all activations with sigmoid (makes it worse) |
| Loss decreases only very slowly | Learning rate too low | Raise learning rate, warmup then decay | Making the model even bigger |
| OOM right after training starts | Batch size too large / model size | Reduce batch, gradient accumulation, mixed precision | Adjusting the learning rate (irrelevant) |
| Loss decreases but GPU utilization is 30% | Data loading / I/O bottleneck | Identify the bottleneck with Profiler, then improve the pipeline | Touching learning rate/regularization (irrelevant) |
hyperparameters = {
"gradient_clipping": 1.0, # upper bound on the gradient norm
"lr_scheduler": "cosine",
}Sigmoid and tanh have derivatives close to 0 in their saturated regions, so in a deep network gradients converge to 0 in a chain reaction (vanishing). ReLU has a derivative of 1 in the positive region, which mitigates this. Explosion is the opposite: values greater than 1 are multiplied in a chain and diverge, and gradient clipping suppresses it by setting an upper bound on the norm. Batch normalization and residual connections are structural devices that mitigate both problems.
It captures tensors during training (gradients, weights, loss, etc.) and automatically detects training anomalies.
| Built-in rule | What it catches | Response |
|---|---|---|
vanishing_gradient | Gradients converging to 0 | ReLU, batch normalization, residual connections, initialization |
exploding_tensor | Tensor values surging / NaN·Inf | Lower learning rate, gradient clipping |
overfit | Growing gap between training and validation | More regularization, augmentation, early stopping |
overtraining | Validation metric stops improving and worsens | Stop training early |
loss_not_decreasing | Loss stalls | Check learning rate, initialization, data |
class_imbalance | Skewed class distribution | Oversampling, class weights, re-selecting metrics |
saturated_activation | Saturated activations (sigmoid/tanh) | Replace the activation function, normalization |
from sagemaker.debugger import Rule, rule_configs
rules = [
Rule.sagemaker(rule_configs.vanishing_gradient()),
Rule.sagemaker(rule_configs.loss_not_decreasing()),
]
est = Estimator(..., rules=rules)Distinguishing signal: "automatically detect and alert on vanishing/exploding gradients, overfitting, and stalled loss during training" → Debugger.
It analyzes system resources and performance bottlenecks of training (efficiency, not model quality).
| Signal Profiler looks at | Interpretation | Direction of action |
|---|---|---|
| Low GPU utilization, high CPU utilization | Data preprocessing is CPU-bound | Offload preprocessing, adjust the number of workers |
| GPU idle periodically | Data loading / I/O wait bottleneck | Change input mode (Pipe/FastFile), prefetching |
| Large amount of free GPU memory | Batch is small and resources go unused | Increase batch size (adjust the learning rate together) |
| Low utilization of resources throughout | Instance oversized | Save cost with smaller/fewer instances |
| Tool | What it watches | When | When not to use it |
|---|---|---|---|
| Debugger | Tensors during training = model quality anomalies | While the training job runs | Unsuitable for resource bottleneck/cost analysis |
| Profiler | CPU/GPU, I/O = resources/performance | While the training job runs | Unsuitable for detecting vanishing gradients or overfitting |
| Model Monitor | Input/prediction drift of deployed endpoints | Post-deployment operations, not training | Picking it to detect anomalies during training is the wrong answer |
Distinguishing signals:
⚠️ Trap: Some options mix Debugger, Profiler, and Model Monitor. Quality = Debugger / resources = Profiler / post-deployment drift = Model Monitor — this one line settles most of them.
| Technique | Effect | When to use | Caution |
|---|---|---|---|
| Mixed precision (FP16/BF16) | Lower memory, higher speed | Large batches, large models | Numerical stability must be verified |
| Gradient accumulation | Collects several small batches for a large-batch effect | When memory constraints rule out a large batch | Time per step increases |
| Checkpointing | Resume from the point of interruption | Long training, preparing for Spot interruptions (linked to Day 1) | Meaningless without an S3 path configured |
⚠️ Trap: "GPU out of memory" does not automatically mean model parallelism is the answer. If the model itself does not fit, model parallelism; if it is because of the batch, gradient accumulation, mixed precision, or a smaller batch.
Training is not going as expected
├─ Does the job fail outright?
│ └─ OOM error → memory problem
│ ├─ The model itself does not fit on a single GPU → model parallelism (Day 1)
│ └─ Because of the batch → reduce batch / gradient accumulation / mixed precision
├─ Is the loss NaN, diverging, or oscillating widely?
│ └─ Yes → learning rate too high or exploding gradients
│ → lower learning rate + gradient clipping (+ batch normalization)
├─ Is the loss barely decreasing and flat?
│ ├─ Early-layer weights barely change → vanishing gradients
│ │ → ReLU family / batch normalization / residual connections / revisit initialization
│ └─ Decreasing only slowly throughout → learning rate too low → raise learning rate, warmup
├─ Does the loss decrease well, but training is slow and expensive?
│ └─ Yes → resource problem → identify the bottleneck with Profiler
│ ├─ GPU idle / I/O wait → improve the input pipeline
│ └─ Low utilization throughout → shrink instances to save cost
└─ Does the loss decrease but validation gets worse?
└─ Yes → generalization problem (Day 3) → regularization, augmentation, early stopping
(can be detected automatically with Debugger's overfit / overtraining rules)| Passage clue | Answer to choose | Reason |
|---|---|---|
| "Loss diverges to NaN within a few steps" | Lower learning rate + gradient clipping | Classic symptom of a too-high learning rate / explosion |
| "Early-layer weights are not updated and loss stalls" | ReLU, batch normalization, residual connections, initialization | Techniques that mitigate vanishing gradients |
| "Replace all activations with sigmoid" | Wrong option | Saturation makes vanishing worse |
| "Automatically detect vanishing, overfitting, and loss not decreasing, with alerts and early stopping" | SageMaker Debugger | Built-in rules + CloudWatch event triggers |
| "GPU utilization is low and a data loading bottleneck is suspected" | SageMaker Profiler | Diagnoses bottlenecks and cost through system metrics |
| "OOM with a large batch, but we want to keep the batch effect" | Gradient accumulation | Collects gradients from several small batches and updates once |
| "We increased the batch and convergence got slower" | Increase the learning rate too | Linear scaling rule of thumb |
| "The input distribution seems to have changed after deployment" | Model Monitor | Operational monitoring, not a training tool |
Tomorrow we review all of Week 8 (training jobs, AMT, generalization, optimization) together and tie it up from the exam perspective.
Click a choice to reveal the answer and explanation.
Question 1
While training a deep learning model, the loss diverges to NaN within a few steps. What are the most likely cause and response?
Question 2
In a very deep neural network, the weights of the early layers are barely updated and the loss stagnates from the start. Which of the following techniques is the LEAST helpful for mitigating this vanishing gradient problem?
Question 3
You want to automatically detect training quality issues such as vanishing gradients, overfitting, and non-decreasing loss, and trigger alerts or early stopping when a rule is violated. Which SageMaker feature is MOST suitable?
Question 4
A training job shows low GPU utilization and you suspect a data loading bottleneck. You want to analyze system resource utilization and bottlenecks to improve cost and speed. Which tool is MOST suitable?
Question 5
During large model training, a large batch size causes GPU out-of-memory (OOM). Which technique keeps the effect of the batch size while avoiding the memory constraint?