javascript - 为 tensorflow.js lstm 将 1d 数组 reshape 为 3d

标签 javascript tensorflow lstm tensorflow.js

我正在尝试使用时间序列数据预测 future 的股票价格。我有一个包含 251 个时间步的 xs 数组,以及一个包含该时间步相应股票价格的 ys 数组。我已将 xs 数组 reshape 为 3d,但出现错误

'输入张量应与目标张量具有相同数量的样本。找到 1 个输入样本和 251 个目标样本。'

模型的代码如下。

var xs = [];
var ys = [];
for(i in result){
    xs.push(i);
    ys.push(result[i].close);
    }

    var xt = tf.tensor3d(xs, [1,xs.length,1]);
    var yt = tf.tensor2d(ys, [xs.length, 1]);
    //xt.reshape([1, xs.length, 1]).print();
    //yt.reshape([1, ys.length, 1]).print();
    var lstm1 = tf.layers.lstm({units: 32, returnSequences: true, inputShape:[xs.length,1]});
    var model = tf.sequential();
    model.add(lstm1);
    model.add(tf.layers.dropout({rate:0.2}));
    model.add(tf.layers.lstm({units:5}));
    model.add(tf.layers.dropout({rate:0.2}));
    model.add(tf.layers.dense({units:1, inputShape:[32], activation:'softmax'}));
    model.compile({optimizer:'adam', loss:'categoricalCrossentropy'});



    model.fit(xt, yt, {epochs:1000}).then(() => {

  bestfit = model.predict(tf.tensor(xs, [xs.length,1])).dataSync();

最佳答案

错误似乎来自 model.fit(x, y),因为 x 和 y 的形状似乎不匹配。

x 的形状为 [1, 251, 1],y 的形状为 [251, 1]。这不起作用,因为 x 中的特征多于 y 中的标签。您必须 reshape x 或 y。

  • reshape x:x.reshape([251, 1, 1])x.reshape([251, 1])

  • reshape y:y.reshape([1, 251])y.reshape([1, 251, 1])

注意:只要前两个维度大小相等并且所有维度大小的乘积等于251. reshape 的重点是不要失去特征和标签之间的相关性

关于javascript - 为 tensorflow.js lstm 将 1d 数组 reshape 为 3d,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51938540/

相关文章:

javascript - Telerik RadConfirm 不适用于 asp.net 的 UserControls

javascript - 从 url javascript 中提取部分

machine-learning - LSTM 历史长度与预测误差

python - categorical_accuracy 值是什么意思? categorical_accuracy 100% 但数据显示错误预测

javascript - 仅在跨越某个点时运行某些代码

javascript - 检查传递的参数是否为 Backbone.js View 或命令

Python:在不使用迭代的情况下将for循环操作扩展到矩阵中的每一行

python - 在 TensorflowJS 中构建神经网络

python - tf.sub 和 tensorflow 中的减法运算有什么区别?

tensorflow - 如何处理keras中多元LSTM的多步时间序列预测