TorchLean API

NN.API.Models.ResNet

Residual Convolutional Classifier #

The model accepts any batchShape and spatial rank. Each residual stage chooses its own channel widths, convolution geometry, and shortcut. Global average pooling reduces every spatial axis before the classifier head.

The shortcut added to a residual stage's two-convolution branch.

  • identity {d : ℕ} : Shortcut d

    Reuse the input unchanged; the branch must preserve its complete feature shape.

  • projection {d : ℕ} (convolution : Convolution.Config d) : Shortcut d

    Project the input with a separately configured convolution.

Instances For

    Two convolutions with a ReLU between them, an explicit shortcut, and a final ReLU.

    • First branch convolution; its stride may downsample the input grid.

    • Second branch convolution.

    • shortcut : Shortcut d

      Shortcut whose output must match the branch's channels and spatial grid.

    Instances For

      Grid after both branch convolutions.

      Instances For
        @[reducible, inline]
        abbrev TorchLean.nn.models.ResNet.Stage.Config.outputShape {d : ℕ} (stage : Config d) (spatial : Tensor ℕ [d]) (batchShape : Shape := []) :

        Feature shape after adding the branch and shortcut.

        Instances For
          def TorchLean.nn.models.ResNet.Stage.Config.validate {d : ℕ} (stage : Config d) (inputChannels : ℕ) (spatial : Tensor ℕ [d]) :

          Check both branch convolutions and the explicit shortcut before allocating parameters.

          Instances For

            Configuration for a residual classifier over d spatial axes.

            • inputChannels : ℕ

              Number of channels in each input sample.

            • spatial : Tensor ℕ [d]

              Size of each input axis. Values such as [32, 32] work directly.

            • Convolutional stem, followed by a ReLU.

            • stages : List (Stage.Config d)

              Residual stages in execution order. An empty list pools the stem output directly.

            • classCount : ℕ

              Number of classifier logits per sample.

            Instances For

              Validate the complete residual classifier before allocating any branch parameters.

              Instances For
                Instances For
                  @[reducible, inline]
                  abbrev TorchLean.nn.models.ResNet.Config.inputShape {d : ℕ} (config : Config d) (batchShape : Shape := []) :

                  Input tensor shape with an arbitrary batch shape.

                  Instances For
                    @[reducible, inline]
                    abbrev TorchLean.nn.models.ResNet.Config.outputShape {d : ℕ} (config : Config d) (batchShape : Shape := []) :

                    Classifier output shape with the same batch shape as the input.

                    Instances For
                      def TorchLean.nn.models.ResNet.Internal.buildStage {d : ℕ} (channels : ℕ) (spatial : Tensor ℕ [d]) (stage : Stage.Config d) (batchShape : Shape) :
                      Builder (Sequential (batchShape.concat ((spatial.to Shape).prependDim channels)) (stage.outputShape spatial batchShape))

                      Build one stage, retaining the branch-first parameter and addition order.

                      Instances For
                        def TorchLean.nn.models.resnet {d : ℕ} (config : ResNet.Config d) (batchShape : Shape := []) :
                        Builder (Sequential (config.inputShape batchShape) (config.outputShape batchShape))

                        Build the configured stem and residual stages, then global pooling and a linear classifier.

                        Instances For
                          def TorchLean.nn.models.resnet.buildStages {d : ℕ} (config : ResNet.Config d) (batchShape : Shape) (channels : ℕ) (spatial : Tensor ℕ [d]) (stages : List (ResNet.Stage.Config d)) :
                          Builder (Sequential (batchShape.concat ((spatial.to Shape).prependDim channels)) (config.outputShape batchShape))
                          Instances For