错误:大小(XX)必须与形状x,x,x,x

时间:2019-10-14 20:54:21

标签: tensorflow tensorflow.js

这是一个新手问题,但是任何帮助将不胜感激。

我在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

谢谢!

软件规范:

  • tfjs节点:v1.2.11
  • Node JS:v11.2.0
  • 操作系统:Ubuntu 18.04.2

1 个答案:

答案 0 :(得分:0)

好吧,问题在于完全连接的层(ts.layer.dense)期望以tensor1d作为输入,如另一个问题Why do we flatten the data before we feed it into tensorflow?

所述:

因此,要解决这个问题,必须在完全连接的层之前重新调整张量,如下所示:

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

-1中的ts.reshape(tensor, [-1])处,意味着变换函数将张量展平。

要进行视觉演示,请观看以下YouTube视频:CNN Flatten Operation Visualized