Skip to content

Commit

Permalink
new dispose, cleanup and comments
Browse files Browse the repository at this point in the history
  • Loading branch information
shiffman committed Apr 13, 2020
1 parent 66fb881 commit 071ee19
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 25 deletions.
4 changes: 0 additions & 4 deletions examples/p5js/NeuralNetwork/NeuroEvolution/bird.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@ class Bird {
}
}

dispose() {
this.brain.neuralNetwork.model.dispose();
}

show() {
stroke(255);
fill(255, 100);
Expand Down
6 changes: 5 additions & 1 deletion examples/p5js/NeuralNetwork/NeuroEvolution/ga.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@
function nextGeneration() {
console.log('next generation');
calculateFitness();

// Create a new population
for (let i = 0; i < TOTAL; i++) {
birds[i] = reproduce();
}

// Release all the memory
for (let i = 0; i < TOTAL; i++) {
savedBirds[i].dispose();
savedBirds[i].brain.dispose();
}
savedBirds = [];
}
Expand Down
36 changes: 22 additions & 14 deletions examples/p5js/NeuralNetwork/NeuroEvolution_testing/sketch.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ function setup() {
}
const nnA = ml5.neuralNetwork(options);
const results1 = nnA.classifySync([0.2, 0.1, 0.5]);
console.log('----- Classification A ------');
console.log(results1[0]);

nnA.mutate(0.1);
const results2 = nnA.classifySync([0.2, 0.1, 0.5]);
console.log('----- Default Mutated Classification A ------');
console.log(results2[0]);

// Mutating a neural network
Expand All @@ -19,21 +21,27 @@ function setup() {
}
nnA.mutate(0.1, customMutate);
const results3 = nnA.classifySync([0.2, 0.1, 0.5]);
console.log('----- Custom Mutated Classification A ------');
console.log(results3[0]);

// const nnCopy = nnA.copy();
// const results4 = nnCopy.classifySync([0.2, 0.1, 0.5]);
// console.log(results4[0]);
const nnCopy = nnA.copy();
const results4 = nnCopy.classifySync([0.2, 0.1, 0.5]);
console.log('----- Identical Copy Classification A ------');
console.log(results4[0]);

// const nnB = ml5.neuralNetwork(options);
// const results5 = nnB.classifySync([0.2, 0.1, 0.5]);
// console.log(results5[0]);
const nnB = ml5.neuralNetwork(options);
const results5 = nnB.classifySync([0.2, 0.1, 0.5]);
console.log('----- Classification B ------');
console.log(results5[0]);

// const child = nnA.crossover(nnB);
// const resultsA = nnA.classifySync([0.2, 0.1, 0.5]);
// const resultsB = nnB.classifySync([0.2, 0.1, 0.5]);
// const childResults = child.classifySync([0.2, 0.1, 0.5]);
// console.log(resultsA[0]);
// console.log(resultsB[0]);
// console.log(childResults[0]);
const child = nnA.crossover(nnB);
const resultsA = nnA.classifySync([0.2, 0.1, 0.5]);
const resultsB = nnB.classifySync([0.2, 0.1, 0.5]);
const childResults = child.classifySync([0.2, 0.1, 0.5]);
console.log('----- Classification A ------');
console.log(resultsA[0]);
console.log('----- Classification B ------');
console.log(resultsB[0]);
console.log('----- Crossover AB classification ------');
console.log(childResults[0]);
}
18 changes: 17 additions & 1 deletion src/NeuralNetwork/NeuralNetwork.js
Original file line number Diff line number Diff line change
Expand Up @@ -272,8 +272,20 @@ class NeuralNetwork {
return this.model;
}

/**
* dispose and release the memory for the model
*/
dispose() {
this.model.dispose();
}

// NeuroEvolution Functions

/**
* mutate the weights of a model
* @param {*} rate
* @param {*} mutateFunction
*/
mutate(rate, mutateFunction) {
tf.tidy(() => {
const weights = this.model.getWeights();
Expand All @@ -284,7 +296,7 @@ class NeuralNetwork {
// TODO: Evaluate if this should be sync or not
const values = tensor.dataSync().slice();
for (let j = 0; j < values.length; j+=1) {
if (Math.random() < rate) {
if (Math.random() < rate || 0.1) {
if (mutateFunction) {
values[j] = mutateFunction(values[j]);
} else {
Expand All @@ -299,6 +311,10 @@ class NeuralNetwork {
});
}

/**
* create a new neural network with crossover
* @param {*} other
*/
crossover(other) {
return tf.tidy(() => {
const weightsA = this.model.getWeights();
Expand Down
40 changes: 35 additions & 5 deletions src/NeuralNetwork/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ class DiyNeuralNetwork {
this.save = this.save.bind(this);
this.load = this.load.bind(this);

// release model
this.dispose = this.dispose.bind(this);

// neuroevolution
this.mutate = this.mutate.bind(this);

Expand Down Expand Up @@ -110,6 +113,9 @@ class DiyNeuralNetwork {
}
}

/**
* createLayersNoTraining
*/
createLayersNoTraining() {
// Makes some sample data
// TODO: Account for regression
Expand All @@ -123,6 +129,9 @@ class DiyNeuralNetwork {
this.addDefaultLayers(this.options.task, this.neuralNetworkData.meta);
}

/**
* copy
*/
copy() {
const nnCopy = new DiyNeuralNetwork(this.options);
return tf.tidy(() => {
Expand Down Expand Up @@ -778,7 +787,7 @@ class DiyNeuralNetwork {
*/

/**
* predict
* synchronous predict
* @param {*} _input
*/
predictSync(_input) {
Expand All @@ -804,7 +813,7 @@ class DiyNeuralNetwork {
}

/**
* predict
* synchronous classify
* @param {*} _input
*/
classifySync(_input) {
Expand All @@ -830,7 +839,7 @@ class DiyNeuralNetwork {
}

/**
* predict
* synchronous predict internal
* @param {*} _input
* @param {*} _cb
*/
Expand Down Expand Up @@ -950,7 +959,7 @@ class DiyNeuralNetwork {
}

/**
* classify
* synchronous classify internal
* @param {*} _input
* @param {*} _cb
*/
Expand Down Expand Up @@ -1156,11 +1165,32 @@ class DiyNeuralNetwork {
});
}

// NeuroEvolution functions
/**
* dispose and release memory for a model
*/
dispose() {
this.neuralNetwork.dispose();
}

/**
* ////////////////////////////////////////////////////////////
* New methods for Neuro Evolution
* ////////////////////////////////////////////////////////////
*/

/**
* mutate the weights of a model
* @param {*} rate
* @param {*} mutateFunction
*/
mutate(rate, mutateFunction) {
this.neuralNetwork.mutate(rate, mutateFunction);
}

/**
* create a new neural network with crossover
* @param {*} other
*/
crossover(other) {
const nnCopy = this.copy();
nnCopy.neuralNetwork.crossover(other.neuralNetwork);
Expand Down

0 comments on commit 071ee19

Please sign in to comment.