BrainJS / brain.js

🤖 GPU accelerated Neural networks in JavaScript for Browsers and Node.js
https://brain.js.org
MIT License
14.4k stars 1.07k forks source link

Generate possible results from training #5

Closed stephanbogner closed 8 years ago

stephanbogner commented 8 years ago

Is there a way to train the brain and generate possible results instead of letting the brain analyse a certain input?

For example: "Give me 4 colors which match 'orange' with at least 80% accuracy".

As pseudo-code:

var net = new NeuralNetwork(); //Create neural network

net.train([{input: {r:1, g:0.65, b:0},  output: {orange: 1}}, //This is orange
           {input: {r:0, g:0.54, b:0},  output: {green: 1}}, //This is green
           {input: {r:0.6, g:1, b:0.5}, output: {green: 1}}, //This is also green
           {input: {r:0.67, g:0, b:1},  output: {purple: 1}}]); //This is purple

var output = net.run({"orange": ">0.8", "results": 4}); //return 4 colors which match 'orange' with at least 80% accuracy

Pseudo-output:

[{r:1,    g:0.65, b:0},
 {r:0.98, g:0.55, b:0},
 {r:1,    g:0.55, b:0.2},
 {r:0.85, g:0.55, b:0}]

A real world example would be: "Tell me the first thing which comes to your mind when I say Internet". You might say something like Tim Berners-Lee or Wikipedia.

Is something like that possible with BrainJS? Note: I also posted this question on StackOverflow.

robertleeplummerjr commented 8 years ago

Very cool idea! I will first state one of my favorite quotes: "anything is possible, even if it isn't". I will invest some time this weekend thinking about this, and see if I (or if anyone else wants to chime in) have a solution for you.

robertleeplummerjr commented 8 years ago

Snap! Just thought about what you are actually trying to do and essentially you are running the network backwards. Not only is this possible, I think it should be easy. Will see if I can get a POC later today.

Some notes:

Thoughts on: var output = net.mull({ results: 4, orange: { gte: 0.8 } });

robertleeplummerjr commented 8 years ago

Posting research as found. To find a result, one must apparently un-sigmoid or flatten the curved output. In the source, we have a very elegant sigmoid of: (1 / (1 + Math.exp(-sum)));, of which the opposite is apparently (as found when playing around with console) -(Math.log(sum) / Math.log(Math.E));.

robertleeplummerjr commented 8 years ago

The es6 version of the no lib neural function is:

function run(input, net) {
  let i = 1
    , layer
    , output
    , id
    , iid
    , node
    , sum
    ;

  for (; i < net.layers.length; i++) {
    layer = net.layers[i];
    output = {};

    for (id in layer) {
      node = layer[id];
      sum = node.bias;

      for (iid in node.weights) {
        sum += node.weights[iid] * input[iid];
      }
      output[id] = (1 / (1 + Math.exp(-sum)));
    }
    input = output;
  }

  return output;
}

If my calculations are correct (and there is a good reason they are not), the inverse would look something like:

function mull(output, net) {
  let i = net.layers.length
    , layer
    , input
    , id
    , iid
    , node
    , sum
    ;

  while (i-- > 1) {
    layer = net.layers[i];
    input = {};

    for (id in layer) {
      node = layer[id];
      sum = node.bias;

      for (iid in node.weights) {
        sum -= node.weights[iid] / output[iid];
      }
      input[id] = -(Math.log(sum) / Math.log(Math.E));
    }
    output = input;
  }

  return input;
}
robertleeplummerjr commented 8 years ago

If a neural network could be described as a mathematical funnel, what you have devised is a mathematical explosion.

robertleeplummerjr commented 8 years ago

It doesn't stop there, if you can receive an input to get an output and receive an output to get an input and if you had an intermediate function put them back and forth, you would have neural reflection.

robertleeplummerjr commented 8 years ago

I'm really close here: https://jsfiddle.net/robertleeplummerjr/jn0n4xn4/7/

robertleeplummerjr commented 8 years ago

This is a very rough proof of concept: https://jsfiddle.net/robertleeplummerjr/jn0n4xn4/11/

robertleeplummerjr commented 8 years ago

Brain.js, what's on your mind?

robertleeplummerjr commented 8 years ago

I did get further with this when tinkering last night, but it occurred to me that without knowing what the inputs were, it gets a bit hairy.

stephanbogner commented 8 years ago

Unfortunately I'm too bad of a coder to help you with your code 😞 But I am happy that you are curious with what I wanted to achieve! If I can help in any way, let me know.

robertleeplummerjr commented 8 years ago

The more I look up about this, the more interesting it gets. I found this: https://www.coursera.org/course/neuralnets where he does this very thing with his network. And the question asked on stack exchange: http://stackoverflow.com/questions/27673883/using-a-learned-artificial-neural-network-to-solve-inputs

robertleeplummerjr commented 8 years ago

This reply is what I'm following: http://stackoverflow.com/a/27675589

robertleeplummerjr commented 8 years ago

Closer proof of concept: https://jsfiddle.net/jn0n4xn4/12/

robertleeplummerjr commented 8 years ago

I believe I'm onto something. I now am debugging the values that come from each step of both run and mull (I really don't like that name, lol, even though it may be accurate) and comparing them from both run and mull. I noticed that the values from mull were huge! It hit me that (and I don't know why this never hit me prior) that the values that are sent through the network are sigmoid-ed. You see, at first I was affectively un-sigmoid-ing the values (logit) and feeding that math back through, I logit the output to obtain the input, but it never occurred to me that the value that needs to be reversed back to the previous node are all sigmoid-ed. Onwards and upwards. I will have an updated fiddle soon.

robertleeplummerjr commented 8 years ago

Updated here: https://jsfiddle.net/robertleeplummerjr/jn0n4xn4/13/

robertleeplummerjr commented 8 years ago

OK, I believe I successfully have it: https://jsfiddle.net/robertleeplummerjr/jn0n4xn4/14/ as close as was mentioned in the answer mentioned in stack exchange. Next step is to see if we can push it beyond what it currently is.

robertleeplummerjr commented 8 years ago

Analyzed this further with the new logging method, which makes it easier to understand what goes out and comes in, and the sigmoid in doesn't work. Was getting confused by inputs and outputs, and didn't have enough coffee, my theory was right.

robertleeplummerjr commented 8 years ago

Here is the beta of the solved neural network running in reverse: https://jsfiddle.net/robertleeplummerjr/jn0n4xn4/17/

It should be noted that:

Do you feel like this would be of value somewhere in brain.js as a utility perhaps?

robertleeplummerjr commented 8 years ago

As for naming this function, rather than mull, how about one of these: unRun, trySolveInputs, nur, runBackward, or retro?

robertleeplummerjr commented 8 years ago

I went ahead and added it into a branch retro, and renamed it to the same: https://github.com/harthur-org/brain.js/blob/retro/lib/retro.js

robertleeplummerjr commented 8 years ago

And as for the ability to query (because I couldn't help myself): https://github.com/harthur-org/brain.js/blob/retro/lib/query.js#L3

use like this:

var brain = require('brain');
var net = new brain.NeuralNetwork(); //Create neural network
net.train([
  {input: {r:1, g:0.65, b:0},  output: {orange: 1}}, //This is orange
  {input: {r:0, g:0.54, b:0},  output: {green: 1}}, //This is green
  {input: {r:0.6, g:1, b:0.5}, output: {green: 1}}, //This is also green
  {input: {r:0.67, g:0, b:1},  output: {purple: 1}} //This is purple
]);
var inputs = brain.query({ count: 4, orange: { gte: 0.8 } }, net);

Have fun!

If there is interest enough, I'll merge it into master.

robertleeplummerjr commented 8 years ago

Also, very very very non-specific unit tests (still really not sure how to test it): https://github.com/harthur-org/brain.js/blob/retro/test/unit/retro.js https://github.com/harthur-org/brain.js/blob/retro/test/unit/query.js

robertleeplummerjr commented 8 years ago

I had a lot of fun on this. Please feel free to keep commenting open, closing to focus on priorities.

stephanbogner commented 8 years ago

@robertleeplummerjr Just to let you know: I am putting a small demo together with something I had in mind with it. I am busy these days, so it might take a while, but I'll keep you posted. Love you code, thank you very much. I am glad you had fun, too!

jrobinson01 commented 7 years ago

will this ever make it into master?

robertleeplummerjr commented 7 years ago

If there was sufficient desire.

robertleeplummerjr commented 7 years ago

I did end up putting it here though: https://github.com/harthur-org/brain.retro.js fyi

jrobinson01 commented 7 years ago

nice! thanks @robertleeplummerjr

robertleeplummerjr commented 7 years ago

I'd love to find out how you are using it!

jrobinson01 commented 7 years ago

I'm trying to use it in an image recognition demo. I'm using image data from my web cam. What I'm trying to do is, after I've trained and run the network, take the output and run it through retro, then display in image of what the network "sees" as the input.

I'm having an issue with the retro function though. I think the problem may be with how I'm formatting my inputs and output. An example of my training data:

[ {input: [0.3, 0.2, 1.0 ... 0.1], output: {isMe:1.0}} ]

The input arrays are large arrays of numbers. I'm just getting an empty object out of retro(). I'm guessing something funky is up with the use of Object.keys() in the retro code. I'm hoping I'll figure it out this evening.

robertleeplummerjr commented 7 years ago

Can we continue this here: https://github.com/harthur-org/brain.retro.js/issues/1?

Esger commented 4 years ago

I was hoping this question would lead to an answer to my own question: Can the trained network output the -let's say the 8- most probable outputs? I'm training the recurrent.LSTM network with a significant chunk of text which I convert to an array of {input:[character], output: [subsequent character]} What I want to build is a keyboard with 8 keys, that (almost) always shows the most probable 8 letters that you'll need next. Like this but that works with arrays of most probable next keys. I'm trying to do better with brainJs.

robertleeplummerjr commented 4 years ago

You'll need either a natural language configuration of brain.js, like https://github.com/axa-group/nlp.js, or a LSTM network.