TorchLean API

NN.Spec.Models.Cnn

Convolutional Network Specifications #

This module defines a two-block convolutional network over an arbitrary number of spatial axes. Its spatial parameters are vectors, so the same model definition applies to sequence, image, volume, and higher-rank data. Both the compositional module description and the explicit reverse-mode specification use the generic convolution and pooling operations.

Implementation status #

nn.models.cnn (NN/API/Models/Cnn.lean) builds a one-block classifier convolution -> activation -> max pool -> flatten -> linear, whereas this file specifies a two-block network; no theorem relates them. This specification is imported by NN/Runtime/PyTorch/Export/CNN.lean.

def Models.Cnn.blockOutSpatial {d : } (spatial kernel convStride convPadding poolKernel poolStride poolPadding : TorchLean.Tensor [d]) :

Spatial shape after one convolution followed by one pooling operation.

Instances For
    def Models.Cnn.outputSpatial {d : } (spatial kernel convStride₁ convPadding₁ convStride₂ convPadding₂ poolKernel poolStride₁ poolPadding₁ poolStride₂ poolPadding₂ : TorchLean.Tensor [d]) :

    Spatial shape after two convolution-pooling blocks.

    Instances For
      def Models.Cnn.featureShape {d : } (channels : ) (spatial kernel convStride₁ convPadding₁ convStride₂ convPadding₂ poolKernel poolStride₁ poolPadding₁ poolStride₂ poolPadding₂ : TorchLean.Tensor [d]) :

      Feature-map shape after the second pooling operation.

      Instances For
        def Models.Cnn.featureSize {d : } (channels : ) (spatial kernel convStride₁ convPadding₁ convStride₂ convPadding₂ poolKernel poolStride₁ poolPadding₁ poolStride₂ poolPadding₂ : TorchLean.Tensor [d]) :

        Number of scalar features presented to the linear head.

        Instances For
          def Models.Cnn.spec {α : Type} [TorchLean.Storage α] [Context α] [DecidableRel fun (x1 x2 : α) => x1 > x2] {d inChannels hiddenChannels outputSize : } {spatial kernel convStride₁ convPadding₁ convStride₂ convPadding₂ poolKernel poolStride₁ poolPadding₁ poolStride₂ poolPadding₂ : TorchLean.Tensor [d]} {hPoolKernel : ∀ (i : Fin d), poolKernel.getScalar i 0} {hPoolStride₁ : ∀ (i : Fin d), poolStride₁.getScalar i 0} {hPoolStride₂ : ∀ (i : Fin d), poolStride₂.getScalar i 0} (conv₁ : Spec.ConvSpec d inChannels hiddenChannels kernel convStride₁ convPadding₁ α) (conv₂ : Spec.ConvSpec d hiddenChannels hiddenChannels kernel convStride₂ convPadding₂ α) (pool₁ : Spec.MaxPoolSpec d poolKernel poolStride₁ poolPadding₁ hPoolKernel hPoolStride₁) (pool₂ : Spec.MaxPoolSpec d poolKernel poolStride₂ poolPadding₂ hPoolKernel hPoolStride₂) (head : Spec.LinearSpec α (featureSize hiddenChannels spatial kernel convStride₁ convPadding₁ convStride₂ convPadding₂ poolKernel poolStride₁ poolPadding₁ poolStride₂ poolPadding₂) outputSize) :

          Two convolution-pooling blocks followed by a linear head.

          Instances For
            def Models.Cnn.withReluSpec {α : Type} [TorchLean.Storage α] [Context α] [DecidableRel fun (x1 x2 : α) => x1 > x2] {d inChannels hiddenChannels outputSize : } {spatial kernel convStride₁ convPadding₁ convStride₂ convPadding₂ poolKernel poolStride₁ poolPadding₁ poolStride₂ poolPadding₂ : TorchLean.Tensor [d]} {hPoolKernel : ∀ (i : Fin d), poolKernel.getScalar i 0} {hPoolStride₁ : ∀ (i : Fin d), poolStride₁.getScalar i 0} {hPoolStride₂ : ∀ (i : Fin d), poolStride₂.getScalar i 0} (conv₁ : Spec.ConvSpec d inChannels hiddenChannels kernel convStride₁ convPadding₁ α) (conv₂ : Spec.ConvSpec d hiddenChannels hiddenChannels kernel convStride₂ convPadding₂ α) (pool₁ : Spec.MaxPoolSpec d poolKernel poolStride₁ poolPadding₁ hPoolKernel hPoolStride₁) (pool₂ : Spec.MaxPoolSpec d poolKernel poolStride₂ poolPadding₂ hPoolKernel hPoolStride₂) (head : Spec.LinearSpec α (featureSize hiddenChannels spatial kernel convStride₁ convPadding₁ convStride₂ convPadding₂ poolKernel poolStride₁ poolPadding₁ poolStride₂ poolPadding₂) outputSize) :

            The same network with ReLU after each convolution.

            Instances For
              def Models.Cnn.forward {α : Type} [TorchLean.Storage α] [Context α] [DecidableRel fun (x1 x2 : α) => x1 > x2] {d inChannels hiddenChannels outputSize : } {spatial kernel convStride₁ convPadding₁ convStride₂ convPadding₂ poolKernel poolStride₁ poolPadding₁ poolStride₂ poolPadding₂ : TorchLean.Tensor [d]} {hPoolKernel : ∀ (i : Fin d), poolKernel.getScalar i 0} {hPoolStride₁ : ∀ (i : Fin d), poolStride₁.getScalar i 0} {hPoolStride₂ : ∀ (i : Fin d), poolStride₂.getScalar i 0} (conv₁ : Spec.ConvSpec d inChannels hiddenChannels kernel convStride₁ convPadding₁ α) (conv₂ : Spec.ConvSpec d hiddenChannels hiddenChannels kernel convStride₂ convPadding₂ α) (pool₁ : Spec.MaxPoolSpec d poolKernel poolStride₁ poolPadding₁ hPoolKernel hPoolStride₁) (pool₂ : Spec.MaxPoolSpec d poolKernel poolStride₂ poolPadding₂ hPoolKernel hPoolStride₂) (head : Spec.LinearSpec α (featureSize hiddenChannels spatial kernel convStride₁ convPadding₁ convStride₂ convPadding₂ poolKernel poolStride₁ poolPadding₁ poolStride₂ poolPadding₂) outputSize) (x : TorchLean.Tensor α (Spec.Shape.ofList (inChannels :: spatial.to (List )))) :
              TorchLean.Tensor α [outputSize]

              Evaluate a convolutional chain on one input tensor.

              Instances For

                Hyperparameters for a two-block convolutional network of spatial rank d.

                Instances For
                  structure Models.TwoBlockCnn.Config.WF {d : } (config : Config d) :

                  Conditions needed by convolutional and pooling implementations.

                  Instances For

                    The default configuration at any spatial rank.

                    Instances For

                      The default configuration is well formed.

                      structure Models.TwoBlockCnn.Model {d : } (config : Config d) (inChannels : ) (spatial : TorchLean.Tensor [d]) (α : Type) [TorchLean.Storage α] (hCfg : config.WF) :

                      A generic two-block convolutional network with an explicit linear head.

                      Instances For
                        structure Models.TwoBlockCnn.Grads {d : } (config : Config d) (inChannels : ) (spatial : TorchLean.Tensor [d]) (α : Type) [TorchLean.Storage α] :

                        Parameter gradients for Model.

                        Instances For
                          def Models.TwoBlockCnn.Model.forward {α : Type} [TorchLean.Storage α] [Context α] {d : } {config : Config d} {inChannels : } {spatial : TorchLean.Tensor [d]} {hCfg : config.WF} (m : Model config inChannels spatial α hCfg) (x : TorchLean.Tensor α (Spec.Shape.ofList (inChannels :: spatial.to (List )))) :

                          Forward pass for Model.

                          Instances For
                            def Models.TwoBlockCnn.Model.backward {α : Type} [TorchLean.Storage α] [Context α] [DecidableRel fun (x1 x2 : α) => x1 > x2] {d : } {config : Config d} {inChannels : } {spatial : TorchLean.Tensor [d]} {hCfg : config.WF} (m : Model config inChannels spatial α hCfg) (x : TorchLean.Tensor α (Spec.Shape.ofList (inChannels :: spatial.to (List )))) (gradOutput : TorchLean.Tensor α [config.outputSize]) :
                            Grads config inChannels spatial α × TorchLean.Tensor α (Spec.Shape.ofList (inChannels :: spatial.to (List )))

                            Reverse-mode parameter and input derivatives for Model.

                            Instances For