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

mnist-javascript

v1.0.0

Published

An easy way to use the MNIST data set in javascript.

Downloads

6

Readme

MNIST Digits

This library provides easy access to the MNIST Handwritten Digits dataset to test and train neural networks in Javascript. It can work directly in browser or through node.js.

The training set can include up to 60,000 digits and the test set up to 10,000. You can also limit the digits included as well (e.g. creating training and test sets with just the numbers 1 and 2 for easier debugging).

Installation

for node.js: npm install mnist-javascript --save

for the browser: bower install mnist-javascript --save

Usage

To use, construct an instance of the MNIST class. All functionality is accessed through this class.

Constructor

// All parameters set to default values
const mnist = new MNIST();

// Specifying all parameters to custom values
const mnist = new MNIST({trainingCount: 30000, testCount: 5000, includedDigits: [1, 3, 5], batchSize: 10});

Parameters

trainingCount [optional]: number

  • The number (between 0 and 60,000) of digit samples you want in your training set, chosen randomly.
  • default = 60,000

testCount [optional]: number

  • The number (between 0 and 10,000) of digit samples you want in your test set, chosen randomly.
  • default = 10,000

includedDigits [optional]: number[]

  • The digits you want included in your sets (e.g. [1,3,5,7,9] if you just wanted odd digits to train and test with)
  • Note: Specifying a subset of digits means the number of total samples you'll have access to will decrease.
  • default = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

batchSize [optional]: number

  • The number of samples in each training batch (see batches below)
  • default = 100

Sample Structure

Each training and test sample is a basic object with two keys:

  • input : 784 grayscale pixel values (normalized)
  • output : an array of length 10 with a 1 in the spot corresponding to the label for the digit
const threeSample = {
  input: [0, 0.5, 0.3, ..., 0.8, 1, 0, 0],  // 784 values, one for each pixel
  output: [0, 0, 0, 1, 0, 0, 0, 0, 0]  // since this sample is a three, it has a 1 at that index
};

Training Functionality

Properties

trainSamples: Sample[] All the training samples in random order (ignores batching)

Methods

nextBatch() - returns a Sample[] of the next batch, of length batchSize or null if no batches remain. hasBatch() - returns true if there are still batches to iterate over, false otherwise. resetBatches() - resets the internal batch index so calling nextBatch() will start over with the first training batch.

Testing Functionality

testSamples: Sample[] All the test samples in random order

Methods

nextTest() - returns the next test as a Sample or null if no tests remain. hasTest() - returns true if there are still tests to iterate over, false otherwise. resetTests() - resets the internal test index so calling nextTest() will start over with the first Sample.

Example Use Cases

  1. Construct a MNIST instance to test and train your NN using batches
const network = new NeuralNetwork(); // example of a NN class to train and test
const mnist = new mnist();

// Train network by running through all batches
while (mnist.hasBatch()) {
  let batch = mnist.nextBatch();
  network.train(batch);
}

// Test the network
network.test(mnist.testSamples);
  1. Construct a MNIST instance to test and train your NN without batches
const network = new NeuralNetwork(); // example of a NN class to train and test
const mnist = new mnist({trainingCount: 55000});

// Train using all training samples at once
network.train(mnist.trainingSamples);

// Test the network
network.test(mnist.testSamples);
  1. Construct a MNIST instance and iterate over test samples
const network = new NeuralNetwork(); // example of a NN class to train and test
const mnist = new mnist({trainingCount: 55000});

// Train using all training samples at once
network.train(mnist.trainingSamples);

// Test the network
while (mnist.hasTest()) {
	network.testSingleSample(mnist.nextTest());
}

Helper Functionality

Get Specific Digits

You can grab a specified number of samples of a particular digit (randomly chosen from the training set)

const mnist = new mnist();
const digit = 5;
const count = 2;

const samples = mnist.getDigit(digit, count);

Parameters

digit: number The digit you want samples of (between 0 and 9)

count [optional]: number The number of samples you want (defaults to 1)

return: An array of count samples.

Draw A Digit (Browser Only)

You can have MNIST render a sample digit to an HTML canvas if you provide the canvas context.

const mnist = new mnist();
const ctx = window.getElementById("myCanvas").getContext('2d'); // grab a canvas element
const sampleThree = mnist.getDigit(3)[0]; // grab a random sample 3 to render

mnist.drawDigit(sampleThree.input, ctx);

Parameters

pixels: number[] An array of 784 pixel values to render (the input of a sample)

ctx: CanvasRenderingContext2D A canvas rendering context, can get using getContext(2d)

x [optional]: number X offset into the canvas for the image

y [optional]: number Y offset into the canvas for the image

License

MIT