autopycoin.layers.TrendBlock#

class autopycoin.layers.TrendBlock(*args, **kwargs)[source]#

Trend block definition.

This layer represents the smaller part of the nbeats model. Final layers are constrained which define a polynomial function of small degree p. Therefore it is possible to get explanation from this block.

Parameters
label_widthint

Horizon time to forecast.

p_degreeint | float

Degree of the polynomial function.

n_neuronsint

Number of neurons in Fully connected layers.

drop_ratefloat

Rate of the dropout layer. This is used to estimate the epistemic error. Expected a value between 0 and 1. Default to 0.

Raises
ValueError

drop_rate is not between 0 and 1. All others arguments are not strictly positive integers.

Notes

input shape: N-D tensor with shape: (…, batch_size, time step). The most common situation would be a 2D input with shape (batch_size, time step).

output shape: N-D tensor with shape: (…, batch_size, units). For instance, for a 2D input with shape (batch_size, units), the output would have shape (batch_size, units). With a QuantileLossError with 2 quantiles or higher the output would have shape (quantiles, batch_size, units). If you add 2 variables, the output would have shape (variables, quantiles, batch_size, units).

Examples

>>> from autopycoin.layers import TrendBlock, SeasonalityBlock
>>> from autopycoin.models import Stack, NBEATS
>>> from autopycoin.losses import QuantileLossError
>>> trend_block = TrendBlock(label_width=10,
...                          p_degree=2,
...                          n_neurons=16,
...                          drop_rate=0.1,
...                          name="trend_block")
>>> seasonality_block = SeasonalityBlock(label_width=10,
...                                      forecast_periods=[10],
...                                      backcast_periods=[20],
...                                      forecast_fourier_order=[10],
...                                      backcast_fourier_order=[20],
...                                      n_neurons=15,
...                                      drop_rate=0.1,
...                                      name="seasonality_block")
>>> trend_blocks = [trend_block for _ in range(3)]
>>> seasonality_blocks = [seasonality_block for _ in range(3)]
>>> trend_stacks = Stack(trend_blocks, name="trend_stack")
>>> seasonality_stacks = Stack(seasonality_blocks, name="seasonality_stack")
>>> model = NBEATS([trend_stacks, seasonality_stacks], name="interpretable_NBEATS")
Attributes
label_widthint

Return the label_width.

input_widthint

Return the input_width.

p_degreeint

Return the degree of the trend equation.

input_specÌnputSpec

InputSpec instance(s) describing the input format for this layer.

drop_ratefloat

Return the drop rate.

is_interpretablebool

Return True if the block is interpretable.

is_g_trainablebool

Return True if the last layer is trainable.

block_typestr

Return the block type.

coefficient_factory(output_last_dim: int, p_degrees: tensorflow.python.framework.ops.Tensor) tensorflow.python.framework.ops.Tensor[source]#

Compute the coefficients used in the last layer a.k.a g layer.

Parameters
output_last_dimint
p_degree: int
Returns
coefficientstensor with shape (p_degree, label_width)

Coefficients of the g layer.

get_coefficients(output_last_dim: int, branch_name: str) tensorflow.python.framework.ops.Tensor[source]#

Return the coefficients calculated by the _coefficients_factory method.

Parameters
output_last_dimint
branch_namestr
Returns
coefficientsweight with shape (p_degree, label_width)

Coefficients of the g layer.

property p_degree: int#

Return the degree of the trend equation.