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

Generate possible results from training #5

Closed
stephanbogner opened this issue Mar 12, 2016 · 34 comments
Closed

Generate possible results from training #5

stephanbogner opened this issue Mar 12, 2016 · 34 comments

Comments

@stephanbogner
Copy link

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
Copy link
Contributor

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
Copy link
Contributor

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:

  • Probably use a more js-ly defined query, similar to mongo for example, so that it is all json and free of extra parsers
  • Since this would just be in the context of js specifically, you could loose the quotes for brevity
  • Since you eloquently ordered your query in english, I'll try and do the same in js.
  • run seems like the wrong method name, since we are (hypothetically at this point) reversing the procedure, from input -> output, to output -> input, may I suggest a verb that describes it in english? A quick (seemingly ironic) reverse word lookup reveals "mull" may be a good find (http://www.onelook.com/?loc=rescb&refclue=think&w=mull)

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

@robertleeplummerjr
Copy link
Contributor

robertleeplummerjr commented May 22, 2016

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
Copy link
Contributor

robertleeplummerjr commented May 22, 2016

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
Copy link
Contributor

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

@robertleeplummerjr
Copy link
Contributor

robertleeplummerjr commented May 22, 2016

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
Copy link
Contributor

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

@robertleeplummerjr
Copy link
Contributor

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

@robertleeplummerjr
Copy link
Contributor

Brain.js, what's on your mind?

@robertleeplummerjr
Copy link
Contributor

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
Copy link
Author

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
Copy link
Contributor

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
Copy link
Contributor

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

@robertleeplummerjr
Copy link
Contributor

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

@robertleeplummerjr
Copy link
Contributor

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
Copy link
Contributor

@robertleeplummerjr
Copy link
Contributor

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
Copy link
Contributor

robertleeplummerjr commented Jun 18, 2016

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
Copy link
Contributor

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

It should be noted that:

  • "Running in reverse" means that it estimates what the input are, it does not actually know what the specific inputs were. It runs backward by estimating inputs (each layer node weights are added together, and that value is divided by the un-sigmoided sum, or logit, an average which is then sigmoided back into the network as the next output for the previous layer's node). The network itself does run in reverse, literally, but that doesn't mean you'll get perfect results, you'll get sort of fuzzy results. Be that as it may, they are still pretty fantastic to see.
  • If you did like me, and run a bunch of methods, and then run them back through the network, you won't get the results that you thought you would. If you carefully look at what was output from the initial run, you'll notice the results are simply fuzzy.
  • You could solve the inputs by simply building another ai 😄 to estimate for them. Though that may not be as fun, it would probably be more accurate.
  • You could probably solve the inputs by running the network and tracking values for different combinations, and then run it backwards informing each node how to handle the estimated value, but I think this is more broot force, would require tons of cpu, and would ultimately try and tell you what you may already know about the network, and ultimately would be relying on a solution outside of the network, which really isn't running it backwards.
  • With great power comes great responsibility.

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

@robertleeplummerjr
Copy link
Contributor

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

@robertleeplummerjr
Copy link
Contributor

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
Copy link
Contributor

robertleeplummerjr commented Jun 19, 2016

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
Copy link
Contributor

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
Copy link
Contributor

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

@stephanbogner
Copy link
Author

@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
Copy link

will this ever make it into master?

@robertleeplummerjr
Copy link
Contributor

If there was sufficient desire.

@robertleeplummerjr
Copy link
Contributor

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

@jrobinson01
Copy link

nice! thanks @robertleeplummerjr

@robertleeplummerjr
Copy link
Contributor

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

@jrobinson01
Copy link

jrobinson01 commented Jan 25, 2017

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
Copy link
Contributor

Can we continue this here: BrainJS/brain.retro.js#1?

@Esger
Copy link

Esger commented Mar 15, 2020

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
Copy link
Contributor

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

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

4 participants