word2vec()

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.

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!

Example

// Create a new word2vec method
const wordVectors = ml5.word2vec("data/wordvecs.json", modelLoaded);

// When the model is loaded
function modelLoaded() {
  console.log("Model Loaded!");
}

// Find the closest word to 'rainbow'
wordVectors.nearest("rainbow", function(err, results) {
  console.log(results);
});

Here is a complete example.

Constructor

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 value that specifies if the model has loaded.

.model

The model being used.

Methods

.add(inputs, ?max, ?callback)

Add a series of word vectors. Returns the closest vector of that sum.

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.

.subtract(inputs, ?max, ?callback)

Subtract a series of vectors. Returns the closest vector of that sum.

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.

.average(inputs, ?max, ?callback)

Average a series of vectors. Returns the closest vector of that average.

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.

.nearest(input, ?max, ?callback)

Find the nearest vector. Returns max array of values.

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.

.getRandomWord(?callback)

Find a random vector in the loaded model. Returns a string.

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.

Source

/src/Word2vec/