SpatialTransformerLayer<T>

Layers · AiDotNet.NeuralNetworks.Layers

Represents a spatial transformer layer that enables spatial manipulations of data via a learnable transformation.

Inheritance: LayerBase<T> → SpatialTransformerLayer<T>

Implements: IAuxiliaryLossLayer<T>, IShapeContract

For Beginners

This layer helps a neural network focus on the important parts of an image by learning to transform it.

Think of it like having a smart camera that can:

  • Zoom in on the important objects
  • Rotate images to make them easier to recognize
  • Crop out distractions
  • Fix distortions or perspective problems

Benefits include:

  • Automatic learning of the best transformation for the task
  • Improved recognition of objects regardless of their position or orientation
  • Better handling of distorted or warped inputs

For example, when recognizing handwritten digits, a spatial transformer might learn to straighten tilted digits or zoom in on the digit, making it easier for the rest of the network to classify.

How It Works

A spatial transformer layer performs learnable geometric transformations on input feature maps. It consists of three main components: a localization network that predicts transformation parameters, a grid generator that creates a sampling grid, and a sampler that applies the transformation using bilinear interpolation. This allows the network to automatically learn invariance to translation, scale, rotation, and more general warping.

Constructors

Constructor Summary
SpatialTransformerLayer(int outputHeight, int outputWidth, IActivationFunction<T> activationFunction, SpatialTransformerDataFormat dataFormat) Initializes a new instance of the SpatialTransformerLayer class with a vector activation function.
SpatialTransformerLayer(int outputHeight, int outputWidth, IVectorActivationFunction<T> vectorActivationFunction, SpatialTransformerDataFormat dataFormat) Initializes a new instance of the SpatialTransformerLayer class with a vector activation function.

Properties

Property Summary
T AuxiliaryLossWeight { get; set; } Gets or sets the weight for the auxiliary loss contribution.
bool SupportsTraining { get; }
bool UseAuxiliaryLoss { get; set; } Gets or sets a value indicating whether auxiliary loss is enabled for this layer.

Methods

Method Summary
Dictionary<string, string> GetAuxiliaryLossDiagnostics() Gets diagnostic information about the auxiliary loss computation.
Dictionary<string, string> GetDiagnostics() Gets diagnostic information about this component’s state and behavior.
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].
T ComputeAuxiliaryLoss() Computes the auxiliary loss for this layer based on transformation regularization.
void ClearGradients()
void ResetState() Resets the internal state of the spatial transformer layer.
void SetTrainableParameters(IReadOnlyList<Tensor<T>> parameters) Replaces trainable parameter tensors (e.g., with ParameterBuffer views).
void UpdateParameters(T learningRate) Updates the parameters of the layer using the calculated gradients.
void ZeroGrad() Clears all gradient fields discovered by convention ({paramName}Gradient).