TorchLean API

NN.API.Models.Vit

Vision Transformer #

Patch embedding is an arbitrary-dimensional convolution. The spatial output is flattened into a token axis before the Transformer block, so the construction applies equally to one-dimensional signals, images, volumes, and higher-dimensional grids.

How a vision Transformer turns encoded tokens into one vector per sample.

  • mean : Pooling

    Average every encoded patch token.

  • cls : Pooling

    Prepend a learned class slot and select it after the encoder.

Instances For

    Configuration for a Transformer encoder over patches from a d-dimensional spatial domain.

    • inputChannels :

      Number of channels in each input sample.

    • spatial : Tensor [d]

      Extent of each spatial axis.

    • patchEmbedding : Convolution.Config d

      Convolution that extracts and embeds patches.

    • headCount :

      Number of attention heads. Must be positive.

    • headWidth :

      Width of each attention head. Must be positive.

    • feedForwardWidth :

      Width of the feed-forward sublayer.

    • layerCount :

      Number of Transformer encoder blocks.

    • dropout? : Option Float

      Dropout probability for attention and feed-forward outputs.

    • attentionDropout? : Option Float

      Drop attention probabilities before value aggregation; independent of residual dropout.

    • feedForwardDropout? : Option Float

      Drop activated FFN hidden units before their output projection.

    • attentionInputBias : Bool

      Enable trainable query, key, and value biases while preserving the legacy default layout.

    • pooling : Pooling

      Classifier readout; .cls also prepends a learned class token.

    Instances For

      Attach a classifier output width to reusable encoder settings.

      Instances For

        Encoder settings embedded in a classifier configuration.

        Instances For

          Validate patch extraction and the complete Transformer template before allocating parameters.

          Instances For

            Validate both the reusable encoder and classifier head before construction.

            Instances For

              Grid produced by patch embedding.

              Instances For

                Number of patch tokens.

                Instances For

                  Number of slots inserted before the patch sequence.

                  Instances For

                    Number of tokens passed through the Transformer encoder.

                    Instances For

                      Number of scalar features in the complete encoded token sequence.

                      Instances For
                        @[reducible, inline]

                        Input shape for any caller-supplied batch shape.

                        Instances For
                          @[reducible, inline]

                          Patch tensor produced by the embedding convolution.

                          Instances For
                            @[reducible, inline]

                            Patch tokens after moving the embedding width to the final axis.

                            Instances For
                              @[reducible, inline]

                              Tokens consumed and produced by the Transformer stack.

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

                                Classifier input shape for any caller-supplied batch shape.

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

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

                                  Instances For
                                    def TorchLean.nn.models.Internal.patchesToTokens {d : } (config : ViT.EncoderConfig d) (batchShape : Shape := []) :
                                    Layer (config.patchShape batchShape) (config.patchTokenShape batchShape)

                                    Implementation layer that turns a patch grid into a token sequence.

                                    Instances For
                                      def TorchLean.nn.models.Internal.prependClassToken {d : } (config : ViT.EncoderConfig d) (batchShape : Shape := []) :
                                      Sequential (config.patchTokenShape batchShape) (batchShape.concat [1 + config.patchCount, config.patchEmbedding.outChannels])

                                      Prepend one shared, trainable class token to every independently mapped sequence.

                                      Instances For
                                        def TorchLean.nn.models.Internal.tokensToChannels {d : } (config : ViT.EncoderConfig d) (batchShape : Shape := []) :
                                        Sequential (config.outputShape batchShape) (batchShape.concat [config.patchEmbedding.outChannels, config.sequenceLength])

                                        Implementation layer that moves the embedding width before the token axis.

                                        Instances For
                                          def TorchLean.nn.models.Internal.meanVitTokens {d : } (config : ViT.EncoderConfig d) (batchShape : Shape := []) :
                                          Builder (Sequential (config.outputShape batchShape) (batchShape.appendDim config.patchEmbedding.outChannels))

                                          Implementation layer for mean-pool classification.

                                          Instances For
                                            def TorchLean.nn.models.Internal.firstVitToken {d : } (config : ViT.EncoderConfig d) (batchShape : Shape := []) :
                                            Sequential (config.outputShape batchShape) (batchShape.appendDim config.patchEmbedding.outChannels)

                                            Implementation layer for class-token classification.

                                            Instances For
                                              def TorchLean.nn.models.vitEncoder {d : } (config : ViT.EncoderConfig d) (batchShape : Shape := []) :
                                              Builder (Sequential (config.inputShape batchShape) (config.outputShape batchShape))

                                              Build the patch and Transformer portion of a vision transformer.

                                              Mean readout leaves the patch sequence unchanged. Class-token readout prepends one learned token before the Transformer. Classification, reconstruction, and other tasks can attach their own heads without rebuilding the patch pipeline.

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

                                                Build a vision Transformer encoder followed by a linear classifier.

                                                Instances For