ml5js / ml5-library

Friendly machine learning for the web! 🤖
https://ml5js.org
Other
6.45k stars 905 forks source link

word2vec API: direct vector math #236

Open shiffman opened 5 years ago

shiffman commented 5 years ago

In teaching word2vec in the A2Z class, I noticed two things about the API that would be good to discuss.

Arguments

While it's nice to have the option of adding, subtracting, averaging, etc. an array of vectors, the added square bracket syntax is confusing. It would be nice to overload the math functions to also parse multiple arguments as an array. I know this is probably tricky given we also allow for an optional argument as the number of elements to return. Maybe we can just allow for the case of math between two words not requiring an array.

// Valid
word2vec.add('apple', 'pear');  
word2vec.add('apple', 'pear', 10);  
word2vec.add(['apple', 'pear', 'orange']); 
word2vec.add(['apple', 'pear', 'orange'], 10);

// Invalid
word2vec.add('apple', 'pear', 'orange');
word2vec.add('apple', ['pear', 'orange']);

Direct vector math

Without access to the numeric vectors themselves (see: #215), it's problematic to do certain operations. Take the following "analogy" example:

let difference = await word2vec.subtract('kitten', 'cat');
let result = await word2vec.add(difference[0].word, 'dog');

This could lead to strange results since we are not actually adding the difference vector to dog, rather we are adding a vector associated with the word closest to that difference vector to dog. If the math functions returned both the closest word as well as the raw vector itself, we could say:

let difference = await word2vec.subtract('kitten', 'cat');
let result = await word2vec.add(difference.vector, 'dog');

This would also make it possible to "interpolate" between two words by calculating the vector then moving along it more slowly. To make this possible we would need to:

  1. Add a vector property with the raw numbers (converted presumably from their corresponding tensor) to the result of all math operations like add(), average(), etc.
  2. Overload all of the math operations to accept either a word or a raw array of numbers.

Perhaps both of these suggestions add too much complexity / maintenance?

saptab commented 4 years ago

any update on this? @cvalenzuela

I would like to load the vector provided a word.

in python it can be done like for a model 'word2vec'

man_vec = word2vec["man"]

What is the equivalent in ml5?