Skip to content
Related Articles

Related Articles

Improve Article

Tensorflow.js tf.layers.embedding() Function

  • Last Updated : 26 May, 2021

Tensorflow.js is an open-source library that is developed by Google for running machine learning models as well as deep learning neural networks in the browser or node environment.

The tf.layers.embedding() function is used to map positive integers into dense vectors of fixed size.

Syntax:

tf.layers.embedding(args)

Parameters: This function accepts the args as a parameter which can have the following properties:

  • inputDim: It is used to specify the vocabulary size.
  • outputDim: It is used to specify the dimension of the dense embedding.
  • embeddingsInitializer: It is used to specify the initializer of the embeddings matrix.
  • embeddingsRegularizer: It is used to specify which regularizer function is applied to embeddings matrix. 
  • activityRegularizer: It is used to specify which regularizer function is applied to activation. 
  • embeddingsConstraint: It is used to specify which constraint function is applied to the embeddings matrix. 
  • maskZero: It is used to check whether the input value 0 is a special padding value.
  • inputLength: It is used to specify the length of input sequences.
  • inputShape: It is used to create an input layer to insert before this layer.
  • batchInputShape: It is used to create an input layer to insert before this layer.  
  • batchSize: It is used to construct the batchInputShape if the inputShape is specified and batchInputShape is not specified.
  • dtype: It is used to denote the data-type for this layer.  
  • name: It is used to denote the name for this layer.
  • trainable: It is used to indicate whether the weights of this layer are updatable by fit or not.  
  • weights: It is used to denote the initial weight values of the layer.
  • inputDType: It is just for the legacy support and not to be use for new code.

Return value: It returns the Embedding.



Example 1:

Javascript




// Import library
import * as tf from "@tensorflow/tfjs"
    
// Create embedding layer
const embeddingLayer = tf.layers.embedding({
   inputDim: 10,
   outputDim: 3,
  inputLength: 2
});
  
const input = tf.ones([2, 2]);
  
// Apply embedding to input 
const output = embeddingLayer.apply(input);
    
// Print the output
console.log(output)

Output:

Tensor
    [[[0.0179072, 0.0069226, 0.0202718],
      [0.0179072, 0.0069226, 0.0202718]],

     [[0.0179072, 0.0069226, 0.0202718],
      [0.0179072, 0.0069226, 0.0202718]]]

Example 2:

Javascript




// Import the library
import * as tf from "@tensorflow/tfjs"
    
// Create embedding layer
const embeddingLayer = tf.layers.embedding({
   inputDim: 100,
   outputDim: 4,
  inputLength: 3
});
    
const input = tf.ones([3, 3]);
  
// Apply embedding to input
const output = embeddingLayer.apply(input);
    
// Print the output
console.log(output)

Output:

Tensor
    [[[0.0443502, -0.0342815, 0.0228792, 0.0198386],
      [0.0443502, -0.0342815, 0.0228792, 0.0198386],
      [0.0443502, -0.0342815, 0.0228792, 0.0198386]],

     [[0.0443502, -0.0342815, 0.0228792, 0.0198386],
      [0.0443502, -0.0342815, 0.0228792, 0.0198386],
      [0.0443502, -0.0342815, 0.0228792, 0.0198386]],

     [[0.0443502, -0.0342815, 0.0228792, 0.0198386],
      [0.0443502, -0.0342815, 0.0228792, 0.0198386],
      [0.0443502, -0.0342815, 0.0228792, 0.0198386]]]

Reference: https://js.tensorflow.org/api/latest/#layers.embedding




My Personal Notes arrow_drop_up
Recommended Articles
Page :