Home Knowledge Base Jupyter Notebooks and ML Workflows

Jupyter Notebooks and ML Workflows

Notebook Environments

Options

PlatformBest ForGPUCost
Google ColabQuick experimentsT4/A100Free tier available
Kaggle NotebooksCompetitions, datasetsT4x2/P100Free (30h/week)
JupyterLabLocal developmentYour GPUFree
SageMaker StudioAWS integrationVariousPay-per-use
Vertex AI WorkbenchGCP integrationVariousPay-per-use
DatabricksEnterprise, SparkVariousEnterprise pricing

Notebook Best Practices

Code Organization

**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

PitfallSolution
Hidden stateRestart kernel, run all cells
Out-of-order executionUse cell magic: %%time at top
No version controlUse nbstripout, jupytext
Memory leaksClear GPU cache, restart kernel
Long outputsUse logging, tqdm for progress

Converting Notebooks to Production

Tools

ToolPurpose
nbconvertConvert to Python script
jupytextKeep .py and .ipynb in sync
papermillParameterize and run notebooks
nbdevBuild 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

**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

**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()
notebookjupytercolabworkflow

Explore 500+ Semiconductor & AI Topics

From EUV lithography to CUDA optimization — search the full knowledge base or chat with our AI assistant.