Open In App

Tensorflow.js tf.layers.multiply() Function

Improve
Improve
Like Article
Like
Save
Share
Report

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

The tf.layers.multiply() function is used to perform element-wise multiplication of an array of inputs.

Syntax:

tf.layers.multiply()

Parameters:

  • inputShape: If this parameter is defined, it will create another input layer to insert before this layer.
  • batchInputShape: If this parameter is defined, it will create another input layer to insert before this layer.
  • batchSize: Used to construct batchInputShape, if not already specified.
  • dtype: Specifies the data-type for this layer. Defaults value of this parameter is ‘float32’.
  • name: Specifies name for this layer.
  • updatable: Specifies whether the weights of this layer can be updated by fit or not.
  • trainable: Specifies whether the weights of this layer are updatable by fit.
  • weights: Specifies the initial weight values of the layer.
  • inputDType: ‘float32’ or ‘int32’ or ‘bool’ or ‘complex64’ or ‘string’.

Return value: Single tensor of same type as input tensors.

Example 1:

Javascript




// Import the library
import * as tf from "@tensorflow/tfjs"
 
const input1 = tf.input({shape: [3, 2]})
const input2 = tf.input({shape: [3, 2]})
const input3 = tf.input({shape: [3, 2]})
 
// Create a multiply layer
const multiplyLayer = tf.layers.multiply()
 
// Multiple array of inputs by applying multiplyLayer
const product = multiplyLayer.apply([input1, input2, input3])
 
// Print the shape of output tensor
console.log(JSON.stringify(product.shape))


Output:

[null,3,2]

Note: Here null denotes the undetermined batch size.

Example 2:

Javascript




// Import the library
import * as tf from "@tensorflow/tfjs"
 
// Inputs
const input1 = tf.tensor([-2, 1, 0, 5]);
const input2 = tf.tensor([3, 2, 3, 2]);
const input3 = tf.tensor([4, 3, 1, 2]);
 
// Create multiply layer
const multiplyLayer = tf.layers.multiply();
 
// Multiply inputs
const product = multiplyLayer.apply(
    [input1, input2, input3]);
 
// Print product
console.log(product);


Output:

Tensor
    [-24, 6, 0, 20]

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



Last Updated : 19 Nov, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads