用于时间序列预测的 Keras LSTM:预测特征向量

Keras LSTM for timeseries prediction: predicting vectors of features

我有一个包含 N 个观测值和 F 个特征的时间序列数据集。每个功能都可以显示 (1) 或不显示 (0)。所以数据集看起来像这样:

T    F1    F2    F3    F4    F5 ... F
0    1     0     0     1     0      0
1    0     1     0     0     1      1
2    0     0     0     1     1      0
3    1     1     1     1     0      0
...
N    1     1     0     1     0      0

我正在尝试使用基于 LSTM 的架构,根据观察值 T-W - T,预测哪些特征在时间 T+1 出现,其中 W 是某个时间的宽度 window。如果 W=4,则 LSTM 'sees' 4 个时间步进入过去以进行预测。 LSTM 需要 3D 输入,其形式为 (number_batches, W, F)。一个简单的 Keras 实现可能看起来像:

model = Sequential()
model.add(LSTM(128, stateful=True, batch_input_shape=(batch_size, W, F)))
model.add(Dense(F, activation='sigmoid'))

model.compile(loss='binary_crossentropy',
              optimizer='rmsprop',
              metrics=['accuracy'])
model.fit(x_train, y_train,
          batch_size=batch_size, epochs=250, shuffle=False,
          validation_data=(x_val, y_val))

我遇到的主要问题是:完整的数据集有大量的特征(> 200)并且特征相对较少出现,即 0 比 1 更常见。神经网络简单学习将所有值设置为 0,从而达到 'accuracy'.

的高度

本质上,我想用某个值对输入矩阵中的每个 1 进行加权以赋予它更多重要性,但我对如何在 Keras 中实现这一点感到困惑。我知道 Keras 中有一个选项 sample_weight,但它是如何工作的?例如,我不知道如何在我的示例中实现它。这是我遇到的问题的合理解决方案吗?此类问题通常使用哪些优化器和损失函数?

这是我用于二维高度不平衡数据的损失函数,效果很好。您可以将binary_crossentropy替换为另一种损失。

import keras.backend as K

def weightedByBatch(yTrue,yPred):

    nVec = K.ones_like(yTrue) #to sum the total number of elements in the tensor
    percent = K.sum(yTrue) / K.sum(nVec) #percent of ones relative to total
    percent2 = 1 - percent #percent of zeros relative to total   
    yTrue2 = 1 - yTrue #complement of yTrue (yTrue+ yTrue2 = full of ones)   

    weights = (yTrue2 * percent2) + (yTrue*percent)
    return K.mean(K.binary_crossentropy(yTrue,yPred)/weights)

对于您的 3D 数据,这可能有效,但也许您可以按列工作,为每个特征创建一对权重,而不是将所有特征加在一起。

可以这样做:

def weightedByBatch2D(yTrue,yPred):

    nVec = K.ones_like(yTrue) #to sum the total number of elements in the tensor
    percent = K.sum(K.sum(yTrue,axis=0,keepdims=True),axis=1,keepdims=True) / K.sum(K.sum(nVec,axis=0,keepdims=True),axis=1,keepdims=True) #percent of ones relative to total
    percent2 = 1 - percent #percent of zeros relative to total   
    yTrue2 = 1 - yTrue #complement of yTrue (yTrue+ yTrue2 = full of ones)   

    weights = (yTrue2 * percent2) + (yTrue*percent)
    return K.mean(K.binary_crossentropy(yTrue,yPred)/weights)