Reusable neural-network blocks.
This module defines public block constructors such as residual, convolutional, and MLP-style compositions built from the public layer-building API.
Small set of activation choices for block builders.
PyTorch analogues:
relu<->torch.nn.relugelu<->torch.nn.gelusilu<->torch.nn.silutanh<->torch.nn.tanhsigmoid<->torch.nn.sigmoid
- relu : Activation
- gelu : Activation
- silu : Activation
- tanh : Activation
- sigmoid : Activation
Instances For
Instances For
Interpret an Activation as a TorchLean layer.
Instances For
MLP (multi-layer perceptron) configuration.
This builder produces a sequential stack of linear layers with activations and optional dropout.
PyTorch analogue: a hand-written nn.Sequential(Linear(...), ReLU(), ..., Linear(...)).
- activation : Activation
Activation used after each hidden linear layer.
Optional dropout probability after each activation.
- seedBase : ℕ
Base seed used to deterministically initialize all linear layers (and dropout if present).
Instances For
Internal recursion for mlp.
This builds the sequential stack stage-by-stage, threading a seed so each linear (and optional dropout) layer gets a deterministic initialization key.
Instances For
Build an MLP as a sequential stack of linear layers and activations.
This is a small PyTorch-shaped constructor: a typical call looks like:
TorchLean.nn.blocks.mlp 784 10 { hidden := [128, 128], activation := .relu }.
Instances For
Convolution followed by an activation and optional dropout.
- conv : Conv d
- activation : Activation
- seedDropout : ℕ
Instances For
Build a rank-polymorphic convolution/activation block.
Instances For
Build a rank-polymorphic convolution/activation/max-pooling block.
Instances For
Residual/skip-connection layer as a single LayerDef.
Given inner : Seq s s, this builds a layer that computes
$x \mapsto \operatorname{inner}(x) + x$.
PyTorch analogue: $x + f(x)$ blocks used throughout ResNets and Transformers.
Instances For
Lift residualLayer into a sequential model.
Instances For
Branching (skip connections) #
Seq is linear, but we sometimes want a PyTorch-like $x \mapsto f(x) + g(x)$ block.
We expose this as a single LayerDef whose parameter list is params(f) ++ params(g) and whose
forward pass runs both programs and adds their outputs.
Combine two sequential branches into a single layer that adds their outputs.
The resulting layer runs both f and g on the same input $x$ and returns $f(x) + g(x)$.
Parameters are concatenated as params(f) ++ params(g).
Instances For
Combine two models with the same input/output shapes by summing their outputs.
This is a typed residual-add block: addBranches f g represents the model
$x \mapsto f(x) + g(x)$,
and its parameter list is the concatenation of the two branches’ parameter lists.
Instances For
Apply an activation after adding two branches with the same output shape.