N-BEATS:可解释时间序列预测模型
Nbeats在时序预测领域算是比较有影响力的一个模型了,它开创了一个全新的时序数据预测backbone。让人印象深刻的是,Nbeats也只用了全连接层来实现时序的预测。
在Nbeats中,最为核心的内容是经由全连接层实现对时序数据的分解。而在拟合过程中,借鉴了GBDT拟合残差的过程,Nbeats每层拟合的是时间序列部分信息(即之前层拟合的残差)。
class TrendBlock(tf.keras.layers.Layer):
"""
Parameter
---------
p_degree: integer
Degree of the polynomial function.
horizon: integer
Horizon time to horizon.
back_horizon: integer
Past to rebuild.
n_neurons: integer
Number of neurons in Fully connected layers.
"""
def __init__(self, horizon, back_horizon, p_degree, n_neurons, dropout_rate, **kwargs):
super().__init__(**kwargs)
self._p_degree = tf.reshape(tf.range(p_degree + 1, dtype='float32'), shape=(-1, 1)) # Shape (-1, 1) in order to broadcast horizon to all p degrees
self._horizon = tf.cast(horizon, dtype='float32')
self._back_horizon = tf.cast(back_horizon, dtype='float32')
self._n_neurons = n_neurons
self._FC_stack = [tf.keras.layers.Dense(n_neurons, activation='relu',
kernel_initializer="glorot_uniform") for _ in range(4)]
self._dropout = tf.keras.layers.Dropout(dropout_rate)
self._FC_back_horizon = self.add_weight(shape=(n_neurons, p_degree + 1), trainable=True,
initializer="glorot_uniform", name='FC_back_horizon_trend')
self._FC_horizon = self.add_weight(shape=(n_quantiles, n_neurons, p_degree + 1), trainable=True,
initializer="glorot_uniform", name='FC_horizon_trend')
self._horizon_coef = (tf.range(self._horizon) / self._horizon) ** self._p_degree
self._back_horizon_coef = (tf.range(self._back_horizon) / self._back_horizon) ** self._p_degree
def call(self, inputs):
for dense in self._FC_stack:
inputs = dense(inputs) # shape: (Batch_size, n_neurons)
inputs =