Word2Vec
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
// 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', (err, results) => {
console.log(results);
});
Usage
Initialize
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.
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.
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.
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.
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.
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.
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