Tensorflow Js 中多时间序列预测的最佳方法

Best approach for multiple time series prediction in Tensor Flow Js

编辑: 这个答案很有帮助,但是,我真的很想要一些实际的张量流 javascript 代码来展示如何实现它。

我发现的其他问题不包括归一化或我预测未来 1 点以外的特定目标,其中要记住局部性,即如果在 T+1 预测 1,则更有可能是 T+2为 0.

总数据样本(实际有132个样本)

const Data = [
    [0,45,0,0],
    [1,40,0,0],
    [0,10,3,0],
    [0,0,0,0],
    [2,30,0,1], 
    [2,20,3,1]
 ];

数组表示 [X1,X2,X3,Y]

*注意 X2 需要归一化 --- 如果我们需要将 X2 预测为未来值以便在单个网络中执行所有这些操作时预测 Y,则不确定如何处理此问题

最终目标

使用此数据(具有更多样本)以最准确的方式预测接下来的 5 个 Y 值,同时权衡先前的模式,就像在时间序列预测中一样,过去的样本可能比那时的 25 个样本更重要。

当前进度

通过在 25 个 [X1,X2,X3] 数组上训练,预测了 25 个正向值,结果发现输入位置 1([X1,X2,X3] 数组 1)可能会影响接下来的所有 25 个 Y 值,但位置2 ([X1,X2,X3] 数组 2) 只能影响集合的 2-25 等等 --- 位置 25 只能影响 25 个预测值的最后一个值 --- 所以我是就网络而言,甚至可能无法真正预测接下来的 25 个。

当前方法

上训练后,期望的预测将是接下来的 3 个 Y 值(例如)

输入:

[
    [0,45,0],
    [1,40,0],
    [0,10,3]
]

输出

[
    [0],
    [1],
    [1]
]

目前尝试的模型

var model = tf.sequential();

model.add(tf.layers.batchNormalization({
    inputShape:[null,6],
    axis: 2,
    units:10,
    //returnSequences: true,
    //kernelInitializer: 'VarianceScaling'
}));

model.add(tf.layers.lstm({
    //inputShape:[null,7],
    units: 10,
    activation: 'relu',
    returnSequences: true,
    //kernelInitializer: 'VarianceScaling'
}));

model.add(tf.layers.lstm({
    //inputShape:[null,7],
    units: 6, 
    activation: 'relu',
    returnSequences: true,
    //kernelInitializer: 'VarianceScaling'
}));

//modelHelper(model);
const optimizer = tf.train.adam (.05);
//optimizer: 'sgd', loss: 'binaryCrossentropy', lr: 0.1
model.compile({
    loss:tf.losses.meanSquaredError,
    optimizer:optimizer
});

当每个时间位置(X1、X2、X3)使用多个值时,预测时间序列接下来的 4、5 或 25 个单个 (Y) 值的最佳方法是什么而不是只是下一个值?

*浏览 5 年后创建了一个帐户,所以迷失在这个帐户上。

这里有很多问题。

What is the best approach to predict the next 4 or 5 or 25 single (Y) values of a time series rather than just the next value

您只需要 return lstm 的序列。如果你想预测接下来的 4 个值,那么最后一个 lstm 层的 units 应该是 4,return returnSequences 设置为 true。

如果您想根据序列预测 1 或 0,则可以使用 binaryCrossEntropy 损失和最后一层的 softmax 激活来计算概率。至于最有可能的是什么,如果数据与你的观察非常一致,网络就会计算出来,即如果在 T 中预测到 1,那么接下来在 T+i 中很可能预测到 0。

X2 requires normalization --- not sure how to handle this if we need to predict X2 into future values in order to predict Y while doing all of this in a single network

这并不特定于您的用例,最佳做法是将所有数据保持在同一范围内。对于具有高方差的数据,往往会对影响收敛的模型产生很大影响。您可以在喂养模型之前标准化 x2 特征。这是一个将跨所有功能规范化数据的函数

// standardize a tensor data by computing (data - mean(data) ) / std(data)

function standardize(data) {
  let means = []
  let variances = []
  for ( let axe = 0 ; axe < axes; axe++) {
    const {mean, variance} = tf.moments(data.gather([axe], 1), undefined, true )
    means.push(mean)
    variances.push(variances)
  }

  return data.sub(tf.concat(means).reshape([axes])).div(tf.concat(variances).reshape([axes]))
}