Giter Site home page Giter Site logo

Comments (34)

robertleeplummerjr avatar robertleeplummerjr commented on April 29, 2024 1

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

from brain.js.

robertleeplummerjr avatar robertleeplummerjr commented on April 29, 2024

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.

from brain.js.

robertleeplummerjr avatar robertleeplummerjr commented on April 29, 2024

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 } });

from brain.js.

robertleeplummerjr avatar robertleeplummerjr commented on April 29, 2024

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));.

from brain.js.

robertleeplummerjr avatar robertleeplummerjr commented on April 29, 2024

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;
}

from brain.js.

robertleeplummerjr avatar robertleeplummerjr commented on April 29, 2024

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

from brain.js.

robertleeplummerjr avatar robertleeplummerjr commented on April 29, 2024

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.

from brain.js.

robertleeplummerjr avatar robertleeplummerjr commented on April 29, 2024

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

from brain.js.

robertleeplummerjr avatar robertleeplummerjr commented on April 29, 2024

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

from brain.js.

robertleeplummerjr avatar robertleeplummerjr commented on April 29, 2024

Brain.js, what's on your mind?

from brain.js.

robertleeplummerjr avatar robertleeplummerjr commented on April 29, 2024

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.

from brain.js.

stephanbogner avatar stephanbogner commented on April 29, 2024

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.

from brain.js.

robertleeplummerjr avatar robertleeplummerjr commented on April 29, 2024

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

from brain.js.

robertleeplummerjr avatar robertleeplummerjr commented on April 29, 2024

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

from brain.js.

robertleeplummerjr avatar robertleeplummerjr commented on April 29, 2024

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

from brain.js.

robertleeplummerjr avatar robertleeplummerjr commented on April 29, 2024

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.

from brain.js.

robertleeplummerjr avatar robertleeplummerjr commented on April 29, 2024

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

from brain.js.

robertleeplummerjr avatar robertleeplummerjr commented on April 29, 2024

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.

from brain.js.

robertleeplummerjr avatar robertleeplummerjr commented on April 29, 2024

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.

from brain.js.

robertleeplummerjr avatar robertleeplummerjr commented on April 29, 2024

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?

from brain.js.

robertleeplummerjr avatar robertleeplummerjr commented on April 29, 2024

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

from brain.js.

robertleeplummerjr avatar robertleeplummerjr commented on April 29, 2024

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

from brain.js.

robertleeplummerjr avatar robertleeplummerjr commented on April 29, 2024

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.

from brain.js.

robertleeplummerjr avatar robertleeplummerjr commented on April 29, 2024

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

from brain.js.

stephanbogner avatar stephanbogner commented on April 29, 2024

@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!

from brain.js.

jrobinson01 avatar jrobinson01 commented on April 29, 2024

will this ever make it into master?

from brain.js.

robertleeplummerjr avatar robertleeplummerjr commented on April 29, 2024

If there was sufficient desire.

from brain.js.

robertleeplummerjr avatar robertleeplummerjr commented on April 29, 2024

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

from brain.js.

jrobinson01 avatar jrobinson01 commented on April 29, 2024

nice! thanks @robertleeplummerjr

from brain.js.

robertleeplummerjr avatar robertleeplummerjr commented on April 29, 2024

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

from brain.js.

jrobinson01 avatar jrobinson01 commented on April 29, 2024

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.

from brain.js.

robertleeplummerjr avatar robertleeplummerjr commented on April 29, 2024

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

from brain.js.

Esger avatar Esger commented on April 29, 2024

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.

from brain.js.

robertleeplummerjr avatar robertleeplummerjr commented on April 29, 2024

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

from brain.js.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.