Google News
logo
ml5.js Interview Questions
word2vec has been disabled : We've intentionally disabled the word2vec function after recognizing it has the potential to produce harmful outputs while using the pre-trained model files included in our examples. We'll consider reenabling the word2vec function along with changes to address these issues in a future release of ml5.js
 
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.
 
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.
The ml5 utilities are handy functions that make your life easier when working with data, images, etc.
 
Usage : 

Methods :
.flipImage() : Flips an image or video input horizontally and returns the flipped image. Handy for mirroring an image or video.
const flippedImage = ml5.flipImage(input);
 
Inputs :
 
* input : Optional. A HTMLVideoElement | p5 video element | HTMLImageElement.

Outputs :
 
* Object : Returns a flipped image.

Example :
 
Assuming you're using ml5 with p5.js :
<html>
  <meta charset="UTF-8" />
  <title>flipImage</title>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.9.0/p5.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.9.0/addons/p5.dom.min.js"></script>
  <script src="http://localhost:8080/ml5.js" type="text/javascript"></script>
  <body>
    <script>
      let video;
      function setup() {
        createCanvas(640, 480);
        video = createCapture(VIDEO);
        video.size(640, 480);
        video.hide();
      }

      function draw() {
        const flippedVideo = ml5.flipImage(video);
        image(flippedVideo, 0, 0, width, height);
      }
    </script>
  </body>
</html>

Sources : Ml5.js, W3C, more..