Tensorflowjs 在训练时产生形状错误

Tensorflowjs yields shape error when training

我正在尝试使用 tensorflow 版本 tfjs@0.12.0 训练一个简单的网络模型,并且我正在使用层 API。它只是一个将两个数字作为输入的网络,returns 相同的两个数字 - 因此网络应该尝试学习单位矩阵。

我正在使用 6 个示例进行训练,因此我的输入和输出的形状都是 [6,2],但是我得到了一个形状错误。

我的 javascript 文件如下:

function setup() {
    const model = tf.sequential();

    const hidden = tf.layers.dense({
        units: 4,
        useBias: true,
        activation:'sigmoid',
        inputDim: [2],
    });

    model.add(hidden);

    const output = tf.layers.dense({
        units: 2,
        activation:'sigmoid',
    });

    model.add(output);

    model.compile({
        optimizer: 'sgd',
        loss: 'meanSquaredError',
    });

    const xs = tf.tensor2d([
        [0.25, 0.92],
        [0.12,0.3],
        [0.4,0.74],
        [0.3,0.82],
        [0.09,0.95],
        [0.53,0.2],
    ]);

    const ys = tf.tensor2d([
        [0.25, 0.92],
        [0.12,0.3],
        [0.4,0.74],
        [0.3,0.82],
        [0.09,0.95],
        [0.53,0.2],
    ]);


    console.log(model.fit(xs,ys));
}

然而,当我 运行 脚本时,我得到一个形状错误:

Promise { <state>: "rejected" }
sketch.js:46:5
Error: Error when checking input: expected dense_Dense1_input to have shape [,2], but got array with shape [6,2].

在您的 hidden 图层声明中设置 inputDim: 2inputShape: [2]