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

mutate() function not working as expected #139

Open
blakesanie opened this issue Aug 2, 2018 · 15 comments
Open

mutate() function not working as expected #139

blakesanie opened this issue Aug 2, 2018 · 15 comments

Comments

@blakesanie
Copy link

Heres my code:

var nn = new neataptic.architect.Random(12,20,2);
console.log(nn);
var method = neataptic.methods.mutation.FFW;
nn.mutate(method);
console.log(nn);

nn1

nn2

The two neural nets being console logged are identical - there was no mutation going on whatsoever. After doing some troubleshooting of my own (yes, the library is linked to the program), I am completely stuck. Why isn't the mutate() method working as intended on my neural network? Thanks in advance!

@AZE38
Copy link

AZE38 commented Apr 30, 2019

i have the same problem !

have you found the solution??

@rhysstubbs
Copy link

rhysstubbs commented Apr 30, 2019

I assume you realise neataptic.methods.mutation.FFW; is an array on the mutation object, so the mutate() function call wouldn't do anything as the underlying switch statement wouldn't have a corresponding case for an array, it is looking for specific values.

The options that are available for FFW are:

  mutation.ADD_NODE,
  mutation.SUB_NODE,
  mutation.ADD_CONN,
  mutation.SUB_CONN,
  mutation.MOD_WEIGHT,
  mutation.MOD_BIAS,
  mutation.MOD_ACTIVATION,
  mutation.SWAP_NODES

You should be using:

const neuralNetwork = new neataptic.architect.Random(12,20,2);
const method = neataptic.methods.mutation.FFW.ADD_NODE; // I suggest you make this stochastic
neuralNetwork.mutate(method);

TLDR;

You're passing an array of objects, the mutate function is expecting an object.

I hope this helps, I've used this library quite a bit and If you find yourself stuck again, have a look through the source; It is generally very simple throughout 👍

@AZE38
Copy link

AZE38 commented Apr 30, 2019

yes thanks, I understood what you are saying.

But when i test
"newpop[i], it's "myNetwork"

NEWPOP[i].mutate(Methods.mutation.ALL.MOD_WEIGHT)

This error it's return
Error: No (correct) mutate method given!

But when
console.log(Methods.mutation.ALL) //return ->"MOD_WEIGHT" "MOD_BIAS" etc..

So ,I think I'm using it badly ^^

@rhysstubbs
Copy link

rhysstubbs commented Apr 30, 2019

That error is only thrown if the method passed is undefined.

Also, here NEWPOP[i].mutate(Methods.mutation.ALL.MOD_WEIGHT) you're accessing an array as if it were an object. If you want to get a specific mutation method, use:

const method = Methods.mutation.MOD_WEIGHT;

I would open the mutation methods file and take a look at the structure of the object etc. it'll help you understand how to effectively use it.

@AZE38
Copy link

AZE38 commented Apr 30, 2019

hoo , yeah it seems to work !

Thanks very much.

@rhysstubbs
Copy link

@AZE38 No worries 👍

@AZE38
Copy link

AZE38 commented Apr 30, 2019

arf , weight dont change, juste no error message "-_-

i think ,im just a fu***** noob
XD

@rhysstubbs
Copy link

@AZE38 You may be better off starting a Stack Overflow post for this. But in order for anyone to help you here, we need a MCVE (https://stackoverflow.com/help/mcve) example of what it is you're doing.

@AZE38
Copy link

AZE38 commented Apr 30, 2019

i just create some network , i evaluate and selection and duplicate for newpop, and i want just change Weight of my newpop.

Like this :
->create pop of Perceptron.
->full activate with my inputs
->evaluate with "my score function"
-> selection Best and duplicate network

so i just want do network[i].mutate(Methods.mutation.MOD_WEIGHT)

But just if i create One Network like this
Network=Architect.Perceptron(50,60,40,40,40,40,32)
And i just want to mutate this network
Network.mutate(Methods.mutation.MOD_WEIGHT)

the weight dont change

i initialize like
var Neat = neataptic.Neat; var Methods = neataptic.methods; var Config = neataptic.Config; var Architect = neataptic.architect; var Network = neataptic.Network; var Node = neataptic.Node;

So i have all methods etc, but dont change weight.

@rhysstubbs
Copy link

@AZE38 Without the code you're actually using it is extremely difficult for anyone to help debug.

In those examples you just gave you are using Network.mutate(), however, you've also initialised var Network = neataptic.Network;.

You should have something along the lines of:

let population = [];

for (let i = 0; i < 100; i++) {
    population.push(new Network(2,6));
}

// ...

// Now let's mutate

let newPopulation = [];
for (let i = 0; i < population.length; i++) {
    
    let agent = population[i]; // this is our Network instance
    agent.mutate(methods.mutation.MOD_WEIGHT);
    newPopulation.push(agent);
}

// At this point, all agents should have mutated weights

@AZE38
Copy link

AZE38 commented Apr 30, 2019

yes it's exactly what i am doing.

Hoo.. May be it's the "percentRate" of the number of mutate weights wich is small , and many node dont mutate !?

i can fix the "PercentRate"?

@rhysstubbs
Copy link

rhysstubbs commented Apr 30, 2019

@AZE38 I'm sorry but I do not understand what you mean. It'd be far easier if you provide the actual code it is you're using. Are you expecting ALL weights to have changed? If that is the case, you're misunderstanding how this works. Only 1 stochastic connection will be mutated. If you want to mutate the weight for every connection, you will need to do this manually.

Here is the original source code for the MOD_WEIGHT case:

var allconnections = this.connections.concat(this.selfconns);

var connection = allconnections[Math.floor(Math.random() * allconnections.length)];
var modification = Math.random() * (method.max - method.min) + method.min;
connection.weight += modification;

As you can see, it'll only update a single connection.

@AZE38
Copy link

AZE38 commented Apr 30, 2019

" Are you expecting ALL weights to have changed?" No no , just a portion of them , but how i can fix this "ratio".

Ho, ok , when i use Network.mutate(methods.mutation.MOD_WEIGHT), i modifie just ONE weight of them !?

if i want mutate ~30% of my network's weights, i put Network.mutate in loop to repeat the network.mutate()?

@rhysstubbs
Copy link

" Are you expecting ALL weights to have changed?" No no , just a portion of them , but how i can fix this "ratio".

Ho, ok , when i use Network.mutate(methods.mutation.MOD_WEIGHT), i modifie just ONE weight of them !?

if i want mutate ~30% of my network's weights, i put Network.mutate in loop to repeat the network.mutate()?

First answer, yes, only one weight is modified.

Second answer, that wouldn't necessarily mutate 30% of your weights. You would mutate 30 times, but not necessarily 30 different weights.

@AZE38
Copy link

AZE38 commented Apr 30, 2019

ok ok ok !
I did not understand that it just changed one.

Thanks very much, it's work now ^^

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

3 participants