Class NBEATSBlock<T>
- Namespace
- AiDotNet.TimeSeries
- Assembly
- AiDotNet.dll
Represents a single block in the N-BEATS architecture.
public class NBEATSBlock<T>
Type Parameters
TThe 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
lookbackWindowintThe number of historical time steps used as input.
forecastHorizonintThe number of future time steps to predict.
hiddenLayerSizeintThe size of hidden layers in the fully connected network.
numHiddenLayersintThe number of hidden layers.
thetaSizeBackcastintThe size of the theta vector for backcast basis expansion.
thetaSizeForecastintThe size of the theta vector for forecast basis expansion.
useInterpretableBasisboolWhether to use interpretable basis functions.
polynomialDegreeintThe 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
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
inputNodeComputationNode<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
inputVector<T>The input time series vector of length lookbackWindow.
Returns
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
parametersVector<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.