CodingTrain / Toy-Neural-Network-JS

Neural Network JavaScript library for Coding Train tutorials
MIT License
425 stars 242 forks source link

Adding crossover function #100

Open enginefeeder101 opened 6 years ago

enginefeeder101 commented 6 years ago

This commit adds crossover functions to the Matrix and NeuralNetwork class. It defaults to a uniform crossover (ratio = 0.5), but this is customizable (third parameter).

You can use it as such:

let parentA = new NeuralNetwork(2, 4, 1);
let parentB = new NeuralNetwork(2, 4, 1);
let child = NeuralNetwork.crossover(parentA, parentB);
shiffman commented 6 years ago

Awesome job! The use of map() is very clever!

Note for examples I prefer to use something generic like parentA or parent1 rather than "mom" / "dad".

I will hold off on merging but will do so during a video when I implement the algorithm! I think this is simple enough that I don't need to do my own video tutorial implementing crossover. (I cover it in the GA series anyway.)

enginefeeder101 commented 6 years ago

I added a commit which checks if a NeuralNetwork and/or Matrix are of the same breed before applying the crossover algorithm. This prevents crossing-over between objects with different sizes.