我试图用lstm rnn在tensorflow.js中构建一个简单的时间序列预测脚本。很明显我是新来的。我一直在尝试从keras rnn/lstm层api改编js代码,这显然是相同的事情。从我收集的资料来看,我的图层、形状等都是正确的。你觉得我在这里做错了什么?
async function predictfuture(){
////////////////////////
// create fake data
///////////////////////
var xs = tf.tensor3d([
[[1],[1],[0]],
[[1],[1],[0]],
[[1],[1],[0]],
[[1],[1],[0]],
[[1],[1],[0]],
[[1],[1],[0]]
]);
xs.print();
var ys = tf.tensor3d([
[[1],[1],[0]],
[[1],[1],[0]],
[[1],[1],[0]],
[[1],[1],[0]],
[[1],[1],[0]],
[[1],[1],[0]]
]);
ys.print();
////////////////////////
// create model w/ layers api
///////////////////////
console.log('Creating Model...');
/*
model design:
i(xs) h o(ys)
batch_size -> * * * -> batch_size
timesteps -> * * * -> timesteps
input_dim -> * * * -> input_dim
*/
const model = tf.sequential();
//hidden layer
const hidden = tf.layers.lstm({
units: 3,
activation: 'sigmoid',
inputShape: [3 , 1]
});
model.add(hidden);
//output layer
const output = tf.layers.lstm({
units: 3,
activation: 'sigmoid',
inputShape: [3] //optional
});
model.add(output);
//compile
const sgdoptimizer = tf.train.sgd(0.1)
model.compile({
optimizer: sgdoptimizer,
loss: tf.losses.meanSquaredError
});
////////////////////////
// train & predict
///////////////////////
console.log('Training Model...');
await model.fit(xs, ys, { epochs: 200 }).then(() => {
console.log('Training Complete!');
console.log('Creating Prediction...');
const inputs = tf.tensor2d( [[1],[1],[0]] );
let outputs = model.predict(inputs);
outputs.print();
});
}
predictfuture();
我的错误是: