notebook

**Jupyter Notebooks and ML Workflows** **Notebook Environments** **Options** | Platform | Best For | GPU | Cost | |----------|----------|-----|------| | Google Colab | Quick experiments | T4/A100 | Free tier available | | Kaggle Notebooks | Competitions, datasets | T4x2/P100 | Free (30h/week) | | JupyterLab | Local development | Your GPU | Free | | SageMaker Studio | AWS integration | Various | Pay-per-use | | Vertex AI Workbench | GCP integration | Various | Pay-per-use | | Databricks | Enterprise, Spark | Various | Enterprise pricing | **Notebook Best Practices** **Code Organization** ```python **Cell 1: Imports and configuration** import torch import transformers CONFIG = { "model_name": "meta-llama/Llama-2-7b-hf", "max_length": 512, } **Cell 2: Data loading** def load_data(): ... **Cell 3: Model setup** def setup_model(): ... **Cell 4: Training loop** **Cell 5: Evaluation** **Cell 6: Save results** ``` **Common Pitfalls to Avoid** | Pitfall | Solution | |---------|----------| | Hidden state | Restart kernel, run all cells | | Out-of-order execution | Use cell magic: %%time at top | | No version control | Use nbstripout, jupytext | | Memory leaks | Clear GPU cache, restart kernel | | Long outputs | Use logging, tqdm for progress | **Converting Notebooks to Production** **Tools** | Tool | Purpose | |------|---------| | nbconvert | Convert to Python script | | jupytext | Keep .py and .ipynb in sync | | papermill | Parameterize and run notebooks | | nbdev | Build libraries from notebooks | **Refactoring Pattern** 1. Extract functions to .py modules 2. Keep notebook for exploration/visualization 3. Create CLI or API for production use 4. Add tests for extracted functions **Magic Commands** ```python **Time a cell** %%time model.generate(...) **Run shell commands** !nvidia-smi !pip install transformers **Autoreload imports** %load_ext autoreload %autoreload 2 **Environment variables** %env CUDA_VISIBLE_DEVICES=0 ``` **GPU Memory Management** ```python **Check GPU memory** !nvidia-smi **Clear PyTorch cache** torch.cuda.empty_cache() **Delete objects and trigger GC** del model import gc gc.collect() torch.cuda.empty_cache() ```

Go deeper with CFSGPT

Get AI-powered deep-dives, save terms, and run advanced simulations — free account.

Create Free Account