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

multilayer-perceptron-js

v1.0.0

Published

A small multilayer perceptron library for educational purposes

Downloads

4

Readme

MultilayerPerceptronJS

MultiLayerPerceptron

Refer to my blog post.

This is a small neural network "library" that's intended for educational purposes. I wanted to develop something that is easily understood and very readable, so this library is far from optimized or efficient.

Installing

You can install this via npm

npm install multilayer-perceptron-js

Creating a neural network

const { MultiLayerPerceptron, ActivationFunction } = require('multilayer-perceptron-js');

let sigmoid = new ActivationFunction(
  x => 1 / (1 + Math.exp(-x)), // sigmoid
  y => y * (1 - y) // derivative of sigmoid
);

let mlp = new MultiLayerPerceptron({inputDimension: 2})
  .addLayer({nodes: 2, activation: sigmoid})
  .addLayer({nodes: 2, activation: sigmoid})
  .addLayer({nodes: 1, activation: sigmoid})
  .randomizeWeights();

Training a neural network


mlp.train({
  trainData: dataset.inputs,
  trainLabels: dataset.targets,
  validationData: validationDataset.inputs,
  validationLabels: validationDataset.targets,
  numEpochs: numberOfEpochs,
  learningRate: learningRate,
  verbose: true
});

Where dataset.inputs, dataset.targets, validationDataset.inputs, and validationDataset.targets are arrays. If you were solving the XOR problem, dataset might look like this (such that the indexes of each array line up):

let dataset = {
  inputs: [
    [0, 0],
    [0, 1],
    [1, 0],
    [1, 1]
  ],
  targets: [
    [0],
    [1],
    [1],
    [0]
  ]
};

You'll see something like this while training if verbose is true:

Epoch 10; Error 1.9860974914165456
...
Epoch 100; Error 0.76609707552872275
...
Epoch 1000; Error 0.166096867598113085
...
Epoch 5000; Error 0.036096035894226
...
Epoch 10000; Error 0.03609582796927307
...

Making predictions

To make a prediction, just call predict on the MultiLayerPerceptron object. You'll receive the predicted guess and the state of the neural network. If you have a model that solved the XOR problem, making predictions would look like this:

console.log(mlp.predict([0, 0]).prediction); // [0.02039202706589195]
console.log(mlp.predict([0, 1]).prediction); // [0.9848467111547554]
console.log(mlp.predict([1, 0]).prediction); // [0.9850631024542238]
console.log(mlp.predict([1, 1]).prediction); // [0.013544196415469074]

Acknowledgements

Much of the implementation is inspired from The Coding Train's videos on neural networks, and the 3Blue1Brown videos on neural networks helped me understand what I was doing a lot better.