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
Comments
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. |
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: |
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: |
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;
} |
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. |
I'm really close here: https://jsfiddle.net/robertleeplummerjr/jn0n4xn4/7/ |
This is a very rough proof of concept: https://jsfiddle.net/robertleeplummerjr/jn0n4xn4/11/ |
Brain.js, what's on your mind? |
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. |
Unfortunately I'm too bad of a coder to help you with your code 😞 |
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 |
This reply is what I'm following: http://stackoverflow.com/a/27675589 |
Closer proof of concept: https://jsfiddle.net/jn0n4xn4/12/ |
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. |
Updated here: https://jsfiddle.net/robertleeplummerjr/jn0n4xn4/13/ |
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. |
|
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? |
As for naming this function, rather than mull, how about one of these: unRun, trySolveInputs, nur, runBackward, or retro? |
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 |
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. |
Also, very very very non-specific unit tests (still really not sure how to test it): |
I had a lot of fun on this. Please feel free to keep commenting open, closing to focus on priorities. |
@robertleeplummerjr Just to let you know: |
will this ever make it into master? |
If there was sufficient desire. |
I did end up putting it here though: https://github.com/harthur-org/brain.retro.js fyi |
nice! thanks @robertleeplummerjr |
I'd love to find out how you are using it! |
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:
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. |
Can we continue this here: BrainJS/brain.retro.js#1? |
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? |
You'll need either a natural language configuration of brain.js, like https://github.com/axa-group/nlp.js, or a LSTM network. |
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:
Pseudo-output:
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.
The text was updated successfully, but these errors were encountered: