GraphSpec Spatial Primitives #
This file extends the sequential GraphSpec core (NN.GraphSpec.Core) with
single-input/single-output spatial operations used by convolutional pipelines.
These are not model definitions. They are reusable nodes in the GraphSpec vocabulary:
Primitive.convwraps the arbitrary-rank convolution specification and runtime operation;Primitive.maxPoolwraps arbitrary-rank max pooling;Primitive.batchNormwraps normalization over an arbitrary spatial shape;Primitive.flattenis the bridge from image-like tensors to vector classifiers.
The corresponding model examples live under NN.GraphSpec.Models.
Important scope note:
- These primitives all fit the sequential language
Chain ps σ τbecause they have one input tensor and one output tensor (no merging of paths). - Residual networks require skip connections ($y+x$), which are multi-input and require
sharing. For that, use
NN.GraphSpec.DAG, whose DAG primitive constructors reuse these sequential adapters when possible.
Why only these spatial operations?
GraphSpec only exposes an operation once we have both sides of the contract in place:
- a pure Spec meaning, and
- an executable TorchLean program meaning.
The general always-available primitives (linear, relu, softmax) live in
NN.GraphSpec.Core; this file is the current spatial extension pack. More packs can be added as
we decide which runtime/spec operations should become architecture-level GraphSpec nodes.
Parameter convention (sequential GraphSpec) #
Each primitive has an explicit type-level parameter-shape list ps : List Shape.
For example, an N-D convolution is parameterized by:
kernel : Tensor α (outC :: inC :: kernelShape)bias : Tensor α [outC]
When you compose graphs with >>>, these parameter-shape lists concatenate, giving a typed
interface for model parameters.
References / citations (informal pointers) #
- Convolutional networks: LeCun et al. (1998), “Gradient-based learning applied to document recognition”.
- BatchNorm: Ioffe & Szegedy (2015), “Batch Normalization: Accelerating Deep Network Training…”.
- Global average pooling: Lin et al. (2013), “Network In Network”.
Arbitrary-rank convolution on a channels-first tensor with no batch axis.
Inputs:
- parameters
kernel, bias(in that order), - input tensor
x : (inChannels, spatial...).
Output:
The output has shape (outChannels, convOutSpatial spatial kernel stride padding...).
Instances For
Arbitrary-rank max pooling on a channels-first tensor (parameter-free).
Output shapes follow the standard pooling size formulas:
Each spatial axis uses the corresponding kernel, stride, and padding entry.
Instances For
Flatten any tensor to a 1D vector (parameter-free).
Output shape is [Spec.Shape.size s], i.e. a vector whose length is the number of
elements of the input shape.
This is a reshape/view operation (no arithmetic), used to connect convolutional features to a vector-valued classifier head.
PyTorch analogy: torch.flatten(x).
Instances For
BatchNorm over every axis in spatial, independently for each channel.
Parameters are the affine vectors (gamma, beta). GraphSpec keeps this primitive stateless;
running statistics belong to the stateful model layer.
Instances For
Chain constructor for Primitive.conv.
Instances For
Chain constructor for Primitive.maxPool.
Instances For
Chain constructor for Primitive.flatten.
Instances For
Chain constructor for Primitive.batchNorm.