缺少值的多元LSTM [英] Multivariate LSTM with missing values

查看:69
本文介绍了缺少值的多元LSTM的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用LSTM解决时间序列预测问题. 输入包含多个功能,因此我使用的是多元LSTM. 问题是缺少一些值,例如:

I am working on a Time Series Forecasting problem using LSTM. The input contains several features, so I am using a Multivariate LSTM. The problem is that there are some missing values, for example:

    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.

Instead of interpolating the missing values, that can introduce bias in the results, because sometimes there are a lot of consecutive timestamps with missing values on the same feature, I would like to know if there is a way to let the LSTM learn with the missing values, for example, using a masking layer or something like that? Can someone explain to me what will be the best approach to deal with this problem? I am using Tensorflow and Keras.

推荐答案

如FrançoisChollet(Keras的创建者)在

As suggested by François Chollet (creator of Keras) in his book, one way to handle missing values is to replace them with zero:

通常,使用神经网络可以安全地输入缺少的值,例如 0,但前提条件是0尚未是有意义的值.这 网络将从接触数据中获悉值0表示 缺少数据,将开始忽略该值.请注意,如果您 期望测试数据中缺少值,但是网络已经过培训 在没有任何缺失值的数据上,网络将不会学会 忽略缺失的值!在这种情况下,您应该人为地 生成缺少条目的训练样本:复制一些训练 多次采样,然后删除一些您期望的功能 在测试数据中很可能会丢失.

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元素.)

So you can assign zero to NaN elements, considering that zero is not used in your data (you can normalize the data to a range, say [1,2], and then assign zero to NaN elements; or alternatively, you can normalize all the values to be in range [0,1] and then use -1 instead of zero to replace NaN elements.)

另一种替代方法是在Keras中使用 Masking 层.您为其指定一个掩码值,例如0,它将删除所有其特征均等于掩码值的任何时间步(即行).但是,以下所有图层都应支持屏蔽,并且您还需要预处理数据并将屏蔽值分配给一个时间步长(包括一个或多个NaN功能)的所有功能.来自Keras文档的示例:

Another alternative way is to use a Masking layer in Keras. You give it a mask value, say 0, and it would drop any timestep (i.e. row) where all its features are equal to the mask value. However, all the following layers should support masking and you also need to pre-process your data and assign the mask value to all the features of a timestep which includes one or more NaN features. Example from Keras doc:

考虑形状为(samples, timesteps,features)的Numpy数据数组x, 送入LSTM层.您想掩盖时间步长#3 和#5,因为您缺乏这些时间步长的数据.您可以:

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:

  • 设置x[:, 3, :] = 0.x[:, 5, :] = 0.

LSTM层之前的mask_value=0.插入一个Masking层:

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))

这篇关于缺少值的多元LSTM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆