我正在尝试使用时间序列数据预测 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/