Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NaN returns when first training entry is longer then second #56

Open
markg85 opened this issue Jul 28, 2017 · 1 comment
Open

NaN returns when first training entry is longer then second #56

markg85 opened this issue Jul 28, 2017 · 1 comment

Comments

@markg85
Copy link

markg85 commented Jul 28, 2017

Hi,

My first steps in deep learning don't go so well. I keep getting NaN.
That turned out to be a "lucky" coincidence.

See this full example:

const Mind = require('node-mind');

var asciiToBin = (function () {
    var pad = '00000000';

    return function (str) {
        return str.replace(/./g, function (c) {
            var bin = c.charCodeAt(0).toString(2);
            return pad.substring(bin.length) + bin;
        });
    };
}());

var binToAscii = function (bin) {
    return bin.replace(/[01]{8}/g, function (v) {
        return String.fromCharCode(parseInt(v, 2));
    });
};

function binArray(data) {
  return Array.from(asciiToBin(data)).map(Number);
}

const mind = new Mind()
.learn([
  { input: binArray("the2"), output: [1] },
  { input: binArray("the"), output: [1] }
]);

const result = mind.predict(binArray("the"));

console.log(result);

That returns NaN.
Now if i merely swap the learn inputs around to this:

  { input: binArray("the"), output: [1] },
  { input: binArray("the2"), output: [1] }

Then it magically starts working :)

This is not the only ones where it produces NaN. It does so in the following cases as well (only changing the inputs, not the predict):

  { input: binArray("the "), output: [1] },
  { input: binArray("the "), output: [1] }

Here it seems like the predict has to be at least as long as the shortest input.

  { input: binArray("one"), output: [1] },
  { input: binArray("two"), output: [1] },
  { input: binArray("a"), output: [1] },

Also gives NaN. Puttin "a" on top makes it work (in this case).

This might be a bug, and one that seems rather easy to get? Or i'm doing something else extremely wrong, in which case i'd also love to hear that!

p.s. if one knows a more efficient way to convert a string to a binary array. Or if there is a more elegant way of thing this in this library, please do tell :)

Cheers,
Mark

@markg85
Copy link
Author

markg85 commented Jul 30, 2017

Hmm, what i said above is still very much true, but it's true for any case where n+1 is shorter then n. N being the entry you attempt to learn.

That basically means that you can only learn entries of equal length or where the length is bigger than the last entry you learned.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant