嗨,我正在尝试构建一个转换神经网络,但我无法对其进行训练。
代码如下:
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()
返回包含标签数据的平面数组。
答案 0 :(得分:0)
model.fit()
是一种异步方法。您应该使用await
或then
。
例如,使用await
(自ES2017起):
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
:
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);
});