javascript - Tensorflow.js LSTM 时间序列预测

标签 javascript tensorflow lstm rnn tensorflow.js

我正在尝试使用 LSTM RNN 在 Tensorflow.js 中构建一个简单的时间序列预测脚本。显然我是 ML 的新手。我一直在尝试从 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();

还有我的错误:

enter image description here

最佳答案

代码通过添加 returnSequences: true 并将输出层单位更改为 1 来运行:

//hidden layer
const hidden = tf.layers.lstm({
    units: 3,
    activation: 'sigmoid',
    inputShape: [3 , 1],
    returnSequences: true
});
model.add(hidden);

//output layer
const output = tf.layers.lstm({
    units: 1, 
    activation: 'sigmoid',
    returnSequences: true
})
model.add(output);

正如@Sebastian Speitel 提到的,将输入更改为:

const inputs = tf.tensor3d( [[[1],[1],[0]]] );

关于javascript - Tensorflow.js LSTM 时间序列预测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50728673/

相关文章:

tensorflow - 将 MetaGraphDef 转移到 GraphDef 的简单方法?

Tensorflow 服务 - 有状态 LSTM

javascript - 未捕获的类型错误 : Cannot read property 'action' of null - WordPress Dashboard Not Loading

javascript - 如何从 topojson 文件中获取文本标签以多边形显示?

javascript - 如何在 EXTJS 中添加位于另一个网格面板内的 Ext.window.window 模式

javascript - 亚马逊支付与 Javascript 和 PHP 的集成 - PaymentPlanNotSet

machine-learning - 看到检查点文件的内容了吗?

python - sklearn已安装但无法导入

python - tensorflow LSTM 模型中的 NaN 损失

python - 使用 Keras 和 TensorFlow 实现 LSTM 网络