首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Tensorflow JS模型拟合立即完成,无需执行任何操作

Tensorflow JS模型拟合立即完成,无需执行任何操作
EN

Stack Overflow用户
提问于 2019-02-21 04:56:29
回答 1查看 108关注 0票数 0

嗨,我正在尝试建立一个卷积神经网络,但我不能训练它。

代码如下:

代码语言:javascript
复制
model = tf.sequential();
model.add(tf.layers.conv2d({
    inputShape: [48, 48, 1],
    kernelSize: FILTER_SIZE,
    filters: 64,
    dataFormat: "channelsLast",
    activation: ActFunc.RELU
}));
model.add(tf.layers.maxPooling2d(maxPoolConf));
model.add(tf.layers.conv2d({
    kernelSize: FILTER_SIZE,
    filters: 128,
    dataFormat: "channelsLast",
    activation: ActFunc.RELU
}));
model.add(tf.layers.maxPooling2d(maxPoolConf));
model.add(tf.layers.conv2d({
    kernelSize: FILTER_SIZE,
    filters: 256,
    dataFormat: "channelsLast",
    activation: ActFunc.RELU
}));
model.add(tf.layers.maxPooling2d(maxPoolConf));
model.add(tf.layers.conv2d({
    kernelSize: FILTER_SIZE,
    filters: 512,
    dataFormat: "channelsLast",
    activation: ActFunc.RELU
}));
model.add(tf.layers.maxPooling2d(maxPoolConf));
model.add(tf.layers.flatten());
model.add(tf.layers.dense({units: 128, activation: 'relu'}));
model.add(tf.layers.dense({units: 256, activation: 'relu'}));
model.add(tf.layers.dense({units: 512, activation: 'relu'}));
model.add(tf.layers.dense({units: 1024, activation: 'relu'}));
model.add(tf.layers.dense({
    units: 7,
    activation: 'softmax'
}));
model.compile({
    optimizer: 'adam',
    loss: 'categoricalCrossentropy',
    metrics: ['accuracy', 'categoricalCrossentropy']
});

let image_tensor = tf.tensor4d(training_data.getInputData(), [training_data.length, 48, 48, 1]);
let correct_prediction_tensor = tf.tensor2d(training_data.getLabels(), [training_data.length, 7]);

const history = await model.fit(image_tensor, correct_prediction_tensor,
    {
        batchSize: 128,
        epochs: 10,
        shuffle: true,
        callbacks: {
            onEpochEnd: (epoch, logs) => {
                // Plot the loss and accuracy values at the end of every training epoch.
                console.log(epoch, logs);
            },
            onTrainStart: console.log("Starting Training..."),
            onTrainEnd: console.log("Training Finished!"),
        }
    });

当我运行这段代码时,它会打印“开始训练...”紧接着“训练结束了!”(它甚至不训练模型),然后我的GPU有100%的负载,直到我关闭选项卡。该怎么办呢。

输入数据为48x48图像。

training_data.getInputData()返回一个平面数组,其中包含每个图像的像素数据,而training_data.getLabels()返回一个包含标签数据的平面数组。

EN

回答 1

Stack Overflow用户

发布于 2019-02-21 05:15:32

model.fit()是一种异步方法。您应该使用awaitthen

例如,使用await (从ES2017开始):

代码语言:javascript
复制
  const history = await model.fit(image_tensor, correct_prediction_tensor,
    {
        batchSize: 128,
        epochs: 10,
        shuffle: true,
        callbacks: {
            onEpochEnd: (epoch, logs) => {
                // Plot the loss and accuracy values at the end of every training epoch.
                console.log(epoch, logs);
            },
            onTrainStart: console.log("Starting Training..."),
            onTrainEnd: console.log("Training Finished!"),
        }
    });

或使用then

代码语言:javascript
复制
  model.fit(image_tensor, correct_prediction_tensor,
    {
        batchSize: 128,
        epochs: 10,
        shuffle: true,
        callbacks: {
            onEpochEnd: (epoch, logs) => {
                // Plot the loss and accuracy values at the end of every training epoch.
                console.log(epoch, logs);
            },
            onTrainStart: console.log("Starting Training..."),
            onTrainEnd: console.log("Training Finished!"),
        }
    }).then(history => {
      console.log('history:', history);
    });
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54795120

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档