PatchEmbeddingLayer<T>

Layers · AiDotNet.NeuralNetworks.Layers

Implements a patch embedding layer for Vision Transformer (ViT) architecture.

Inheritance: LayerBase<T> → PatchEmbeddingLayer<T>

Implements: IShapeContract

For Beginners

This layer breaks an image into small square pieces (patches) and converts each patch into a numerical representation that can be processed by a transformer.

Think of it like cutting a photo into a grid of smaller squares, then describing each square with numbers. For example, a 224x224 image with 16x16 patches would be cut into 196 patches (14x14 grid), and each patch would be represented by a vector of numbers (the embedding).

This allows transformers, which were originally designed for text, to process images by treating the patches like “words” in a sentence.

How It Works

The patch embedding layer divides an input image into fixed-size patches and projects them into an embedding space. This is a key component of Vision Transformers, converting 2D spatial information into a sequence of embeddings that can be processed by transformer encoder blocks.

Constructors

Constructor Summary
PatchEmbeddingLayer(int patchSize, int embeddingDim, IActivationFunction<T> activationFunction, IInitializationStrategy<T> initializationStrategy)
PatchEmbeddingLayer(int patchSize, int embeddingDim, int expectedInputChannels, IActivationFunction<T> activationFunction, IInitializationStrategy<T> initializationStrategy) Eager-channel ctor: when the caller knows the input channel count at construction time (e.g.

Properties

Property Summary
bool SupportsTraining { get; } Indicates whether this layer supports training.

Methods

Method Summary
Dictionary<string, string> GetParameterRoles() Returns parameter roles for per-role optimizer configuration (e.g., weight decay exemption for biases).
IReadOnlyList<OutputAxisContract> OutputAxesFor(int inputRank)
IReadOnlyList<Tensor<T>> GetTrainableParameters() Returns all trainable parameter tensors marked with [TrainableParameter].
void ClearGradients()
void ResetState()
void SetTrainableParameters(IReadOnlyList<Tensor<T>> parameters) Replaces trainable parameter tensors (e.g., with ParameterBuffer views).
void UpdateParameters(T learningRate) Updates the layer’s parameters using the calculated gradients.
void UpdateParametersGpu(IGpuOptimizerConfig config) Updates layer parameters using GPU-resident optimizer.
void ZeroGrad() Clears all gradient fields discovered by convention ({paramName}Gradient).