输入大小为1的训练会在随后的预测中导致NaN [英] Training with an input size of 1 causes NaN in subsequent predictions

查看:101
本文介绍了输入大小为1的训练会在随后的预测中导致NaN的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在MNIST教程此处用于识别手写字符.

I'm following the MNIST tutorial here for recognizing handwritten characters.

我能够毫无问题地加载和识别手写数字,但是现在我想在新图像上再次训练模型(特别是一次).

I'm able to load and recognize handwritten digits without issue, but now I want to train the model again on new images (specifically one at a time).

由于某种原因,当我选择等于1的训练量时,我所有的预测都变为NaN.

For some reason, when I choose a training size equal to 1, all my predictions become NaN.

如果我选择一个值> = 2,就可以正常工作.

If I pick a value >=2, it works fine.

火车功能:

async function train(model, data)
{
    const TRAIN_DATA_SIZE = 1; // WHEN THIS IS 1, CAUSES PREDICT TO OUTPUT NaN

    const [trainXs, trainYs] = tf.tidy(() =>
    {
        const d = data.nextTrainBatch(TRAIN_DATA_SIZE);
        return [
            d.xs.reshape([TRAIN_DATA_SIZE, 28, 28, 1]),
            d.labels
        ];
    });

    console.log(trainXs.dataSync());
    console.log(trainYs.dataSync());

    return model.fit(trainXs, trainYs);
}

nextTrainBatch的代码是此处.

用于预测的示例输出:

currentTensor = tf.tensor2d(inputs, [1, PIXELSSQUARED]);

const output = model.predict(currentTensor.reshape([1, 28, 28, 1]));
const prediction_value = Array.from(output.argMax(1).dataSync());
console.log(output.dataSync());

训练大小为2或更大时:

When training size is 2 or greater:

Float32Array(10) [3.308702423154841e-9, 5.89648436744028e-8, 0.00005333929220796563, 0.8063259720802307, 7.401082784824764e-13, 1.1464327087651327e-7, 6.5924318955190575e-12, 0.1936144232749939, 0.000004253268798493082, 0.000001676815713835822]

训练大小为1时:

Float32Array(10) [NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN]

推荐答案

该模型已达到数值不稳定性.使用SGD之类的优化程序可能会有所帮助.但是,批量大小为1实际上不是一个好主意,因为模型可能会在最佳值附近波动

The model is reaching a numerical instability. Use an optimizer such as SGD might help. However, using a batch size of 1 is practically not a good idea as the model might oscillate around optimum values

我希望用户在模型做出预测后选择正确的值,例如进行预测,选择正确的输出,根据此信息进行重新训练

I want the user to select the correct value after the model has made it's prediction e.g. Make Prediction, Select Correct Output, Retrain based on this information

如果要进一步训练,则需要具有与模型inputShape匹配的数据.这样就可以收集预测值和用户选择的结果,并可以用来进一步训练模型

if you want to train further, you would need to have the data that matches the model inputShape. So the value predicted and the result chosen by the user will be collected and It can be used to train the model further

// the model has been trained
y = model.predict(x) // predict

假设用户将验证结果y.进一步训练

Suppose that the user will validate the result y. To train further

model.fit(x, y)

周期继续

这篇关于输入大小为1的训练会在随后的预测中导致NaN的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆