AgentSkillsCN

colab-notebook-development

创建新 Colab 笔记本的模板模式。适用于以下场景:(1) 为实验创建新笔记本;(2) 为笔记本添加功能模块;(3) 用于代理或验证相关的笔记本;(4) 任何基于 GPU 训练基础设施的笔记本。

SKILL.md
--- frontmatter
name: colab-notebook-development
description: "Pattern for creating new Colab notebooks. Trigger when: (1) Creating a new notebook for experiments, (2) Adding notebook-based functionality, (3) Agent or validation notebooks, (4) Any notebook that uses GPU training infrastructure."
author: Claude Code
date: 2026-02-01

Colab Notebook Development Pattern (v3.7.0)

CRITICAL RULE

ALWAYS copy the structure from notebooks/training.ipynb when creating new notebooks.

DO NOT:

  • Improvise cell structure
  • Combine setup cells
  • Skip drive mount
  • Use different extraction methods
  • Assume paths work differently

Canonical Reference

The authoritative template is notebooks/training.ipynb. Every new notebook MUST follow its pattern for:

  1. Part 1: Setup (Cells 1-6)

    • GPU verification
    • Drive mount (separate cell)
    • Repository extraction (%%bash cell)
    • Path setup
    • Dependencies
    • API keys
  2. Part 2+: Your Content

    • Configuration cells
    • Data loading (use prefetch_all_data)
    • Training/experiment loops
    • Results saving

Cell-by-Cell Template

Setup Cells (MANDATORY - Copy Exactly)

python
# Cell 1: GPU Verification
!nvidia-smi --query-gpu=name,memory.total,driver_version --format=csv
import torch
assert torch.cuda.is_available(), "CUDA not available!"
# ... (see training.ipynb)
python
# Cell 2: Mount Drive
from google.colab import drive
drive.mount('/content/drive')
bash
# Cell 3: Extract (%%bash magic)
%%bash
cd /content
if [ ! -d "Alpaca_trading" ]; then
    unzip -q /content/drive/MyDrive/Colab_Projects/Alpaca_trading.zip
fi
python
# Cell 4: Path Setup
import os, sys
os.chdir('/content/Alpaca_trading')
sys.path.insert(0, '/content/Alpaca_trading')
python
# Cell 5: Dependencies
%pip install -q torch gymnasium alpaca-py pandas numpy scipy arch
python
# Cell 6: API Keys
API_KEYS_FILE = '/content/Alpaca_trading/config/API_key_500Paper.txt'
# ... load keys

Data Loading Pattern

python
# Use the same pattern as training.ipynb
from alpaca_trading.data.caching_fetcher import prefetch_all_data

CACHE_DIR = '/content/drive/MyDrive/Colab_Projects/training_data'
prefetched_data = prefetch_all_data(
    SYMBOLS,
    force_refresh=False,
    cache_dir=CACHE_DIR,
    min_bars=2000,
    keys_file=API_KEYS_FILE,
)

Output Directory Pattern

python
# ALWAYS create directories before saving
from pathlib import Path

OUTPUT_DIR = '/content/drive/MyDrive/Colab_Projects/my_experiment'
Path(OUTPUT_DIR).mkdir(parents=True, exist_ok=True)
Path(f'{OUTPUT_DIR}/models').mkdir(parents=True, exist_ok=True)

# Then save
trainer.save(f'{OUTPUT_DIR}/models/{symbol}.pt')

GPU Cleanup Pattern

python
# After each training run (from training.ipynb)
import gc
trainer.cleanup()
del trainer, env, prices
gc.collect()
torch.cuda.empty_cache()

Failed Attempts (Session 2026-02-01)

AttemptWhy it FailedCorrect Approach
Combined drive mount + extractExtract fails before mount completesSeparate cells
Python os.path.exists() for extractionDidn't have drive mountedUse %%bash with conditionals
Gave user instructions on errorUser frustrationJust copy training.ipynb
Created notebook from scratchMany missing piecesAlways start from training.ipynb
Forgot output directoriestrainer.save() failedmkdir before any save
Different pip install listImport errorsMatch training.ipynb exactly

Checklist for New Notebooks

  • Cells 1-6 copied exactly from training.ipynb
  • Drive mount is separate cell (not combined)
  • %%bash used for extraction (not Python)
  • API keys loaded from config/ files
  • Output directories created with mkdir
  • GPU cleanup after each training run
  • prefetch_all_data() used for data loading

Files

code
notebooks/training.ipynb         # CANONICAL TEMPLATE
notebooks/agent_validation_analysis.ipynb  # Example following pattern

References

  • Skill: colab-unzip-workflow - File paths and API keys
  • Skill: persistent-cache-gap-filling - Data caching configuration
  • Skill: gpu-memory-cleanup - GPU cleanup patterns