繁体   English   中英

Tensorflow.js Model 尺寸不匹配

[英]Tensorflow.js Model Dimension Mismatch

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

但是当我试图训练我的ai时它不会启动,并且我收到消息: 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);
            }
        }
    });
}

好的,这是我的代码:

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);
            }
        }
    });
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM