BidirectionalLayer<T>

Layers · AiDotNet.NeuralNetworks.Layers

Represents a bidirectional layer that processes input sequences in both forward and backward directions.

Inheritance: LayerBase<T> → BidirectionalLayer<T>

Implements: IShapeContract

For Beginners

This layer looks at input data in two ways at the same time - both forward and backward.

Think of it like reading a sentence:

  • Forward reading: “The cat sat on the mat” (left to right)
  • Backward reading: “mat the on sat cat The” (right to left)

By processing data in both directions:

  • The layer can understand context from both past and future elements
  • It can discover patterns that might be missed if only looking in one direction
  • It often improves performance on sequence tasks like text processing

For example, in the sentence “The bank is by the river”, the meaning of “bank” depends on both previous words (“The”) and future words (“by the river”). A bidirectional layer helps capture these relationships.

How It Works

A bidirectional layer processes input sequences in two directions: forward (from first to last) and backward (from last to first). This approach allows the layer to capture patterns that depend on both past and future context. The outputs from both directions can either be merged (typically added together) or kept separate, depending on the configuration.

Constructors

Constructor Summary
BidirectionalLayer(LayerBase<T> innerLayer, bool mergeMode, IActivationFunction<T> activationFunction, IEngine engine) Initializes a new instance of the BidirectionalLayer class with the specified inner layer and a vector activation function.
BidirectionalLayer(LayerBase<T> innerLayer, bool mergeMode, IVectorActivationFunction<T> vectorActivationFunction, IEngine engine) Initializes a new instance of the BidirectionalLayer class with the specified inner layer and a vector activation function.

Properties

Property Summary
bool SupportsGpuTraining { get; } Gets a value indicating whether this layer supports GPU-resident training.
bool SupportsTraining { get; } The computation engine (CPU or GPU) for vectorized operations.

Methods

Method Summary
IReadOnlyList<ILayer<T>> GetSubLayers()
IReadOnlyList<OutputAxisContract> OutputAxesFor(int inputRank)
void ClearGradients()
void ResetState() Resets the internal state of the bidirectional layer and its inner layers.
void UpdateParameters(T learningRate) Updates the parameters of both forward and backward layers using the calculated gradients.
void UpdateParametersGpu(IGpuOptimizerConfig config) Performs the backward pass on GPU tensors.