Word2Vec

illustration of an equation with egg plus cat equals kitten

Description

Word2vec is a group of related models that are used to produce word embeddings. This method allows you to perform vector operations on a given set of input vectors.

You can use the word models we provide, trained on a corpus of english words (watch out for bias data!), or you can train your own vector models following this tutorial. More of this soon!

Quickstart

  1. // Create a new word2vec method
  2. const wordVectors = ml5.word2vec('data/wordvecs.json', modelLoaded);
  3. // When the model is loaded
  4. function modelLoaded() {
  5. console.log('Model Loaded!');
  6. }
  7. // Find the closest word to 'rainbow'
  8. wordVectors.nearest('rainbow', (err, results) => {
  9. console.log(results);
  10. });

Usage

Initialize

  1. const word2vec = ml5.Word2Vec(model, ?callback);

Parameters

  • model: A string to the path of the JSON model.
  • callback: Optional. A callback function that is 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. Boolean value that specifies if the model has loaded.



.model

Object. The model being used.


Methods


.add()

Add a series of word vectors.

  1. word2vec.add(inputs, ?max, ?callback);

📥 Inputs

  • inputs: An array of strings containing the inputs to be added
  • max: Optional. The maximum results to return. Defaults to 1.
  • callback: Optional. A callback function that is called once the model has made the operation. If no callback is provided, it will return a promise that will be resolved once operation is completed.

📤 Outputs

  • Object: Returns the closest vector of that sum.


.subtract()

Subtract a series of vectors.

  1. word2vec.subtract(inputs, ?max, ?callback);

📥 Inputs

  • inputs: An array of strings containing the inputs to be subtracted.
  • max: Optional. The maximum results to return. Defaults to 1.
  • callback: Optional. A callback function that is called once the model has made the operation. If no callback is provided, it will return a promise that will be resolved once operation is completed.

📤 Outputs

  • Object: Returns the closest vector of that sum.


.average()

Average a series of vectors.

  1. word2vec.average(inputs, ?max, ?callback);

📥 Inputs

  • inputs: An array of strings containing the inputs to be averaged.
  • max: Optional. The maximum results to return. Defaults to 1.
  • callback: Optional. A callback function that is called once the model has made the operation. If no callback is provided, it will return a promise that will be resolved once operation is completed.

📤 Outputs

  • Object: Returns the closest vector of that average.


.average()

Average a series of vectors.

  1. word2vec.average(inputs, ?max, ?callback);

📥 Inputs

  • inputs: An array of strings containing the inputs to be averaged.
  • max: Optional. The maximum results to return. Defaults to 1.
  • callback: Optional. A callback function that is called once the model has made the operation. If no callback is provided, it will return a promise that will be resolved once operation is completed.

📤 Outputs

  • Object: Returns the closest vector of that average.


.nearest()

Find the nearest vector. Returns max array of values.

  1. word2vec.nearest(inputs, ?max, ?callback);

📥 Inputs

  • input: The input vector string.
  • max: Optional. The maximum results to return. Defaults to 10.
  • callback: Optional. A callback function that is called once the model has made the operation. If no callback is provided, it will return a promise that will be resolved once operation is completed.

📤 Outputs

  • Object: Returns max array of values.


.getRandomWord()

Find a random vector in the loaded model.

  1. word2vec.getRandomWord(?callback);

📥 Inputs

  • callback: Optional. A callback function that is called once the model has made the operation. If no callback is provided, it will return a promise that will be resolved once operation is completed.

📤 Outputs

  • String: Returns a string.

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, Dan Shiffman, and Jenna Xu

Credits:

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

Source Code