【问题标题】:Tensorflow.js Model Dimension MismatchTensorflow.js 模型维度不匹配
【发布时间】:2021-03-30 21:47:57
【问题描述】:

我是 tensoflow.js 的新手,我在创建 CNN 模型时遇到问题,因为尺寸不匹配。我有一个使用 tf.browser.fromPixels(image); 的 3d 数组。

但是当我尝试训练我的人工智能时它不会启动,我收到了消息:Uncaught (in promise) Error: Error when checking target: expected dense_Dense2 to have shape [6,6,4], but got array with shape [6,6,3].

这里是完整的代码:

image = new Image(32, 32);
data = tf.browser.fromPixels(image); //to get pixel array

model = tf.sequential();
encoder = tf.layers.dense({units: 4, batchInputShape:[6, 6, 3], activation: 'relu', kernelInitializer:"randomNormal", biasInitializer:"ones"});
decoder = tf.layers.dense({units: 4, activation: 'relu'});
model.add(encoder);
model.add(decoder);
model.compile({loss:'meanSquaredError', optimizer:tf.train.adam()});

async function botTraining(model, epochs = 60){ //train ai 60 epochs
    history = 
    await model.fit(data, data,{ epochs: epochs + 1,
        callbacks:{
            onEpochEnd: async(epoch, logs) =>{
                console.log("Epoch:" + epoch + " Loss:" + logs.loss);
            }
        }
    });
}

【问题讨论】:

    标签: javascript tensorflow conv-neural-network png autoencoder


    【解决方案1】:

    好的,这是我的代码:

    image = new Image(32, 32);
    data = tf.browser.fromPixels(image); //to get pixel array
    
    model = tf.sequential();
    encoder = tf.layers.dense({units: 4, batchInputShape:[6, 6, 3], activation: 'relu', kernelInitializer:"randomNormal", biasInitializer:"ones"});
    decoder = tf.layers.dense({units: 4, activation: 'relu'});
    model.add(encoder);
    model.add(decoder);
    model.compile({loss:'meanSquaredError', optimizer:tf.train.adam()});
    
    async function botTraining(model, epochs = 60){ //train ai 60 epochs
        history = 
        await model.fit(data, data,{ epochs: epochs + 1,
            callbacks:{
                onEpochEnd: async(epoch, logs) =>{
                    console.log("Epoch:" + epoch + " Loss:" + logs.loss);
                }
            }
        });
    }
    

    我发现我的“bachInputSize”需要设置为与decoder 单位相同

    这是固定代码:

    image = new Image(4, 4);
    data = tf.browser.fromPixels(image);
    
    model = tf.sequential();
    encoder = tf.layers.dense({units: 3, batchInputShape:[null, null, 3], activation: 'relu', kernelInitializer:"randomNormal", biasInitializer:"ones"});
    decoder = tf.layers.dense({units: 3, activation: 'relu'});
    model.add(encoder);
    model.add(decoder);
    model.compile({loss:'meanSquaredError', optimizer:tf.train.adam()});
    
    async function botTraining(model, epochs = 60){
        history = 
        await model.fit(data, data,{ epochs: epochs + 1,
            callbacks:{
                onEpochEnd: async(epoch, logs) =>{
                    console.log("Epoch:" + epoch + " Loss:" + logs.loss);
                }
            }
        });
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-12-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多