neuralga
v1.0.17
Published
Neural Network using Genetic Algorithm
Downloads
5
Readme
Install
Before installing it you need Node.js npm install command:
$ npm install neuralga --save
What is this
Javascript neural network implementation that uses genetic algorithm to train.
Parameters(Genetic Algorithm)
- inputs: The number of inputs of the neural networks (When passing data to the inputs and outputs it must be a number between 0 and 1)
- hiddenlayers: An array containing the amount of neurons in each layer [2] is one layer with two neurons
- outputs: The number of outputs of the neural network
- population: Number of neural networks in the population
- mutationRate: Number between 0 and 1 that determines how often the mutation should be applied
- elitism: Number between 0 and 1 that determines how much of the population should survive and stay without mutations
- provenance: Number between 0 and 1 that determines how much of the population will be new neural networks (this is to add some variety)
- mutationAmount: When a mutation occurs, the mutationAmount determines how many mutations are applied
- fitnessFunction: The function that will determine the fitness of the neural network The Genetic Algorithm tries to maximise the fitness
- memory: This is a boolean that indicates whether the neural networks should have neurons selfConnected
Methods(Genetic Algorithm)
- start: Creates the initial population and set the generation to 0 (it accepts a neural network as argument to create the initial population but its optional)
- step: takes an array of objects that represents the inputs and outputs and gets the population to the next generation
- best: returns the best neural network of the population
Methods(Neural Network)
- execute: give it an array of numbers that represents the inputs and returns an array of outputs
- clearContext: clears the results of the neurons (this method is in case you activate the memory)
- toJSON: returns JSON representation of the Neural Network
Static methods
- fromJSON: returns an instance of a Neural network from JSON object
Examples
XOR
const {GeneticAlgorithm} = require("neuralga")
let data = [
{input:[0,0],output:[0]},
{input:[0,1],output:[1]},
{input:[1,0],output:[1]},
{input:[1,1],output:[0]}
]
function fitness(network){
let error = 0;
for(let i = 0;i<data.length;i++){
let salida = network.execute(data[i].input)
for(let i2 = 0;i2<salida.length;i2++){
error += Math.abs(salida[i2]-parseInt(data[i].output[i2].toFixed(20)));
}
}
return -error;
}
let ga = new GeneticAlgorithm(2,[2],1,10,0.4,0.2,0.2,4,fitness,false)
ga.start();
let bestfitness = -Infinity;
while(bestfitness !== 0){
let overall = ga.step();
bestfitness = ga.best().fitness;
console.log(`Generation: ${ga.generation} best: ${bestfitness} overall: ${overall}`);
}
let best = ga.best();
console.log(best.execute([0,0]))
console.log(best.execute([0,1]))
console.log(best.execute([1,0]))
console.log(best.execute([1,1]))
XOR without relu
const {GeneticAlgorithm,Activations} = require("neuralga")
let data = [
{input:[0,0],output:[0]},
{input:[0,1],output:[1]},
{input:[1,0],output:[1]},
{input:[1,1],output:[0]}
]
function fitness(network){
let error = 0;
for(let i = 0;i<data.length;i++){
let salida = network.execute(data[i].input)
for(let i2 = 0;i2<salida.length;i2++){
error += Math.abs(salida[i2]-parseInt(data[i].output[i2].toFixed(20)));
}
}
return -error;
}
let ga = new GeneticAlgorithm(2,[2],1,10,0.4,0.2,0.2,4,fitness,false)
delete Activations.relu //This removes the relu activation so now the Neurons cant use it to evolve
ga.start();
let bestfitness = -Infinity;
while(bestfitness !== 0){
let overall = ga.step();
bestfitness = ga.best().fitness;
console.log(`Generation: ${ga.generation} best: ${bestfitness} overall: ${overall}`);
}
let best = ga.best();
console.log(best.execute([0,0]))
console.log(best.execute([0,1]))
console.log(best.execute([1,0]))
console.log(best.execute([1,1]))
XOR adding custom activation function
const {GeneticAlgorithm,Activations} = require("neuralga")
let data = [
{input:[0,0],output:[0]},
{input:[0,1],output:[1]},
{input:[1,0],output:[1]},
{input:[1,1],output:[0]}
]
function fitness(network){
let error = 0;
for(let i = 0;i<data.length;i++){
let salida = network.execute(data[i].input)
for(let i2 = 0;i2<salida.length;i2++){
error += Math.abs(salida[i2]-parseInt(data[i].output[i2].toFixed(20)));
}
}
return -error;
}
let ga = new GeneticAlgorithm(2,[2],1,10,0.4,0.2,0.2,4,fitness,false)
/*
When adding custom activations function you cant use more than one {} if you want to save it to json for some limitation in the toJSON function
*/
Activations.binaryStep = function(x){return x<0 ? 0 : 1}
ga.start();
let bestfitness = -Infinity;
while(bestfitness !== 0){
let overall = ga.step();
bestfitness = ga.best().fitness;
console.log(`Generation: ${ga.generation} best: ${bestfitness} overall: ${overall}`);
}
let best = ga.best();
console.log(best.execute([0,0]))
console.log(best.execute([0,1]))
console.log(best.execute([1,0]))
console.log(best.execute([1,1]))
XOR save best to json using fs
const {GeneticAlgorithm} = require("neuralga")
let data = [
{input:[0,0],output:[0]},
{input:[0,1],output:[1]},
{input:[1,0],output:[1]},
{input:[1,1],output:[0]}
]
function fitness(network){
let error = 0;
for(let i = 0;i<data.length;i++){
let salida = network.execute(data[i].input)
for(let i2 = 0;i2<salida.length;i2++){
error += Math.abs(salida[i2]-parseInt(data[i].output[i2].toFixed(20)));
}
}
return -error;
}
let ga = new GeneticAlgorithm(2,[2],1,10,0.4,0.2,0.2,4,fitness,false)
ga.start();
let bestfitness = -Infinity;
while(bestfitness !== 0){
let overall = ga.step();
bestfitness = ga.best().fitness;
console.log(`Generation: ${ga.generation} best: ${bestfitness} overall: ${overall}`);
}
let best = ga.best();
console.log(best.execute([0,0]))
console.log(best.execute([0,1]))
console.log(best.execute([1,0]))
console.log(best.execute([1,1]))
const fs = require('fs');
fs.writeFileSync('network.json', JSON.stringify(best.toJSON()));
XOR get best from json using fs
const {GeneticAlgorithm,NeuralNetwork} = require("neuralga")
let data = [
{input:[0,0],output:[0]},
{input:[0,1],output:[1]},
{input:[1,0],output:[1]},
{input:[1,1],output:[0]}
]
function fitness(network){
let error = 0;
for(let i = 0;i<data.length;i++){
let salida = network.execute(data[i].input)
for(let i2 = 0;i2<salida.length;i2++){
error += Math.abs(salida[i2]-parseInt(data[i].output[i2].toFixed(20)));
}
}
return -error;
}
let ga = new GeneticAlgorithm(2,[2],1,10,0.4,0.2,0.2,4,fitness,false)
const fs = require('fs');
let rawdata = fs.readFileSync('network.json')
let parseddata = JSON.parse(rawdata)
let network = NeuralNetwork.fromJSON(parseddata);
ga.start(network);
let bestfitness = -Infinity;
while(bestfitness !== 0){
let overall = ga.step();
bestfitness = ga.best().fitness;
console.log(`Generation: ${ga.generation} best: ${bestfitness} overall: ${overall}`);
}
let best = ga.best();
console.log(best.execute([0,0]))
console.log(best.execute([0,1]))
console.log(best.execute([1,0]))
console.log(best.execute([1,1]))