ml5js损失与信心NaN

时间:2020-05-24 04:48:08

标签: javascript

我从ml5js入门页面上摘录了以下代码。我将其粘贴到我的HTML文件中,阅读了文档,但是找不到我的结果恢复信心的原因:NaN 然后,我查看了“同时训练”功能并记录了损失,发现该值也是NaN。

我觉得我缺少一些非常简单的东西。

非常感谢任何帮助。

更新

我做了一些小的更改,例如将脚本分离到自己的文件中,并尝试简化下面显示的内容。如果有人遇到相同的问题!

<!-- EXAMPLE THAT WASN'T WORKING -->
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script src="https://unpkg.com/ml5@0.4.3/dist/ml5.min.js"></script>
    <title>ML5JS</title>
</head>
<body>
    <script>
        window.addEventListener('DOMContentLoaded', function(){
            // Step 1: load data or create some data 
            const data = [
                {r: 100, g: 0, b: 0, color: "red-ish"},
                {r: 101, g: 0, b: 0, color: "red-ish"},
                {r: 102, g: 0, b: 0, color: "red-ish"},
                {r: 103, g: 0, b: 0, color: "red-ish"},
                {r: 104, g: 0, b: 0, color: "red-ish"},
                {r: 105, g: 0, b: 0, color: "red-ish"},
                {r: 106, g: 0, b: 0, color: "red-ish"},
                {r: 107, g: 0, b: 0, color: "red-ish"},
                {r: 108, g: 0, b: 0, color: "red-ish"},
                {r: 109, g: 0, b: 0, color: "red-ish"},
                {r: 0, g: 0, b: 103, color: "blue-ish"},
                {r: 0, g: 0, b: 104, color: "blue-ish"},
                {r: 0, g: 0, b: 105, color: "blue-ish"},
                {r: 0, g: 0, b: 106, color: "blue-ish"},
                {r: 0, g: 0, b: 107, color: "blue-ish"},
                {r: 0, g: 0, b: 108, color: "blue-ish"},
                {r: 0, g: 0, b: 109, color: "blue-ish"},
                {r: 0, g: 0, b: 110, color: "blue-ish"},
                {r: 0, g: 0, b: 111, color: "blue-ish"},
                {r: 0, g: 0, b: 112, color: "blue-ish"}
            ];

            // Step 2: set your neural network options
            const options = {
                task: 'classification',
                debug: true
            }

            // Step 3: initialize your neural network
            const nn = ml5.neuralNetwork(options);

            // Step 4: add data to the neural network
            data.forEach(item => {
            const inputs = {
                r: item.r, 
                g: item.g, 
                b: item.b
            };
            const output = {
                color: item.color
            };

            nn.addData(inputs, output);
            });

            // Step 5: normalize your data;
            nn.normalizeData();

            // Step 6: train your neural network
            const trainingOptions = {
                epochs: 100
            }
            nn.train(trainingOptions, finishedTraining);

            // Step 7: use the trained model
            function finishedTraining(){
                classify();
            }

        // Step 8: make a classification
            function classify(){
                const input = {
                    r: 255, 
                    g: 0, 
                    b: 0
                }
                    nn.classify(input, handleResults);
            }

            // Step 9: define a function to handle the results of your classification
            function handleResults(error, result) {
                if(error){
                console.error(error);
                return;
                }
                console.log(result); // 0: {label: "red-ish", confidence: NaN}, 1: {label: "blue-ish", confidence: NaN}
            }
        })

    </script>
</body>
</html>
<!-- Script that worked! -->

const colors = [
    {
        red: 0,
        green: 0,
        blue: 255,
        classification: 'blue-ish'
    },
    {
        red: 0,
        green: 0,
        blue: 254,
        classification: 'blue-ish'

    },
    {
        red: 0,
        green: 0,
        blue: 253,
        classification: 'blue-ish'

    },
    {
        red: 255,
        green: 0,
        blue: 0,
        classification: 'red-ish'
    },
    {
        red: 254,
        green: 0,
        blue: 0,
        classification: 'red-ish'

    },
    {
        red: 253,
        green: 0,
        blue: 0,
        classification: 'red-ish'

    },
    {
        red: 0,
        green: 253,
        blue: 0,
        classification: 'green-ish'
    },
    {
        red: 0,
        green: 254,
        blue: 0,
        classification: 'green-ish'

    },
    {
        red: 0,
        green: 255,
        blue: 0,
        classification: 'green-ish'

    }
]

let model;

let completeTraining = false;


setup()

function setup(){
    let options = {
        inputs: ['red', 'green','blue'],
        outputs: ['classification'],
        task: 'classification',
        debug: 'true'
    };

    model = ml5.neuralNetwork(options);

    add()
}


function add(){
    if(!colors) return;
    colors.forEach(({ classification , ...colors})=>{
        let inputs = {...colors}
        let output = {classification}

        model.addData(inputs, output);
    })

    //setTimeout(function(){
        train()
    //},2000)
}

function train(){
    model.normalizeData()
    model.train({epochs: 150}, whileTraining, finished)
}

function whileTraining(epochs,loss){
    console.log(loss)
}

function finished(){
    completeTraining = true;
    let tester = {red: 0, green: 190, blue: 200}
    model.classify(tester, results)
}

function results(error, data){
    if(error) return;
    console.log(data)
}

1 个答案:

答案 0 :(得分:0)

这是一个错误,当所有输入的参数为 0 时会发生这种情况,目前可以通过将参数的至少一个实例更改为一个非常小的值来解决,在您的情况下,green:0 可以更改为 1 in one案件。希望这个bug能尽快修复。

https://github.com/ml5js/ml5-website/issues/154