Table of Contents

Class NBEATSBlock<T>

Namespace
AiDotNet.TimeSeries
Assembly
AiDotNet.dll

Represents a single block in the N-BEATS architecture.

public class NBEATSBlock<T>

Type Parameters

T

The numeric type used for calculations (e.g., float, double).

Inheritance
NBEATSBlock<T>
Inherited Members

Remarks

Each N-BEATS block consists of: 1. A stack of fully connected layers (the "theta" network) 2. A basis expansion layer for generating backcast (reconstruction of input) 3. A basis expansion layer for generating forecast (prediction of future)

The block architecture implements a doubly residual stacking principle: - Backcast residual: Input minus backcast is passed to the next block - Forecast addition: Forecasts from all blocks are summed for the final prediction

For Beginners: A block is the basic building unit of N-BEATS. Think of it like a specialized predictor that: 1. Looks at the input time series 2. Tries to reconstruct what it saw (backcast) 3. Predicts the future (forecast) 4. Passes the "leftover" patterns it couldn't explain to the next block

Multiple blocks work together, with each one focusing on different aspects of the data.

Constructors

NBEATSBlock(int, int, int, int, int, int, bool, int)

Initializes a new instance of the NBEATSBlock class.

public NBEATSBlock(int lookbackWindow, int forecastHorizon, int hiddenLayerSize, int numHiddenLayers, int thetaSizeBackcast, int thetaSizeForecast, bool useInterpretableBasis, int polynomialDegree = 3)

Parameters

lookbackWindow int

The number of historical time steps used as input.

forecastHorizon int

The number of future time steps to predict.

hiddenLayerSize int

The size of hidden layers in the fully connected network.

numHiddenLayers int

The number of hidden layers.

thetaSizeBackcast int

The size of the theta vector for backcast basis expansion.

thetaSizeForecast int

The size of the theta vector for forecast basis expansion.

useInterpretableBasis bool

Whether to use interpretable basis functions.

polynomialDegree int

The polynomial degree for trend basis (if interpretable).

Remarks

For Beginners: This creates a new block with specific parameters: - lookbackWindow: How far back in time the block looks - forecastHorizon: How far forward in time the block predicts - hiddenLayerSize: How many neurons in each hidden layer (bigger = more capacity) - numHiddenLayers: How many hidden layers (deeper = more complex patterns) - useInterpretableBasis: Whether to use human-understandable basis functions

Properties

ParameterCount

Gets the total number of trainable parameters in the block.

public int ParameterCount { get; }

Property Value

int

Methods

ExportComputationGraph(ComputationNode<T>)

Exports the block as computation graph nodes for JIT compilation.

public (ComputationNode<T> backcast, ComputationNode<T> forecast) ExportComputationGraph(ComputationNode<T> inputNode)

Parameters

inputNode ComputationNode<T>

The input computation node (residual from previous block).

Returns

(ComputationNode<T>, ComputationNode<T>)

A tuple containing (backcast, forecast) computation nodes.

Remarks

This method creates a computation graph that represents the forward pass through the N-BEATS block, enabling JIT compilation for optimized inference.

For Beginners: This converts the block's calculations into a format that can be optimized by the JIT compiler. The resulting computation graph represents: 1. Passing input through fully connected layers with ReLU activation 2. Computing theta parameters for backcast and forecast 3. Applying basis expansion to generate backcast and forecast

Forward(Vector<T>)

Performs the forward pass through the block.

public (Vector<T> backcast, Vector<T> forecast) Forward(Vector<T> input)

Parameters

input Vector<T>

The input time series vector of length lookbackWindow.

Returns

(Vector<T> mean, Vector<T> logVar)

A tuple containing (backcast, forecast) vectors.

Remarks

The forward pass: 1. Passes input through fully connected layers with ReLU activation 2. Computes theta parameters for backcast and forecast 3. Applies basis expansion to generate backcast and forecast

For Beginners: This is where the block actually processes the input data. It takes the historical time series, runs it through the neural network layers, and produces two outputs: - Backcast: The block's attempt to reconstruct the input (what it understood) - Forecast: The block's prediction of future values

GetParameters()

Gets all parameters (weights and biases) as a single vector.

public Vector<T> GetParameters()

Returns

Vector<T>

A vector containing all trainable parameters.

Remarks

For Beginners: This method collects all the weights and biases from all layers into one long vector. This is useful for saving the model or for certain optimization algorithms.

SetParameters(Vector<T>)

Sets all parameters (weights and biases) from a single vector.

public void SetParameters(Vector<T> parameters)

Parameters

parameters Vector<T>

A vector containing all trainable parameters.

Remarks

For Beginners: This is the opposite of GetParameters - it takes a long vector of numbers and distributes them back to all the weights and biases in the block. This is useful for loading a saved model.