TorchLean API

NN.API.Neural.Blocks

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:

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(...)).

      • hidden : List

        Hidden layer widths (each entry creates a Linear -> Activation stage).

      • activation : Activation

        Activation used after each hidden linear layer.

      • dropout? : Option Float

        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.

            Instances For
              def TorchLean.nn.Internal.blocks.convAct (leading : Spec.Shape := Spec.Shape.scalar) {d inChannels : } (spatial : Vector d) (cfg : ConvAct d) [NeZero inChannels] :

              Build a rank-polymorphic convolution/activation block.

              Instances For

                Convolution/activation followed by max pooling.

                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
                            def TorchLean.nn.Internal.blocks.residualBlock {input output : Spec.Shape} (main skip : Sequential input output) (act : Activation := Activation.relu) :
                            Sequential input output

                            Apply an activation after adding two branches with the same output shape.

                            Instances For