neural-network-from-scratch
Construct, train, diagnose, and translate mathematical feedforward neural networks from NumPy matrix calculus to modular PyTorch architectures. Do not use for pre-trained model fine-tuning or high-level API wrappers.
- License 许可
- MIT
- 最近更新
- 2026-11-15
- Stars
- ★ 0
- Forks
- 0
01 前言 Front Matter
- name
- neural-network-from-scratch
- description
- Construct, train, diagnose, and translate mathematical feedforward neural networks from NumPy matrix calculus to modular PyTorch architectures. Do not use for pre-trained model fine-tuning or high-level API wrappers.
02 资源文件 5
- 参考文档CARD.md4.9 KB
- 参考文档config.default.yaml598 B
- 参考文档references/backpropagation-calculus-guide.md4.5 KB
- 参考文档references/numpy-to-pytorch-translation-table.md3.6 KB
- 脚本scripts/scratch_nn.py9.0 KB
03 SKILL.md 正文
Neural Network from Scratch
neural-network-from-scratch is a deep-module agent skill for engineering, training, diagnosing, and migrating mathematical neural networks. Distilled from Eva J Patel's foundational curriculum, it operationalizes matrix calculus, forward propagation, analytical backpropagation derivatives, numerical stability safeguards, and modular translation to PyTorch.
See CARD.md for companion quick reference cards, CLI commands, and lifecycle matrices.
Core Operational Pillars
Every scratch neural network engagement enforces three architectural pillars:
- The Visual Brief — Interactive HTML briefs generated in
%TEMP%/neural-network-from-scratch-<timestamp>.htmlwith live Mermaid.js mathematical DAG diagrams, tensor dimension contracts, and convergence diagnostic scorecards. - The Mandatory Checkpoint — Human-in-the-loop validation via
implementation_plan.mdwithRequestFeedback: true. The agent must halt and await user confirmation before modifying network topologies, updating hyperparameters, or executing extended training loops. - Behavioral Boundaries & Anti-Patterns — Rigid negative guardrails that reject unclamped logarithms, misaligned inner matrix multiplications, zero-variance initializations, and black-box autograd shortcuts.
Authoritative Tooling & Execution Seams
The skill provides deterministic, non-interactive execution engines in scripts/:
python scripts/scratch_nn.py train [--epochs 10000] [--lr 0.1] [--seed 42] [--json]: Trains the 2-layer network on the XOR dataset and outputs convergence metrics.python scripts/scratch_nn.py verify [--json]: Executes deterministic convergence diagnostics asserting loss reduction and 100% classification accuracy.
Consult co-located architectural reference guides in references/:
- When deriving chain-rule partial derivatives or verifying analytical equations, consult
backpropagation-calculus-guide.mdfor full step-by-step calculus proofs, matrix dimensions, and the $\tanh'$ derivative identity. - When migrating verified mathematical algorithms to production deep learning pipelines, consult
numpy-to-pytorch-translation-table.mdfor direct side-by-side mapping totorch.nn.Module, autograd, and optimizer abstractions.
Execution Stages
Stage 1: Dimension & Parameter Topology Framing
Establish network topology, define layer dimension boundaries, and initialize parameter matrices with reproducible pseudorandom seeds.
Procedure
- Define input dimensionality $D_{in} = 2$, hidden neuron count $D_h = 4$, and output dimensionality $D_{out} = 1$.
- Seed the pseudorandom generator (
np.random.seed(42)) to ensure deterministic training runs. - Initialize weight matrices using Gaussian distributions (
np.random.randn):- First layer weights $W_1$ with shape $(D_{in}, D_h) = (2, 4)$.
- Second layer weights $W_2$ with shape $(D_h, D_{out}) = (4, 1)$.
- Initialize bias vectors to zeros (
np.zeros):- Hidden bias $b_1$ with shape $(1, D_h) = (1, 4)$.
- Output bias $b_2$ with shape $(1, D_{out}) = (1, 1)$.
- Verify inner matrix dimension alignment: ensure inner dimensions match for dot products $X \cdot W_1$ and $a_1 \cdot W_2$.
Completion Gate:
Weight and bias matrix shapes verified with inner dimension alignment
Stage 2: Forward Propagation & Activation Calculus
Compute intermediate linear combinations and non-linear activations across both layers.
Procedure
- Compute the hidden layer linear combination: $z_1 = X W_1 + b_1$ with shape $(N, 4)$.
- Apply the non-linear hyperbolic tangent activation function: $a_1 = \tanh(z_1)$ mapping values into $(-1, 1)$.
- Compute the output layer linear combination: $z_2 = a_1 W_2 + b_2$ with shape $(N, 1)$.
- Apply the Sigmoid activation function: $a_2 = \sigma(z_2) = \frac{1}{1 + e^{-z_2}}$ mapping values into $(0, 1)$ probability space.
- Clip extreme exponent values inside Sigmoid if necessary to prevent numerical floating point overflow.
Completion Gate:
Forward linear combinations and non-linear activations computed
Stage 3: Loss Function Formulation & Error Measurement
Quantify network prediction errors using Binary Cross-Entropy (BCE) fortified with numerical stability floors.
Procedure
- Formulate Binary Cross-Entropy across $N$ training examples: $$L = -\frac{1}{N} \sum \left[ y \ln(a_2) + (1 - y) \ln(1 - a_2) \right]$$
- Protect against $\ln(0)$ infinity errors by applying an epsilon floor $\epsilon = 10^{-8}$: $$L = -\frac{1}{N} \sum \left[ y \ln(a_2 + 10^{-8}) + (1 - y) \ln(1 - a_2 + 10^{-8}) \right]$$
- Measure the initial loss at epoch 0 (for XOR with seed 42, initial loss is $\approx 0.70$).
- Establish convergence criteria: require final loss $< 0.05$ and 100% classification accuracy on XOR targets.
Completion Gate:
Binary cross-entropy loss computed with numerical stability epsilon floor
Stage 4: Chain-Rule Backpropagation & Parameter Updates
Derive and compute analytical gradients across all parameters, updating weights via gradient descent.
Procedure
- Compute the output pre-activation error gradient: $dz_2 = a_2 - y$ with shape $(N, 1)$ per
backpropagation-calculus-guide.md. - Compute output parameter gradients:
- $dW_2 = \frac{1}{N} a_1^T dz_2$ with shape $(4, 1)$.
- $db_2 = \frac{1}{N} \sum dz_2$ with shape $(1, 1)$.
- Backpropagate error gradient to hidden layer activations: $da_1 = dz_2 W_2^T$ with shape $(N, 4)$.
- Compute hidden pre-activation gradient using the $\tanh'$ derivative identity $(1 - a_1^2)$: $$dz_1 = da_1 \odot (1 - a_1^2) \quad \text{with shape } (N, 4)$$
- Compute hidden parameter gradients:
- $dW_1 = \frac{1}{N} X^T dz_1$ with shape $(2, 4)$.
- $db_1 = \frac{1}{N} \sum dz_1$ with shape $(1, 4)$.
- Apply gradient descent update scaled by learning rate $\eta = 0.1$: $$W_2 \leftarrow W_2 - \eta dW_2, \quad b_2 \leftarrow b_2 - \eta db_2$$ $$W_1 \leftarrow W_1 - \eta dW_1, \quad b_1 \leftarrow b_1 - \eta db_1$$
Completion Gate:
Analytical chain-rule gradients computed and parameter update applied
Stage 5: Modular PyTorch Translation & Convergence Diagnostics
Verify end-to-end training convergence on XOR and translate verified NumPy logic into modular PyTorch architecture per numpy-to-pytorch-translation-table.md.
Procedure
- Execute full training loop for 10,000 epochs via
python scripts/scratch_nn.py train --epochs 10000. - Verify convergence: assert final loss is strictly less than initial loss, and assert predicted XOR truth table matches ${0, 1, 1, 0}$ with $100%$ accuracy.
- Translate NumPy arrays and manual gradients into idiomatic
torch.nn.Modulesubclasses implementingnn.Linear,nn.Tanh,nn.Sigmoid, andtorch.optim.SGD. - Run automated diagnostic verification via
python scripts/scratch_nn.py verify.
Completion Gate:
XOR training convergence verified with 100% accuracy and PyTorch equivalent translated
Anti-Patterns
- Unclamped Logarithm Loss Evaluation — Computing
np.log(a2)directly without adding $\epsilon = 10^{-8}$, causing catastrophicNaNor-infcrashes when model predictions approach boundary states. - Transposed Dimension Mismatch — Multiplying tensors with misaligned inner dimensions (e.g. attempting
X @ dz1instead ofX.T @ dz1), causing execution crashes or silent incorrect broadcasts. - Monotonic Loss Assertion Assumption — Expecting XOR binary cross-entropy loss to decrease strictly monotonically on every epoch, causing false-positive test failures during normal optimization oscillation.
- Missing Gradient Batch Normalization — Omitting the division by batch size $N$ in $dW_1 = (X^T dz_1) / N$, causing effective gradient scale to explode when changing dataset sizes.
- Symmetric Weight Initialization — Initializing weight matrices with all zeros instead of random Gaussian numbers, preventing hidden neurons from breaking symmetry during gradient descent.
- Untethered PyTorch Autodiff Dependency — Depending prematurely on complex autograd libraries for simple algorithmic problems where pure NumPy matrix calculus provides superior transparency and zero dependencies.
04 同仓库更多 Skills
ui-behavior-primitive-architect
Architect, audit, and verify zero-dependency headless UI behavior primitives, WebMCP agent bridges, WAI-ARIA/SSR/RTL tri-modal checks, and framework expert routing. Do not use for static CSS theme styling or backend database migrations.
trustgraph-ontology-governor
Govern knowledge graphs, SHACL-AF triple policies, OntologyRAG subclass validation, PROV-O dual named graphs, and Laya entropy decision scoring. Do not use for relational SQL queries or generic web scraping.
shim-harness-governor
Govern universal CLI agent loops, 4-tier tool permissions, ancestor-symlink workspace sandboxes, deterministic-first command routing, collision-safe MCP namespacing, and layered skill loading. Do not use for static UI styling or external SQL analytics.
graphiti-zep-memory-governor
Govern bi-temporal knowledge graphs with Graphiti future-edge clamping, MinHash/LSH entity dedup, RRF/MMR reranking, and Zep typed ontologies. Do not use for static SQL joins or generic web crawling.
agora-mcp-workbench-architect
Architect, audit, and orchestrate Agora Workbench MCP servers, loopback sidecars, unified state graphs, gateway/dispatcher connectors, and policy-enforced data lakes. Do not use for static frontend CSS styling or non-MCP desktop GUI automation.
skill-eval-assurance-runner
Evaluate and assure agent skills using small-N Wilson confidence intervals, unbiased pass^k reliability, deterministic trajectory graders, preflight boundary honesty, and sealed digest verification. Do not use for subjective prose review or unconstrained production deployment.