具有缺失值的多元 LSTM

Multivariate LSTM with missing values

我正在使用 LSTM 处理时间序列预测问题。 输入包含多个特征,因此我使用的是多元 LSTM。 问题是有一些缺失值,例如:

    Feature 1     Feature 2  ...  Feature n
 1    2               4             nan
 2    5               8             10
 3    8               8              5
 4    nan             7              7
 5    6              nan            12

而不是插入缺失值,这会在结果中引入偏差,因为有时在同一特征上有很多连续的时间戳缺失值,我想知道是否有办法让LSTM 学习缺失值,例如,使用掩蔽层或类似的东西?有人可以向我解释什么是处理这个问题的最佳方法吗? 我正在使用 Tensorflow 和 Keras。

正如 François Chollet(Keras 的创建者)在 his book 中所建议的,处理缺失值的一种方法是将它们替换为零:

In general, with neural networks, it’s safe to input missing values as 0, with the condition that 0 isn’t already a meaningful value. The network will learn from exposure to the data that the value 0 means missing data and will start ignoring the value. Note that if you’re expecting missing values in the test data, but the network was trained on data without any missing values, the network won’t have learned to ignore missing values! In this situation, you should artificially generate training samples with missing entries: copy some training samples several times, and drop some of the features that you expect are likely to be missing in the test data.

所以你可以将零分配给 NaN 元素,考虑到你的数据中没有使用零(你可以将数据标准化到一个范围,比如 [1,2],然后将零分配给 NaN 元素;或者,您可以将所有值标准化为 [0,1] 范围内,然后使用 -1 而不是零来替换 NaN 元素。)

另一种替代方法是在 Keras 中使用 Masking 层。你给它一个掩码值,比如 0,它会丢弃所有特征都等于掩码值的任何时间步长(即行)。但是,以下所有图层都应支持掩码,您还需要预处理数据并将掩码值分配给包含一个或多个 NaN 特征的时间步长的所有特征。来自 Keras 文档的示例:

Consider a Numpy data array x of shape (samples, timesteps,features), to be fed to an LSTM layer. You want to mask timestep #3 and #5 because you lack data for these timesteps. You can:

  • set x[:, 3, :] = 0. and x[:, 5, :] = 0.

  • insert a Masking layer with mask_value=0. before the LSTM layer:

model = Sequential()
model.add(Masking(mask_value=0., input_shape=(timesteps, features)))
model.add(LSTM(32))

更新(2021 年 5 月): 根据 François Cholle 的更新建议,最好使用更有意义或信息量更大的值(而不是使用零)进行屏蔽缺失值。该值可以计算(例如平均值、中值等)或根据数据本身预测。