Atanas Bobev
Atanas Bobev

Reputation: 71

Brain js prediction

I'm trying to create ML with Brain.js that takes as input a number and outputs its count of significant digits.

Examples:

Input: 234 Output:3

Input: 2413 Output: 4

Input: 1 Output 1

<script src='https://cdn.rawgit.com/harthur-org/brain.js/aabe8cc2/browser.js'></script>

<script>


const network = new brain.NeuralNetwork();

//Only simple test data provided for now

network.train([
    { input: { Num: 2 }, output: { SigFigs: 1 } },
    { input: { Num: 12 }, output: { SigFigs: 2 } },
    { input: { Num: 432 }, output: { SigFigs: 3 } },
    { input: { Num: 1358 }, output: { SigFigs: 4 } },
    { input: { Num: 98 }, output: { SigFigs: 2 } },
    { input: { Num: 9123 }, output: { SigFigs: 4} },
    { input: { Num: 14 }, output: { SigFigs: 2} },
    { input: { Num: 763 }, output: { SigFigs: 3} },
], {
  log: true,
  iterations: 1e6,
  errorThresh: 0.00001
});

const result = network.run({ Num: 43 });

console.log(result); //SigFigs: 0.9999999396415676
//Expected output: 2

</script>

The result is completely no sense for me. I expect something like 2 and in worse case some other number. What I am doing wrong and what should I do to get the expected output?

Upvotes: 1

Views: 1356

Answers (3)

ecki
ecki

Reputation: 790

You must set the output key as the number of sigfigs, then the value as 1.

So an example is:

network.train([
    { input: { Num: 2 }, output: { "1": 1 } },
    { input: { Num: 12 }, output: { "2": 1 } },
    { input: { Num: 432 }, output: { "3": 1 } }
])

When train runs, for the first item in the array, it sees there is an output for "1" but not for "2" or "3", so it sets "2" and "3" as zero.

In this case it keeps your range between 0 and 1, and allows you to treat 0 as false and 1 as true as if they are boolean values.

I haven't actually checked this with brain js as I'm writing this on my iPhone, but if your neutral net has trained correctly, you should get an output that looks like:

const result = network.run({ Num: 43 });
console.log(result);
=> "1": 0.0165, "2": 0.9726, "3": 0.0613

Upvotes: 3

Jaroslav Tavgen
Jaroslav Tavgen

Reputation: 318

Output should be between 0 and 1.

Upvotes: 1

Yosef Tukachinsky
Yosef Tukachinsky

Reputation: 5895

You not need any ML for it.. just use the function

function digits(x) {return Math.floor(Math.log10(x) + 1) }

Upvotes: 1

Related Questions