Coder Social home page Coder Social logo

train-stream's Introduction

brain.js train-stream

A node train stream for brain.js

Usage

Streams are a very powerful tool in node for massive data spread across processes and are provided via the brain.js api in the following way:

Use with brain.js' NeuralNetwork class

import { NeuralNetwork } from 'brain.js';
import { TrainStream } from 'train-stream';

const net = new NeuralNetwork();
const trainStream = new TrainStream({
  neuralNetwork: net,
  floodCallback: () => {
    readInputs(trainStream, data);
  },
  doneTrainingCallback: (stats) => {
    // network is done training!  What next?
  },
});

// kick it off
readInputs(trainStream, data);

function readInputs(stream, data) {
  for (let i = 0; i < data.length; i++) {
    stream.write(data[i]);
  }
  // let it know we've reached the end of the inputs
  stream.endInputs();
}

Use with brain.js' RNN/LSTM class

import { recurrent, utilities } from 'brain.js';
import { TrainStream } from 'train-stream';
const { LSTM } = recurrent;

const neuralNetwork = new LSTM({
  hiddenLayers: [10],
  dataFormatter: new utilities.DataFormatter(), // You'll need to setup a dataformatter
});
const trainStream = new TrainStream({
  neuralNetwork,
  floodCallback: () => {
    trainStream.write(myData);
    trainStream.endInputs();
  },
  doneTrainingCallback: (stats) => {
    // network is done training!  What next?
  },
});

An example of using train stream can be found in examples/stream-example.ts

API

The network now has a WriteStream. You can train the network by using pipe() to send the training data to the network.

Initialization

To train the network using a stream you must first initialize the stream new TrainStream({ neuralNetwork, floodCallback, doneTrainingCallback }) which takes the following options:

  • neuralNetwork - the instance of neural network from brain.js used with the stream. Examples are NeuralNetwork, LSTMTimeStep, or LSTM.
  • floodCallback - the callback function to re-populate the stream. This gets called on every training iteration.
  • doneTrainingCallback(info: { error: number, iterations: number}) - the callback function to execute when the network is done training. The info param will contain a hash of information about how the training went.

Transform

Use a Transform to coerce the data into the correct format. You might also use a Transform stream to normalize your data on the fly.

train-stream's People

Contributors

robertleeplummerjr avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

train-stream's Issues

train-stream does not support the Recurrent class

I tried to configure a Recurrent network to use train-stream, but ran into the following error:

one-brain-1  | node:internal/event_target:1037
one-brain-1  |   process.nextTick(() => { throw err; });
one-brain-1  |                            ^
one-brain-1  | 
one-brain-1  | TypeError [Error]: neuralNetwork.updateTrainingOptions is not a function
one-brain-1  |     at new TrainStream (/one/node_modules/train-stream/dist/index.js:41:23)
one-brain-1  |     at MessagePort.<anonymous> (file:///one/src/encoder.js:57:29)
one-brain-1  |     at [nodejs.internal.kHybridDispatch] (node:internal/event_target:762:20)
one-brain-1  |     at exports.emitMessage (node:internal/per_context/messageport:23:28)
one-brain-1  | Emitted 'error' event on Worker instance at:
one-brain-1  |     at [kOnErrorMessage] (node:internal/worker:326:10)
one-brain-1  |     at [kOnMessage] (node:internal/worker:337:37)
one-brain-1  |     at MessagePort.<anonymous> (node:internal/worker:232:57)
one-brain-1  |     at [nodejs.internal.kHybridDispatch] (node:internal/event_target:762:20)
one-brain-1  |     at exports.emitMessage (node:internal/per_context/messageport:23:28)
one-brain-1  | 
one-brain-1  | Node.js v20.3.1

I was unsure if this was a known issue. The docs say nothing about supporting the Recurrent class, nor do I even think the class is ready, in vanilla Brain.js? I'm not sure, but I've struggled to make one of these networks work in any environment.

Network trained with train-stream cannot be exported to JSON

When I instantiate a GRU/LSTM network, I can use the .toJSON() method without issue. When I train with vanilla Brain.js, I can also export without issue.

When I train with train-stream, the network cannot be exported. There are two errors I keep running into.

This one happens while running in the main thread:

one-brain-1  | RangeError: Maximum call stack size exceeded
one-brain-1  |     at GRU.toJSON (/one/node_modules/brain.js/dist/index.js:7632:11)
one-brain-1  |     at JSON.stringify (<anonymous>)
one-brain-1  |     at file:///one/src/encoder.js:79:46

And this one happens in a worker:

one-brain-1   | node:internal/event_target:1037
one-brain-1   |   process.nextTick(() => { throw err; });
one-brain-1   |                            ^
one-brain-1   | 
one-brain-1   | RangeError [Error]: Maximum call stack size exceeded
one-brain-1   |     at GRU.toJSON (/one/node_modules/brain.js/dist/index.js:7632:11)
one-brain-1   |     at JSON.stringify (<anonymous>)
one-brain-1   |     at MessagePort.<anonymous> (file:///one/src/encoder.js:36:50)
one-brain-1   |     at [nodejs.internal.kHybridDispatch] (node:internal/event_target:762:20)
one-brain-1   |     at exports.emitMessage (node:internal/per_context/messageport:23:28)
one-brain-1   | Emitted 'error' event on Worker instance at:
one-brain-1   |     at [kOnErrorMessage] (node:internal/worker:326:10)
one-brain-1   |     at [kOnMessage] (node:internal/worker:337:37)
one-brain-1   |     at MessagePort.<anonymous> (node:internal/worker:232:57)
one-brain-1   |     at [nodejs.internal.kHybridDispatch] (node:internal/event_target:762:20)
one-brain-1   |     at exports.emitMessage (node:internal/per_context/messageport:23:28)
one-brain-1   | 
one-brain-1   | Node.js v20.3.1

Node 18 fails with the same error. I'm not sure how to proceed at this point.

On a different note, will this run in the browser? And if not, are there any plans to bring the functionality?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.