CharRNN

image of book with text that says, 'you are designing for humans not machines'

Description

RNN and LSTMs (Long Short Term Memory networks) are a type of Neural Network architecture useful for working with sequential data (like characters in text or the musical notes of a song) where the order of the that sequence matters. This class allows you run a model pre-trained on a body of text to generate new text.

You can train your own models using this tutorial or use this set of pre trained models.

Quickstart

  1. // Create the character level generator with a pre trained model
  2. const rnn = ml5.charRNN('models/bolaño/', modelLoaded);
  3. // When the model is loaded
  4. function modelLoaded() {
  5. console.log('Model Loaded!');
  6. }
  7. // Generate content
  8. rnn.generate({ seed: 'the meaning of pizza is' }, (err, results) => {
  9. console.log(results);
  10. });

Usage

Initialize

  1. const charrnn = ml5.charRNN(model, ?callback);

Parameters

  • model: REQUIRED. An absolute or relative path to the charRNN model files.
  • callback: OPTIONAL. A callback to be called once the model has loaded. If no callback is provided, it will return a promise that will be resolved once the model has loaded.

Properties


.ready

Boolean value that specifies if the model has loaded.



.state

The current state of the model.



.model

The pre-trained charRNN model.



.vocabSize

The vocabulary size (or total number of possible characters).


Methods


.generate()

Generates content in a stateless manner, based on some initial text (known as a “seed”). Returns a string.

  1. charrnn.generate(options, ?callback);

📥 Inputs

  • options: REQUIRED. An object specifying the input parameters of seed, length and temperature. Default length is 20, temperature is 0.5 and seed is a random character from the model. The object should look like this

    1. {
    2. seed: 'The meaning of pizza is',
    3. length: 20,
    4. temperature: 0.5,
    5. };
  • callback: Optional. Function. A function to be called when the model has generated content. If no callback is provided, it will return a promise that will be resolved once the model has generated new content.

📤 Outputs

  • Object: Returns an object. {sample: generated,state: this.state}.


.predict()

Feed a string of characters to the model state.

  1. charrnn.predict(temperature, ?callback);

📥 Inputs

  • seed: REQUIRED. Predict the next character based on the model’s current state.
  • callback: Optional. Function. A function to be called when the model finished adding the seed. If no callback is provided, it will return a promise that will be resolved once the prediction has been generated.

📤 Outputs

  • Object: Returns an object {sample, probabilities};


.feed()

Given an image, will make objects in the image disappear

  1. charrnn.feed(seed, ?callback);

📥 Inputs

  • seed: REQUIRED. A string to feed the charRNN model state.
  • callback: Optional. Function.Optional. A function to be called when the model finished adding the seed. If no callback is provided, it will return a promise that will be resolved once seed has been fed..

📤 Outputs

  • Image: Returns an image.


.reset()

Reset the model state

  1. charrnn.reset();

📥 Inputs

  • none

📤 Outputs

  • none

Examples

p5.js

p5 web editor

plain javascript

Demo

No demos yet - contribute one today!

Tutorials

No tutorials yet - contribute one today!

Acknowledgements

Contributors:

  • Cristobal Valenzuela and Memo Atken

Credits:

  • Paper Reference | Website URL | Github Repo | Book reference | etc

Source Code