Thomas Piacentini
← Workshop

Cheat sheet

Notebooks, Data Files and Profiling

Original text · English

Run notebooks safely, load and split datasets, keep big files out of git, profile time and memory, catch silent AI bugs.

Notebook shortcuts

CommandWhat it does
pip install jupyterlab then jupyter labStart JupyterLab
Escape / EnterCommand mode / edit mode
Shift+EnterRun cell, move to next
A / BInsert cell above / below
DDDelete cell
M / YConvert to markdown / code
Shift+TabShow function signature
Ctrl+Shift+HShow all shortcuts
Sources

Magic commands

CommandWhat it does
%timeit np.random.randn(10000)Runs many times and averages. Microbenchmarks
%%timeRuns the cell once. Training runs
%matplotlib inlinePlots render in the notebook
!pip install scikit-learn! runs any shell command
%env CUDA_VISIBLE_DEVICESCheck an environment variable
%whoList all variables in memory
!which pythonCheck the kernel environment
Sources

Notebook troubleshooting

SymptomCauseFix
Works for me, breaks for othersCells run out of orderKernel > Restart & Run All before sharing
Clean notebook needs a deleted cellHidden state in the kernelRestart the kernel regularly
RAM keeps growingVariables persist across cellsdel variable_name and gc.collect()
Kernel dies silentlyUI hides the errorRead the terminal that runs Jupyter
print(df) shows textNot the last expressionEnd the cell with df
Import error after pip installPackage in another Python!pip install package in the notebook
Cell runs foreverWaits for input() or a loopKernel > Interrupt, or I twice
Colab files goneFree session ended after 90 minutes of inactivitydrive.mount('/content/drive')
Sources

Data pipeline

downloadfirst loadHugging Face Hubdatasets and modelsstoredatasets libraryload or streamprocessLocal Cache~/.cache/huggingface/storeFormat ConversionCSV, JSON, Parquet, ArrowprocessData Splitstrain / val / testprocessYour Training Pipelinemodel
Nodes and connections
NodeRoleKind
Hugging Face Hubdatasets and modelsstore
datasets libraryload or streamprocess
Local Cache~/.cache/huggingface/store
Format ConversionCSV, JSON, Parquet, Arrowprocess
Data Splitstrain / val / testprocess
Your Training Pipelinemodel
FromToConnection
Hugging Face Hubdatasets librarydownload
datasets libraryLocal Cachefirst load
datasets libraryFormat Conversion
Format ConversionData Splits
Data SplitsYour Training Pipeline
Sources

Load, stream, convert, split

dataset = load_dataset("stanfordnlp/imdb", split="train")cached after first load
stream = load_dataset("wikimedia/wikipedia", "20231101.en", split="train", streaming=True)
dataset.to_parquet("imdb_train.parquet")also to_csv, to_json
split = dataset.train_test_split(test_size=0.2, seed=42)
train_val = split["train"].train_test_split(test_size=0.125, seed=42)
train_ds, val_ds, test_ds = train_val["train"], train_val["test"], split["test"]
make_splits(ds, train_ratio=0.8, val_ratio=0.1, seed=42)data_utils helper
model_dir = snapshot_download("sentence-transformers/all-MiniLM-L6-v2")
Sources

File formats

SettingDefaultNote
CSVSlowLarge. Human readability, spreadsheets
JSONSlowLarge. APIs, nested data
ParquetFastSmall. Best storage format for AI work
ArrowFastestSmall. In memory, inside datasets
Train split80%Validation 10%, test 10%
Dataset cache~/.cache/huggingface/datasets/Model cache is ~/.cache/huggingface/hub/
Sources

Keep big files out of git

TaskCode
Ignore weights and data*.safetensors, *.pt, data/*.parquet, models/
Track with Git LFSgit lfs track "*.safetensors"
Commit the LFS rulesgit add .gitattributes
Start DVCdvc init
Track data with DVCdvc add data/training_set.parquet
Commit the pointergit add data/training_set.parquet.dvc data/.gitignore
Push data to S3dvc remote add -d myremote s3://my-bucket/dvc-store then dvc push
Sources

Debug and profile tools

TaskCode
Stop only on a bad lossif loss.item() > 100 or torch.isnan(loss): breakpoint()
Inspect in pdbp outputs.shape, p loss.item(), then c or q
Profile functionspython -m cProfile -s cumtime train.py
Profile lines@profile, then kernprof -l -v train.py
Memory by linepython -m memory_profiler your_script.py
Top allocationstracemalloc.take_snapshot().statistics("lineno")
GPU memorytorch.cuda.memory_allocated(), torch.cuda.memory_reserved()
Training curvesSummaryWriter("runs/experiment_1"), then tensorboard --logdir=runs
Sources

Debug workflow

  1. Before training run check_shapes with a sample batch.
  2. First 10 steps debug_print loss, outputs and gradients. No NaN.
  3. During training log loss, learning rate and gradient norms to TensorBoard.
  4. When it breaks drop breakpoint() at the failure point.
  5. For speed time data loading, forward and backward. Profile memory near OOM.
Sources

Silent AI bugs

SymptomCauseFix
NaN after a few stepsLearning rate too highReduce by 10x. Add gradient clipping
NaN immediatelyLog of zero or negativetorch.log(x + 1e-8)
Loss stuck at ln(num_classes)No gradient flowRemove the accidental .detach()
99% test accuracyData leakagecheck_data_leakage(train_set, test_set)
OOMBatch too largeHalve the batch. torch.cuda.empty_cache()
OOM during backwardGradients not clearedoptimizer.zero_grad() each step
Slow, low GPU useData loading bottlenecknum_workers=4 and pin_memory=True
Tensor on the wrong deviceCPU tensor, GPU modelcheck_devices(model, *tensors)
Sources

Read the curves

Loss not decreasing?
Learning rate too low, or an architecture issue.
Loss oscillating wildly?
Learning rate too high.
Train loss down, val loss up?
Overfitting.
Weight histograms collapse to zero?
Vanishing gradients.
Gradient histograms explode?
Add gradient clipping.
Sources

Tips

  • Explore in notebooks, ship in scripts.
  • Use %timeit for microbenchmarks and %%time for training runs.
  • Always set a seed. The same seed gives the same split.
  • Use streaming when a dataset does not fit on disk.
  • Data loading often takes 60% of training time. Set num_workers > 0.
Sources
Original source
---
id: notebooks-data-profiling
title: Notebooks, Data Files and Profiling
code: ND
subtitle: Run notebooks safely, load and split datasets, keep big files out of git, profile time and memory, catch silent AI bugs.
hue: 285
sources: phases/00-setup-and-tooling/05-jupyter-notebooks, phases/00-setup-and-tooling/09-data-management, phases/00-setup-and-tooling/12-debugging-and-profiling
source_hash: 51d70b9241509845
---

## Notebook shortcuts
type: command-table
from: phases/00-setup-and-tooling/05-jupyter-notebooks/docs/en.md

| Command | What it does |
|---|---|
| `pip install jupyterlab` then `jupyter lab` | Start JupyterLab |
| `Escape` / `Enter` | Command mode / edit mode |
| `Shift+Enter` | Run cell, move to next |
| `A` / `B` | Insert cell above / below |
| `DD` | Delete cell |
| `M` / `Y` | Convert to markdown / code |
| `Shift+Tab` | Show function signature |
| `Ctrl+Shift+H` | Show all shortcuts |

## Magic commands
type: command-table
from: phases/00-setup-and-tooling/05-jupyter-notebooks/docs/en.md, phases/00-setup-and-tooling/05-jupyter-notebooks/outputs/prompt-notebook-helper.md

| Command | What it does |
|---|---|
| `%timeit np.random.randn(10000)` | Runs many times and averages. Microbenchmarks |
| `%%time` | Runs the cell once. Training runs |
| `%matplotlib inline` | Plots render in the notebook |
| `!pip install scikit-learn` | `!` runs any shell command |
| `%env CUDA_VISIBLE_DEVICES` | Check an environment variable |
| `%who` | List all variables in memory |
| `!which python` | Check the kernel environment |

## Notebook troubleshooting
type: troubleshooting
from: phases/00-setup-and-tooling/05-jupyter-notebooks/docs/en.md, phases/00-setup-and-tooling/05-jupyter-notebooks/outputs/prompt-notebook-helper.md

| Symptom | Cause | Fix |
|---|---|---|
| Works for me, breaks for others | Cells run out of order | Kernel > Restart & Run All before sharing |
| Clean notebook needs a deleted cell | Hidden state in the kernel | Restart the kernel regularly |
| RAM keeps growing | Variables persist across cells | `del variable_name` and `gc.collect()` |
| Kernel dies silently | UI hides the error | Read the terminal that runs Jupyter |
| `print(df)` shows text | Not the last expression | End the cell with `df` |
| Import error after pip install | Package in another Python | `!pip install package` in the notebook |
| Cell runs forever | Waits for `input()` or a loop | Kernel > Interrupt, or `I` twice |
| Colab files gone | Free session ended after 90 minutes of inactivity | `drive.mount('/content/drive')` |

## Data pipeline
type: architecture
from: phases/00-setup-and-tooling/09-data-management/docs/en.md

```arch
node | hub | 1,1 | Hugging Face Hub | datasets and models | store
node | lib | 2,1 | datasets library | load or stream | process
node | cache | 3,1 | Local Cache | ~/.cache/huggingface/ | store
node | fmt | 2,2 | Format Conversion | CSV, JSON, Parquet, Arrow | process
node | split | 3,2 | Data Splits | train / val / test | process
node | train | 4,2 | Your Training Pipeline | | model
edge | hub | lib | download
edge | lib | cache | first load
edge | lib | fmt |
edge | fmt | split |
edge | split | train |
```

## Load, stream, convert, split
type: annotated-code
lang: python
from: phases/00-setup-and-tooling/09-data-management/docs/en.md, phases/00-setup-and-tooling/09-data-management/code/data_utils.py

```python
dataset = load_dataset("stanfordnlp/imdb", split="train")   # cached after first load
stream = load_dataset("wikimedia/wikipedia", "20231101.en", split="train", streaming=True)
dataset.to_parquet("imdb_train.parquet")                    # also to_csv, to_json
split = dataset.train_test_split(test_size=0.2, seed=42)
train_val = split["train"].train_test_split(test_size=0.125, seed=42)
train_ds, val_ds, test_ds = train_val["train"], train_val["test"], split["test"]
make_splits(ds, train_ratio=0.8, val_ratio=0.1, seed=42)    # data_utils helper
model_dir = snapshot_download("sentence-transformers/all-MiniLM-L6-v2")
```

## File formats
type: defaults
from: phases/00-setup-and-tooling/09-data-management/docs/en.md

| Setting | Default | Note |
|---|---|---|
| CSV | Slow | Large. Human readability, spreadsheets |
| JSON | Slow | Large. APIs, nested data |
| Parquet | Fast | Small. Best storage format for AI work |
| Arrow | Fastest | Small. In memory, inside `datasets` |
| Train split | 80% | Validation 10%, test 10% |
| Dataset cache | `~/.cache/huggingface/datasets/` | Model cache is `~/.cache/huggingface/hub/` |

## Keep big files out of git
type: task-table
from: phases/00-setup-and-tooling/09-data-management/docs/en.md
span: 2

| Task | Code |
|---|---|
| Ignore weights and data | `*.safetensors`, `*.pt`, `data/*.parquet`, `models/` |
| Track with Git LFS | `git lfs track "*.safetensors"` |
| Commit the LFS rules | `git add .gitattributes` |
| Start DVC | `dvc init` |
| Track data with DVC | `dvc add data/training_set.parquet` |
| Commit the pointer | `git add data/training_set.parquet.dvc data/.gitignore` |
| Push data to S3 | `dvc remote add -d myremote s3://my-bucket/dvc-store` then `dvc push` |

## Debug and profile tools
type: task-table
from: phases/00-setup-and-tooling/12-debugging-and-profiling/docs/en.md
span: 2

| Task | Code |
|---|---|
| Stop only on a bad loss | `if loss.item() > 100 or torch.isnan(loss): breakpoint()` |
| Inspect in pdb | `p outputs.shape`, `p loss.item()`, then `c` or `q` |
| Profile functions | `python -m cProfile -s cumtime train.py` |
| Profile lines | `@profile`, then `kernprof -l -v train.py` |
| Memory by line | `python -m memory_profiler your_script.py` |
| Top allocations | `tracemalloc.take_snapshot().statistics("lineno")` |
| GPU memory | `torch.cuda.memory_allocated()`, `torch.cuda.memory_reserved()` |
| Training curves | `SummaryWriter("runs/experiment_1")`, then `tensorboard --logdir=runs` |

## Debug workflow
type: flow
from: phases/00-setup-and-tooling/12-debugging-and-profiling/docs/en.md

1. **Before training**: run `check_shapes` with a sample batch.
2. **First 10 steps**: `debug_print` loss, outputs and gradients. No NaN.
3. **During training**: log loss, learning rate and gradient norms to TensorBoard.
4. **When it breaks**: drop `breakpoint()` at the failure point.
5. **For speed**: time data loading, forward and backward. Profile memory near OOM.

## Silent AI bugs
type: troubleshooting
from: phases/00-setup-and-tooling/12-debugging-and-profiling/docs/en.md, phases/00-setup-and-tooling/12-debugging-and-profiling/outputs/prompt-debug-ai-code.md

| Symptom | Cause | Fix |
|---|---|---|
| NaN after a few steps | Learning rate too high | Reduce by 10x. Add gradient clipping |
| NaN immediately | Log of zero or negative | `torch.log(x + 1e-8)` |
| Loss stuck at ln(num_classes) | No gradient flow | Remove the accidental `.detach()` |
| 99% test accuracy | Data leakage | `check_data_leakage(train_set, test_set)` |
| OOM | Batch too large | Halve the batch. `torch.cuda.empty_cache()` |
| OOM during backward | Gradients not cleared | `optimizer.zero_grad()` each step |
| Slow, low GPU use | Data loading bottleneck | `num_workers=4` and `pin_memory=True` |
| Tensor on the wrong device | CPU tensor, GPU model | `check_devices(model, *tensors)` |

## Read the curves
type: qa
from: phases/00-setup-and-tooling/12-debugging-and-profiling/docs/en.md

- **Loss not decreasing?** Learning rate too low, or an architecture issue.
- **Loss oscillating wildly?** Learning rate too high.
- **Train loss down, val loss up?** Overfitting.
- **Weight histograms collapse to zero?** Vanishing gradients.
- **Gradient histograms explode?** Add gradient clipping.

## Tips
type: tips
from: phases/00-setup-and-tooling/05-jupyter-notebooks/docs/en.md, phases/00-setup-and-tooling/09-data-management/docs/en.md, phases/00-setup-and-tooling/12-debugging-and-profiling/docs/en.md

- Explore in notebooks, ship in scripts.
- Use `%timeit` for microbenchmarks and `%%time` for training runs.
- Always set a seed. The same seed gives the same split.
- Use streaming when a dataset does not fit on disk.
- Data loading often takes 60% of training time. Set `num_workers > 0`.
Code and artifacts 1
What you take away

Saved in your notebook on this device.

Sources and limits

Reading checkpoints are self-reported. Test results are recorded separately and apply to the executed cases.

AI Engineering from Scratch · 3f9715c · MIT

Download this file ↓