Skip to content

Repository files navigation

DecodeAI

Decode AI from first principles. No black boxes. No hand-waving.

This repository is built on a simple belief: you cannot truly master AI by calling model.fit(). To understand how modern AI systems actually work, you need to build them from scratch — derive the math, implement the algorithms, and watch the gradients flow.

Every notebook in this repository dissects a core AI concept by implementing it from the ground up using raw PyTorch and NumPy. We go from the bias-variance tradeoff all the way to building GPT, LLaMA, DeepSeek, and GRPO — the same algorithm behind DeepSeek-R1. If a concept matters, we don't just explain it. We build it, break it, and rebuild it until the intuition is earned.

"What I cannot create, I do not understand." — Richard Feynman


Table of Contents

01 - Machine Learning

#NotebookDescription
01Data ProcessingBias-variance tradeoff, feature scaling, data splitting, and preprocessing pipelines
02RegressionLinear and polynomial regression — cost functions, gradient descent, and regularization
03ClassificationLogistic regression, SVMs, decision trees, random forests, and ensemble methods
04ClusteringK-Means, DBSCAN, hierarchical clustering — algorithms, objective functions, and evaluation
05Dimension ReductionPCA derivation, eigenvalue decomposition, and t-SNE for high-dimensional data

02 - Deep Learning Foundation

#NotebookDescription
01Neural Network FoundationsNumPy vectorization, broadcasting, forward/backward pass from scratch
02Activation FunctionsSigmoid, tanh, ReLU, GELU — why activations matter, saturation, and dying neurons
03Weight InitializationWhy zero init fails, variance explosion/vanishing, Xavier and He initialization proofs
04NormalizationBatch norm, layer norm, group norm — internal covariate shift and loss landscape smoothing
05RegularizationL2 weight decay, dropout, early stopping — fighting overfitting with math
06Residual ConnectionThe degradation problem, skip connections, and why deeper networks can fail without them
07Loss FunctionBCE, cross-entropy derivations — why sigmoid+BCE and softmax+CE produce clean gradients
08OptimizerSGD, momentum, RMSProp, Adam — from vanilla gradient descent to adaptive learning rates
09Model ClassificationEnd-to-end image classification on CIFAR-10 applying all the foundations above

03 - Large Language Model

RNN

#NotebookDescription
01Vanilla RNNRNN cell from scratch — hidden states, BPTT, vanishing/exploding gradients
02Recurrent ClassifierSentiment classification on IMDb using RNN/LSTM with padding and packing
03RNN with AttentionSeq2seq bottleneck problem, Bahdanau attention for date format translation

Transformer Models

#NotebookDescription
A01Pretrained Model - HuggingFaceUsing HuggingFace pipelines and pretrained models for text classification
A02Attention MechanismBahdanau vs Luong attention — the information bottleneck and its solution
A03TransformerFull transformer architecture from scratch — multi-head attention, positional encoding, encoder-decoder
B01BERTBidirectional encoder — WordPiece tokenization, MLM, NSP, and the fine-tuning paradigm
B02ColBERTLate interaction retrieval — MaxSim scoring, query augmentation, token-level matching
C01nanoGPTGPT-2 from scratch — byte-level BPE tokenization, causal self-attention, autoregressive decoding
C02LLaMALLaMA architecture deep dive — RMSNorm, RoPE, SwiGLU, grouped-query attention
C03Mistral MoEMixture of Experts — sparse routing, expert parallelism, sliding window attention
C04DeepSeekMulti-head Latent Attention (MLA) — 24x KV-cache reduction via low-rank compression
C05QwenAdvanced RoPE scaling — Position Interpolation, NTK-Aware, Dynamic NTK, YaRN

Text Retrieval & NLP

#NotebookDescription
01Text EmbeddingCosine similarity, dot product, L2 distance — similarity metrics and embedding spaces
02HNSWApproximate nearest neighbors — HNSW, Product Quantization, IVF for vector search
03Topic ModelingDiscovering latent topics from text corpora
04NERNamed Entity Recognition — BIO tagging, CoNLL-2003, token classification
05RAGRetrieval-Augmented Generation — chunking strategies, vector stores, retrieval pipeline
06Advanced RAGAdvanced retrieval techniques — re-ranking, hybrid search, query transformation

Post-Training Alignment

#NotebookDescription
01Instruction TuningFine-tuning Pythia-2.8B on Dolly 15k — prompt formatting, loss masking on response tokens
02SFTSupervised Fine-Tuning — the first step after pretraining in the LLM pipeline
03Reward ModelORM vs PRM — Bradley-Terry loss, step-level credit assignment for reasoning
04DPO vs ORPO and SimPODirect Preference Optimization — aligning LLMs with human preferences without RL
05GRPO with RLVRGroup Relative Policy Optimization — the algorithm behind DeepSeek-R1, with verifiable rewards
06PEFT (LoRA / QLoRA)LoRA and QLoRA from scratch — low-rank adaptation, 4-bit quantization, 99.6% fewer parameters
07AbliterationMechanistic interpretability — finding and removing the refusal direction in activation space

Model Compression

#NotebookDescription
01DistillationKnowledge distillation — response-level SFT, logit-level KD, rejection sampling
02Model PruningUnstructured and structured pruning — magnitude-based, layer-wise, and global strategies
03QuantizationFP32 to INT4 — numeric formats, quantization schemes, memory-accuracy tradeoffs

Agentic LLM

#NotebookDescription
A01LLM PromptingSystem prompt design patterns — persona, task-specific, guard-rails, few-shot, chain-of-thought
A02LangChainLangChain fundamentals — data loaders, splitters, vectorstores, embeddings, retrieval chains
A03Agent HarnessThe agent loop primitive — tool calling, finish reasons, state management from scratch
A04Agent GatewayIntelligence layers — BASE, IDENTITY, SOUL, MEMORY, SKILLS, TOOLS, CONTEXT, HEARTBEAT
A05Agent OperationProduction observability — logs, metrics, traces, cost attribution, latency profiling
A06Self Learning LoopReflexion and verbal gradients — self-critique, reflection injection, iterative improvement
B01LangGraph AgentCyclic state graphs with LangGraph — conditional edges, tool routing, RAG agents, LangSmith tracing
B02Claude CodeReverse-engineering Claude Code's agent loop — stop reasons, tool execution, harness internals

Production & Inference

#NotebookDescription
01GenerationText generation from scratch — KV-cache, sampling strategies, batched/continuous batching, speculative decoding

LLM Evaluation

#NotebookDescription
01LLM EvaluationPerplexity, intrinsic evaluation metrics — measuring how well a model predicts text

04 - Computer Vision

#NotebookDescription
01CNN FoundationsConvolutions from scratch in NumPy — zero-padding, forward/backward pass, pooling, and gradient derivations
02CNN ArchitectureBaseline CNN to ResNet on FashionMNIST — vanishing gradients and why skip connections work
03Transfer LearningFeature extraction vs fine-tuning on CIFAR-10 — ImageNet normalization, layer freezing strategies
04Object DetectionIoU, NMS, anchor box assignment, and YOLO output decoding — built from scratch
05Image SegmentationU-Net encoder/decoder from scratch — skip connections, pixel-wise loss, SegFormer inference
06Metric LearningSiamese networks, contrastive loss, triplet loss, and FaceNet — embedding spaces for unseen classes
07Vision TransformersViT, DeiT, and Swin from scratch — patch embedding, multi-head self-attention, hierarchical windows
08Contrastive LearningSimCLR, CLIP, and DINOv2 — self-supervised pretraining with NT-Xent loss
09Diffusion ModelDDPM, DDIM, Stable Diffusion — forward/reverse process, noise schedules, ContextUNet
10Model ExplainabilitySaliency maps, GradCAM, Integrated Gradients, and SHAP on ResNet-50

05 - Multi-Modal

#NotebookDescription
01Bridge ArchitectureConnecting frozen ViT to frozen LLM — LLaVA projectors, Flamingo Perceiver, BLIP-2 Q-Former, MoE bridges
02Vision Language ModelQwen-VL style VLM from scratch — TinyViT, MLP projector, mRoPE, visual token insertion, Stage 1 training
03Instruction TuningStage 2–3 VLM training — visual instructions, multi-turn dialog, RLHF-V for hallucination reduction
04Reasoning & InferenceVLM inference pipeline — decoding strategies, streaming, chain-of-thought, visual grounding, evaluation
05Audio & SpeechWaveforms to Mel spectrograms from scratch — audio encoders, Whisper, Phi-4 multimodal speech
06VideoVideo understanding — spatial-temporal attention, ViViT, dynamic FPS sampling, text-timestamp alignment
07Visual Agent & Computer UseVLMs that act on GUIs — perception, planning, action loops, computer-use agents
08Native MultimodalAny-to-any unified token spaces — Chameleon, Transfusion, Emu3, Janus Pro with VQ-VAE tokenizers

Coming Soon

TopicDescription
Training StrategyTraining data curation, loss functions, distributed training, and GPU programming
Model ServingvLLM, PagedAttention, autoscaling, and production deployment
MLOpsExperiment tracking, model versioning, CI/CD for ML, monitoring, and drift detection
LLM BenchmarksMMLU, HumanEval, GSM8K — standardized evaluation and leaderboard methodology
AI GovernanceRed teaming, toxicity benchmarks, bias evaluation, hallucination detection

More work is coming. This repository is actively maintained and expanding as the field evolves.

About

No description, website, or topics provided.

Resources

Stars

86 stars

Watchers

4 watching

Forks

Releases

Packages

Used by

Contributors

Languages