npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

neuralga

v1.0.17

Published

Neural Network using Genetic Algorithm

Downloads

5

Readme

Creator

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]))