Getting Started with TensorFlow.JS

Introduction

TensorFlow.JS is a Machine Learning library for JavaScript. It can run both on the browser (client-side) and on the server-side using Node.JS.

Watch the following video (duration: 8 minutes) to learn more about TensorFlow.JS and some of its applications:

Pre-trained Models: Object Detection

Pre-trained Models: Word Embeddings (Word2Vec)

Since computers can only work with numbers, computer scientists working in the field of Natural Language Understanding (NLU) and Processing (NLP) are using several techniques to convert words or sentences into numbers.

  • One Hot Encoding
  • Encoding with Unique Numbers
  • Word Embeddings

Word embedding is one of the principal concepts in Natural Language Processing, where words from a vocabulary are converted to vectors of real numbers. These vector numbers have the amazing capacity to reflect the relationship between these words.

Pre-trained Models: Universal Sentence Encoder

The Universal Sentence Encoder (USE) model encodes text into embeddings (high-dimensional vectors) that can be used for text classification, semantic similarity, clustering and other natural language tasks.

To read more about embeddings: https://www.tensorflow.org/tutorials/text/word_embeddings

To find out the semantic similarity of two sentences, we encode each sentence using the USE model and then compare the two vectors produced by the model by calculating their inner dot product.

TensorFlow provides two methods that we can use to calculate dot product of the two vectors:

  1. The dot method:
tf.matMul( sentenceVector1.arraySync(), sentenceVector2.arraySync(), false, true );
  1. The matMul method for calculating the dot product of two vectors. We need to transpose the second vector (second argument: true) in order for this operation to work.
tf.matMul( sentenceVector1, sentenceVector2, false, true );

You can get an idea of how the tf.matMul operates on these two vectors by checking this Matrix Multiplication Calculator. The second matrix (red one) is the one transposed (rotated vertically) by setting the second argument of matMul to true.

MatMulTranspose

Semantic Similarity

Image by Google licensed under CC BY 4.0.

Text Classification

Image by Google licensed under CC BY 4.0.

Pre-trained Models: Toxicity Classifier

The Toxicity Classifier model can be used to detect offensive language in a text content.

You can read more about the Toxicity Classifier in the following Medium post:

Text classification using TensorFlow.js: An example of detecting offensive language in browser

Here is a live demo of a text editor that can detect offensive language using the Toxicity Classifier. Don’t forget to check the Enable Toxicity Check checkbox to enable this feature. Here is the source code for the demo.

Resources