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

@shoki/brain

v0.0.5

Published

feedforward neural network

Downloads

4

Readme

brain

feedforward neural network

Installation

$ yarn add @shoki/brain

Usage

@shoki/brain makes it simple to set up a neural network.

Genome

A Brain is created from a Genome. A Genome represents the "physical" structure of the brain.

You can create a basic 1-1 neuron network like so:

import { createGenome, mutation, Brain } from "@shoki/brain";

const genome = createGenome();

const inputNeuronIndex = mutation.addNeuron(genome, {
	type: "input",
	// 1 <-> 1 map from input value to output value
	activation: ActivationFunctionType.CONSTANT,
	description: "input",
});

const outputNeuronIndex = mutation.addNeuron(genome, {
	type: "output",
	// 1 <-> 1 map from input value to output value
	activation: ActivationFunctionType.CONSTANT,
	description: "output",
});

// synapse weight is 1 by default
mutation.addSynapse(genome, {
	neuronIn: inputNeuronIndex,
	neuronOut: outputNeuronIndex,
	weight: 1,
});

const brain = new Brain(genome);

brain.think({
	[inputNeuronIndex]: 1,
});

brain.getNeuronValue(outputNeuronIndex); // 1

image of 1-1 network

The input value of 1 has the following journey:

  • set to input neuron (type is constant, so it isn't modified)
  • passed through synapse (weight is 1, so value is 1 * 1)
  • set to output neuron (type is constant again, so it isn't modified)

Activation functions

Activation functions allow you to manipulate a value within a neuron.

Let's see how we can make a neuron convert negative numbers to positive with the absolute activation function.

import { createGenome, mutation, Brain } from "@shoki/brain";

const genome = createGenome();

const inputNeuronIndex = mutation.addNeuron(genome, {
	type: "input",
	// 1 <-> 1 map from input value to output value
	activation: ActivationFunctionType.CONSTANT,
	description: "input",
});

const outputNeuronIndex = mutation.addNeuron(genome, {
	type: "output",
	// 1 <-> 1 map from input value to output value
	activation: ActivationFunctionType.ABSOLUTE,
	description: "output",
});

// synapse weight is 1 by default
mutation.addSynapse(genome, {
	neuronIn: inputNeuronIndex,
	neuronOut: outputNeuronIndex,
	weight: 1,
});

const brain = new Brain(genome);

brain.think({
	[inputNeuronIndex]: -1,
});

brain.getNeuronValue(outputNeuronIndex); // 1

image of 1-1 network

Here you can see how the absolute activation type turns the negative input of -1 into a positive input of 1.

Multiple inputs

One neuron can receive inputs from multiple synapses. The only aggregation function available here at the moment is sum.

You can create this simply by binding multiple addSynapse calls to the same output neuron.

image of 2-1 network

Hidden neurons

You can create hidden neurons within the network at any point.

Inputs / outputs are only determined by finding neurons which don't have any input synapses, or output synapses, respectively.

To insert a neuron within an existing synapse, you can use insertNeuron.

mutation.insertNeuron(genome, {
	synapseIndex,
	neuron: {
		description: "hidden",
		activation: ActivationFunctionType.ABSOLUTE,
	},
});

When inserting a neuron within a synapse, the right-hand synapse carries the weight from the replaced synapse, while the left-hand synapse is given a weight of 0.

hidden inserted neuron

References

  • Efficient Evolution of Neural Network Topologies

    Kenneth O. Stanley and Risto Miikkulainen

    https://nn.cs.utexas.edu/downloads/papers/stanley.cec02.pdf