Jupyter Notebooks
Jupyter Notebook Setup
Section titled “Jupyter Notebook Setup”Jupyter Notebooks are essential for interactive deep learning development and experimentation.
Installation
Section titled “Installation”# Activate your environment
source ml-env/bin/activate
# Install Jupyter
pip install jupyter notebook jupyterlab
# Launch Jupyter
jupyter notebook
# or
jupyter lab# Activate your environment
conda activate ml-env
# Install Jupyter
conda install jupyter notebook jupyterlab
# Launch Jupyter
jupyter notebookAdding Environment as Jupyter Kernel
Section titled “Adding Environment as Jupyter Kernel”To use your Python environment in Jupyter notebooks:
# Activate your environment
source ml-env/bin/activate
# Install ipykernel
pip install ipykernel
# Add environment as kernel
python -m ipykernel install --user --name ml-env --display-name "Python 3.11 (ML)"
# Verify it's added
jupyter kernelspec list# Activate your environment
conda activate ml-env
# Install ipykernel
conda install ipykernel
# Add environment as kernel with descriptive name
python -m ipykernel install --user --name ml-env --display-name "Python 3.11 (ML - Conda)"
# Verify it's added
jupyter kernelspec listRecommended naming convention:
# Include Python version and environment purpose
python -m ipykernel install --user --name tfmain --display-name "Python 3.10 (TensorFlow)"
python -m ipykernel install --user --name pytorch-env --display-name "Python 3.11 (PyTorch)"Managing Kernels
Section titled “Managing Kernels”List All Kernels
Section titled “List All Kernels”jupyter kernelspec listOutput example:
Available kernels:
ml-env /home/user/.local/share/jupyter/kernels/ml-env
python3 /usr/share/jupyter/kernels/python3
pytorch-env /home/user/.local/share/jupyter/kernels/pytorch-envRemove Unused Kernel
Section titled “Remove Unused Kernel”# Remove a kernel
jupyter kernelspec uninstall unwanted-kernel
# Example
jupyter kernelspec uninstall old-tf-envUpdate Kernel Display Name
Section titled “Update Kernel Display Name”# Remove old kernel
jupyter kernelspec uninstall old-name
# Re-add with new display name
python -m ipykernel install --user --name myenv --display-name "New Display Name"Jupyter Notebook Tips
Section titled “Jupyter Notebook Tips”Timing Cell Execution
Section titled “Timing Cell Execution”%%time
# Your code here
model.train()Shows total execution time for the cell.
%%timeit
# Your code here - runs multiple times for accurate measurement
result = model(data)Runs code multiple times and provides statistics.
# Install extension for persistent timing
pip install jupyter_contrib_nbextensions
jupyter contrib nbextension install --user
jupyter nbextension enable execute_time/ExecuteTime
# Restart Jupyter to see execution times in cells automaticallyShows execution time for every cell automatically.
GPU Monitoring in Notebooks
Section titled “GPU Monitoring in Notebooks”# Check GPU availability
import torch
print(f"CUDA available: {torch.cuda.is_available()}")
print(f"GPU: {torch.cuda.get_device_name(0)}")
print(f"GPU count: {torch.cuda.device_count()}")
# Monitor GPU memory
print(f"Memory allocated: {torch.cuda.memory_allocated(0) / 1e9:.2f} GB")
print(f"Memory reserved: {torch.cuda.memory_reserved(0) / 1e9:.2f} GB")# Check GPU availability
import tensorflow as tf
gpus = tf.config.list_physical_devices('GPU')
print(f"GPUs available: {len(gpus)}")
for gpu in gpus:
print(f" - {gpu}")
# Get GPU memory info
gpu_devices = tf.config.experimental.list_physical_devices('GPU')
for device in gpu_devices:
tf.config.experimental.set_memory_growth(device, True)# Monitor all GPUs in real-time
!nvidia-smi
# Watch GPU usage continuously
!watch -n 1 nvidia-smi # Updates every secondAuto-Reload Modules
Section titled “Auto-Reload Modules”# Add to first cell - automatically reload changed modules
%load_ext autoreload
%autoreload 2Better Output Display
Section titled “Better Output Display”# Display all outputs (not just last one)
from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = "all"Common Issues
Section titled “Common Issues”Issue: Empty Browser Page on Jupyter Start
Section titled “Issue: Empty Browser Page on Jupyter Start”Problem: Jupyter launches but shows blank page
Solution:
# Hard refresh browser
Ctrl + Shift + R # Windows/Linux
Cmd + Shift + R # Mac
# Or clear browser cache and restart Jupyter
jupyter notebook --no-browser
# Then manually open the URL shownIssue: Kernel Not Found / “Dead Kernel”
Section titled “Issue: Kernel Not Found / “Dead Kernel””Problem: Notebook can’t connect to kernel
Solution:
# 1. Check if kernel exists
jupyter kernelspec list
# 2. Reinstall ipykernel in environment
source ml-env/bin/activate
pip install --force-reinstall ipykernel
python -m ipykernel install --user --name ml-env --display-name "Python (ML)"
# 3. Restart Jupyter# 1. Check if kernel exists
jupyter kernelspec list
# 2. Reinstall ipykernel in environment
conda activate ml-env
conda install --force-reinstall ipykernel
python -m ipykernel install --user --name ml-env --display-name "Python (ML)"
# 3. Restart JupyterIssue: Wrong Python Environment
Section titled “Issue: Wrong Python Environment”Problem: Notebook uses system Python instead of your environment
Solution:
# Verify kernel Python path
jupyter kernelspec list
cat ~/.local/share/jupyter/kernels/ml-env/kernel.json
# Should point to your environment, e.g.:
# "/home/user/ml-env/bin/python"
# If wrong, remove and re-add kernel
jupyter kernelspec uninstall ml-env
source ml-env/bin/activate
python -m ipykernel install --user --name ml-envIssue: ImportError for Packages Installed in Environment
Section titled “Issue: ImportError for Packages Installed in Environment”Problem: Package installed but import fails in notebook
Check you’re using the right kernel:
# In notebook cell
import sys
print(sys.executable)
# Should show: /home/user/ml-env/bin/python
# If wrong, change kernel:
# Kernel → Change Kernel → Select correct environmentIssue: Jupyter Lab Extensions Not Loading
Section titled “Issue: Jupyter Lab Extensions Not Loading”# Rebuild JupyterLab
jupyter lab build
# Or clear cache and rebuild
jupyter lab clean
jupyter lab buildIssue: “Charset Normalizer” Error
Section titled “Issue: “Charset Normalizer” Error”Error:
AttributeError: partially initialized module 'charset_normalizer'
has no attribute 'md__mypyc' (most likely due to a circular import)Fix:
pip install -U --force-reinstall charset-normalizerJupyterLab vs Jupyter Notebook
Section titled “JupyterLab vs Jupyter Notebook”Jupyter Notebook (Classic)
Section titled “Jupyter Notebook (Classic)”- Simpler interface
- Lighter weight
- Better for single-notebook work
jupyter notebookJupyterLab (Modern)
Section titled “JupyterLab (Modern)”- Multi-tab interface
- Built-in terminal
- File browser
- Extension system
jupyter labRecommendation: Use JupyterLab for most work, Notebook for simplicity.
Remote Jupyter Access
Section titled “Remote Jupyter Access”SSH Tunneling
Section titled “SSH Tunneling”On remote machine:
jupyter notebook --no-browser --port=8888On local machine:
# Create SSH tunnel
ssh -L 8888:localhost:8888 user@remote-server
# Open browser to http://localhost:8888JupyterHub (Multi-user)
Section titled “JupyterHub (Multi-user)”For team environments, consider JupyterHub:
- Multi-user support
- Resource management
- Authentication
See JupyterHub docs for setup.
Productivity Extensions
Section titled “Productivity Extensions”Recommended Extensions
Section titled “Recommended Extensions”# Install extension manager
pip install jupyter_contrib_nbextensions
jupyter contrib nbextension install --user
# Enable useful extensions
jupyter nbextension enable execute_time/ExecuteTime
jupyter nbextension enable toc2/main # Table of contents
jupyter nbextension enable varInspector/main # Variable inspector
jupyter nbextension enable codefolding/main # Code foldingJupyterLab Extensions
Section titled “JupyterLab Extensions”# GPU monitoring dashboard
pip install jupyterlab-nvdashboard
jupyter labextension install jupyterlab-nvdashboard
# Git integration
pip install jupyterlab-gitBest Practices
Section titled “Best Practices”Notebook Organization
Section titled “Notebook Organization”# Cell 1: Imports and setup
import torch
import numpy as np
%load_ext autoreload
%autoreload 2
# Cell 2: Configuration
BATCH_SIZE = 32
LEARNING_RATE = 1e-3
DEVICE = 'cuda' if torch.cuda.is_available() else 'cpu'
# Cell 3+: Your work
...Save Notebooks as Scripts
Section titled “Save Notebooks as Scripts”# Convert notebook to Python script
jupyter nbconvert --to script notebook.ipynb
# Generates notebook.pyVersion Control
Section titled “Version Control”# Add to .gitignore
*.ipynb_checkpoints/
.ipynb_checkpoints
# Clean output before committing
jupyter nbconvert --ClearOutputPreprocessor.enabled=True --inplace notebook.ipynbNext Steps
Section titled “Next Steps”- Set up Python environments for isolated dependencies
- GPU troubleshooting if GPU not detected
- Monitor training from notebooks