GRULayer<T>

Layers · AiDotNet.NeuralNetworks.Layers

Represents a Gated Recurrent Unit (GRU) layer for processing sequential data.

Inheritance: LayerBase<T> → GRULayer<T>

Implements: IShapeContract

For Beginners

This layer helps neural networks understand sequences of data, like sentences or time series.

Think of the GRU as having a “memory” that helps it understand context:

  • When reading a sentence, it remembers important words from earlier
  • When analyzing stock prices, it remembers relevant trends from previous days
  • It uses special “gates” to decide what information to keep or forget

For example, in the sentence “The clouds were dark and it started to ___”, the GRU would recognize the context and predict “rain” because it remembers the earlier words about dark clouds.

GRUs are simpler versions of LSTMs (Long Short-Term Memory) but often perform similarly well while being more efficient to train.

How It Works

The GRU (Gated Recurrent Unit) layer is a type of recurrent neural network layer that is designed to capture dependencies over time in sequential data. It addresses the vanishing gradient problem that standard recurrent neural networks face when dealing with long sequences. The GRU uses update and reset gates to control the flow of information, allowing the network to retain relevant information over many time steps while forgetting irrelevant details.

Constructors

Constructor Summary
GRULayer(int hiddenSize, IVectorActivationFunction<T> vectorActivation, bool returnSequences, IVectorActivationFunction<T> vectorRecurrentActivation, bool stateful) Lazy ctor with vector activation functions.
GRULayer(int hiddenSize, IVectorActivationFunction<T> vectorActivation, bool returnSequences, IVectorActivationFunction<T> vectorRecurrentActivation, bool stateful, bool resetAfter) Lazy ctor with vector activation functions and the reset gate’s placement stated.
GRULayer(int hiddenSize, bool returnSequences, IActivationFunction<T> activation, IActivationFunction<T> recurrentActivation, bool stateful) Lazy ctor with vector activation functions.
GRULayer(int hiddenSize, bool returnSequences, IActivationFunction<T> activation, IActivationFunction<T> recurrentActivation, bool stateful, bool resetAfter) Lazy ctor with vector activation functions and the reset gate’s placement stated.

Properties

Property Summary
bool ResetAfter { get; } Whether the reset gate applies to the recurrent product (PyTorch / Keras reset_after=True).
bool SupportsTraining { get; } Gets a value indicating 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() Resets the internal state of the 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 with the given vector of parameter values.
void UpdateParameters(Vector<T> parameters) Updates the parameters of the layer with the given vector of parameter values.
void UpdateParametersGpu(IGpuOptimizerConfig config) GPU-resident parameter update with polymorphic optimizer support.
void ZeroGrad() Clears all gradient fields discovered by convention ({paramName}Gradient).