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.

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

  • batch :

    Number of independent samples processed together.

  • inChannels :

    Number of channels in each input sample.

  • spatial : Vector d

    Extent of each spatial axis.

  • patch : Conv d

    Convolution that extracts and embeds patches.

  • outDim :

    Number of classifier outputs per sample.

  • numHeads :

    Number of attention heads.

  • headDim :

    Width of each attention head.

  • ffnHidden :

    Width of the feed-forward sublayer.

Instances For

    Spatial extent of the patch embedding.

    Instances For

      Number of patch tokens.

      Instances For

        Number of flattened features passed to the classifier.

        Instances For

          Input shape (batch, inChannels, spatial...).

          Instances For

            Shape produced by patch embedding.

            Instances For

              Token shape (batch, sequence, embedding).

              Instances For

                Classifier output shape (batch, outDim).

                Instances For

                  flatten the patch grid into a sequence and move channels to the final axis.

                  Instances For
                    def TorchLean.nn.models.vit {d : } (cfg : VitConfig d) (hInChannels : cfg.inChannels 0 := by decide) (hSeqLen : cfg.seqLen 0 := by decide) (hModel : cfg.patch.outChannels 0 := by decide) :

                    Build patch embedding, one Transformer encoder block, and a linear classifier.

                    Instances For