PyTorch Transfer Learning Debugger (v0.1.1)
Diagnose and debug common issues when performing in transfer learning & fine-tuning with PyTorch.
An interactive debugging experience for PyTorch code, enabling users to diagnose issues related to weight updates during training, as well as to address problems with slow or unstable convergence.
Installation (PyPI)
pip install the package:
pip install pytorch-transfer-learning-debugger
or
pip install git+https://github.com/ZachWolpe/PyTorch-transfer-learning-debugger.git
Benefits
- Helps identify if weights are properly updating during training
- Provides visibility into each stage of the training process
- Enables early detection of training issues
- Supports both high-level and granular debugging approaches
This debugging tool is particularly useful when:
- Troubleshooting training convergence issues
- Verifying proper weight updates
- Understanding the flow of data through the model
- Diagnosing optimization problems
Getting Started
Purpose
An interactive debugging tool for PyTorch code that helps diagnose issues related to weight updates during training and addresses problems with slow or unstable convergence.
Key Components
1. Debug Mode Features
The debugger is implemented through a torch_debugger class that provides two main debugging modes:
-
Basic Debug Mode (
debug_mode=True):- Tracks model weight updates
- Only monitors weights with
requires_grad=True - Populates
torch_debugger._track_weightsdictionary for analysis
-
Granular Debug Mode (
granular_logging=True):- Exits after one cycle through the dataloader
- Logs each stage of the computational graph:
- Optimizer verification
- Gradient zeroing
- Data movement to device
- Forward pass and loss computation
- Gradient computation and optimizer steps
2. Implementation Example
Here's how the debugger is integrated into the training loop:
def train_model(model, criterion, optimizer, scheduler, num_epochs=25,
debug_mode=True, granular_logging=False):
# Initialize debugger if debug_mode is enabled
torch_debugger_inst = None
if debug_mode:
torch_debugger_inst = torch_debugger(enable_log_stage=granular_logging)
torch_debugger_inst.verify_optimizer_state(optimizer)
torch_debugger_inst.initial_model_weights_state = model.state_dict().copy()
# Training loop with debugging hooks
for epoch in range(num_epochs):
for phase in ['train', 'val']:
for inputs, labels in dataloaders[phase]:
if debug_mode:
torch_debugger_inst.log_stage('Moving data to device')
# ... more logging stages ...
if phase == 'train':
torch_debugger_inst.track_weights(model.named_parameters())
3. Usage
To use the debugger:
torch_debugger_inst, model = train_model(
model_ft,
criterion,
optimizer_ft,
exp_lr_scheduler,
num_epochs=25,
debug_mode=True,
granular_logging=True # For detailed stage logging
)
4. Analysis Tools
The debugger provides visualization tools to analyze training:
# Plot weight updates over time
torch_debugger_inst.plot_weight_updates()
# Access tracked weights
weights = torch_debugger_inst._track_weights['conv1.weight']
Comprehensive Tutorial
For a comprehensive tutorial, see the GettingStarted.ipynb Juypyter notebook.
Potential Issues during Transfer Learning
| Category | Issue | Symptoms | Solutions |
|---|---|---|---|
| Learning Rate | Improper Configuration | • Too high: unstable training • Too low: slow learning/stuck • Poor adjustment for transfer |
• Start with 10-3 or 10-4 of original LR • Use LR finder • Implement LR scheduling • Use different LRs for layers |
| Layer Management | Freezing Issues | • Wrong layers frozen • Too many/few layers frozen • No gradual unfreezing • Model not learning |
• Start by freezing all but final layers • Gradually unfreeze from top • Monitor layer gradients • Selective layer unfreezing |
| Data Preparation | Input Problems | • Runtime errors • Poor model performance • Slow convergence • Class imbalance |
• Adjust transforms • Match source preprocessing • Balance classes • Use validation set • Data augmentation |
| Architecture | Structure Mismatches | • Bad final layer modifications • Size/channel mismatches • Poor layer initialization • Training fails |
• Verify input/output dimensions • Use proper initialization • Match pretrained architecture • Add adaptation layers |
| Training Process | Optimization Issues | • Not converging • Unstable training • Poor generalization • Memory errors |
• Use Adam/AdamW for fine-tuning • Verify loss matches task • Start with small batches • Use gradient accumulation |
| Implementation | Technical Errors | • Model not in train mode • Gradients not zeroed • Wrong device (CPU/GPU) • Memory leaks |
• Use training checklist • Implement proper train/eval • Check device placement • Monitor memory usage |
| Resource Management | Performance Issues | • OOM errors • Slow training • Resource inefficiency |
• Batch size adjustment • Gradient checkpointing • Efficient data loading • Use GPU acceleration |
| Monitoring & Validation | Quality Control | • Poor metric tracking • Missing validation • Overfitting • Inconsistent results |
• Use debugging tools • Implement validation loops • Track multiple metrics • Monitor gradient flow |
| Pretrained Model | Weight Issues | • Wrong pretrained weights • Corrupted weights • Version incompatibility |
• Verify model source • Check model checksums • Match framework versions |
| Feature Extraction | Transfer Problems | • Suboptimal transfer • Poor adaptation • Loss of pretrained features |
• Choose appropriate layers • Add adaptation layers • Fine-tune feature extractors • Knowledge distillation |
Potential Issues with the Computational Graph
| Issue Category | Specific Problem | Symptoms | Solutions |
|---|---|---|---|
| Gradient Flow | Vanishing Gradients | - Near-zero gradients in early layers - Model not learning |
- Use gradient clipping - Add residual connections - Change activation functions |
| Exploding Gradients | - NaN losses - Large gradient values |
- Implement gradient clipping - Reduce learning rate - Check initialization |
|
| Disconnected Graphs | - Some parameters not updating - Partial learning |
- Remove accidental detach() - Verify tensor operations maintain graph |
|
| Tensor Operations | In-place Operations | - Backward pass errors - "Leaf variable modified" error |
- Replace in-place ops (+=) with regular ops (+) - Create new tensors instead of modifying |
| Device Mismatches | - Runtime errors - CUDA errors |
- Use .to(device) consistently - Implement device checker |
|
| Detached Tensors | - No gradients flowing - Parts of model not learning |
- Set requires_grad=True where needed - Remove unnecessary detach() calls |
|
| Autograd Engine | Broken Computational Paths | - Gradients not computed - backward() errors |
- Fix graph connections - Ensure proper tensor operations |
| Mixed Precision Errors | - NaN losses - Unstable training |
- Use GradScaler - Adjust loss scaling - Check dtype consistency |
|
| Loss Computation | Zero/NaN Losses | - Model not learning - Training instability |
- Verify loss function implementation - Check input normalization |
| Wrong Reduction | - Incorrect gradient scaling - Slow convergence |
- Set appropriate reduction method - Verify batch dimension handling |
: zachcolinwolpe@gmail.com
: 09.12.2024
Metadata
Release files for pytorch-transfer-learning-debugger 0.1.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| pytorch_transfer_learning_debugger-0.1.1.tar.gz | 15.0 kB | With provenance | Details for pytorch_transfer_learning_debugger-0.1.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| pytorch_transfer_learning_debugger-0.1.1-py3-none-any.whl | Python 3 | none | any | Details for pytorch_transfer_learning_debugger-0.1.1-py3-none-any.whl |
Total release size: 28.5 kB
Release files / pytorch_transfer_learning_debugger-0.1.1.tar.gz
| Download URL | pytorch_transfer_learning_debugger-0.1.1.tar.gz |
|---|---|
| Size | 15.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
95d2adf206b0cc17d37db268b81fbcb3f822e4a4ee572324a816c87856163c08
|
|
BLAKE2b-256 checksum How to use checksums |
63fb7b1f9627ec2e3285632f1f2ec270042ad4a6349fab456cda7950fdc7923e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.0.1 CPython/3.12.8
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Dec 9, 2024.
Transparency logRelease files / pytorch_transfer_learning_debugger-0.1.1-py3-none-any.whl
| Download URL | pytorch_transfer_learning_debugger-0.1.1-py3-none-any.whl |
|---|---|
| Size | 13.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
51fafcd5ee76b5007f48fad54ae814a6092a067f23160487b85de0ba904735b4
|
|
BLAKE2b-256 checksum How to use checksums |
87338e06e52643c404b66e4a9fc89d8f5b82e65536f59cf3a26303f8d52c8d54
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.0.1 CPython/3.12.8
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Dec 9, 2024.
Transparency logSecurity for pytorch-transfer-learning-debugger 0.1.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for pytorch_transfer_learning_debugger-0.1.1.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Dec 9, 2024.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.