Error: Size(XX) must match the product of shape x,x,x,x

Error: Size(XX) must match the product of shape x,x,x,x

这是一个新手问题,但我们将不胜感激。

我在 TensorFlow.JS(节点)中遇到 3D 张量问题,代码如下:

const tf = require('@tensorflow/tfjs-node');

(async ()=>{

    let list = [
        {
            xs: [
                [
                    [ 0.7910133603149169, 0.7923634491520086, 0.79166712455722, 0.7928027625311359, 0.4426631841175303, 0.018719529693542337 ],
                    [ 0.7890709817505044, 0.7943561081665688, 0.7915865358198619, 0.7905450669351226, 0.4413258183256521, 0.04449784810703526 ],
                    [ 0.7940229392692819, 0.7924745639669473, 0.7881395357356101, 0.7880208892359736, 0.40902353356570315, 0.14643954229459097 ],
                    [ 0.801474878324385, 0.8003822349633881, 0.7969969705961001, 0.7939094034872144, 0.40227041242732126, 0.03893523221469505 ],
                    [ 0.8022503526561848, 0.8011600386679555, 0.7974621873981194, 0.8011488339557422, 0.43008361179994464, 0.11210020422004835 ],
                ],
                [
                    [ 0.8034111510684465, 0.7985390234525179, 0.7949321830852709, 0.7943788081438548, 0.5739870761673189, 0.13358267460835263 ],
                    [ 0.805714476773561, 0.8072996569653942, 0.8040745782073486, 0.8035592212810225, 0.5899031300445114, 0.03229758335964042 ],
                    [ 0.8103322733081704, 0.8114317495511435, 0.8073606480159334, 0.8057140734135828, 0.5842202187553198, 0.01986941729798157 ],
                    [ 0.815132106874313, 0.8122641403791668, 0.8104353115275772, 0.8103395749739932, 0.5838313552472632, 0.03332674037143093 ],
                    [ 0.8118480102237944, 0.8166500561770489, 0.8128943005604122, 0.8147644523703373, 0.601619389872815, 0.04807286626501376 ],
                ]
            ],
            ys: 1
        }
    ];


    const ds = tf.data.generator(async () => {

        let index = 0;

        return {
            next: async () => {

                if(index >= list.length) return { done : true };
                let doc = list[index];
                index++;

                return {
                    value: {
                        xs : doc.xs,
                        ys : doc.ys
                    },
                    done: false
                };
            }
        };
    }).batch(1);

    let model = tf.sequential();
    model.add(tf.layers.dense({units: 60, activation: 'relu', inputShape: [2, 5, 6]}));

    model.compile({
        optimizer: tf.train.adam(),
        loss: 'sparseCategoricalCrossentropy',
        metrics: ['accuracy']
    });

    await model.fitDataset(ds, {epochs: 1});

    return true;

})().then(console.log).catch(console.error);

此代码生成以下错误:

Error: Size(60) must match the product of shape 1,2,5,60
    at Object.inferFromImplicitShape

我不明白为什么该层将 inputShape 的最后一个值从 6 更改为 60(这是该层的预期输出单位)。

只是为了确认,据我所知 units 应该是以下产品:batchSize * x * y * z,在示例中:1 * 2 * 5 * 6 = 60

谢谢!

软件规格:

好的,问题是全连接层 (ts.layer.dense) 需要 tensor1d 作为输入,如另一个问题所述:

所以,为了做到这一点,张量必须在全连接层之前重新整形,如:

                return {
                    value: {
                        xs : ts.reshape(doc.xs, [-1]),
                        ys : doc.ys
                    },
                    done: false
                };

ts.reshape(tensor, [-1])中的-1表示变换函数将张量展平

对于视觉演示,这里是 YouTube 视频:CNN Flatten Operation Visualized