Skip to content
Related Articles

Related Articles

Improve Article

Tensorflow.js tf.broadcastTo() Function

  • Last Updated : 21 Jun, 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 .broadcastTo() function is used to circulate an array to a consistent model of NumPy-style.

Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it's lowest price ever!

Note:

  • Here, the shape of the tensor is equated to the shape of the broadcast from last to the start. Where, 1’s are prefixed to the shape of the tensor as long as it has the equivalent length like the shape of the broadcast.
  • In case, input.shape[i]==shape[i], then the (i+1)-th axis is formerly consistent to the broadcast and in case, input.shape[i]==1 plus shape[i]==N, then the stated input tensor is covered N times by that axis.

Syntax :  



tf.broadcastTo(x, shape)

 

Parameters:  

  • x: It is the stated tensor input and it can be of type tf.Tensor, TypedArray, or Array.
  • shape: It is the stated shape where the input is to be broadcasted to.

Return Value: It returns the tf.Tensor object.

Example 1:  

Javascript




// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Defining tensor input elements
const y = tf.tensor1d([1, 2, 3, 4]);
  
// Calling broadcastTo() method and
// Printing output
tf.broadcastTo(y, [1, 4]).print();

Output:

Tensor
     [[1, 2, 3, 4],]

Example 2:

Javascript




// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Calling broadcastTo() method and
// Printing output
tf.broadcastTo(tf.tensor1d([3.6, 5.8, 3.7, 1.4, 9.3, 10.5]), 
                           [1, 6]).print();

Output:

Tensor
     [[3.5999999, 5.8000002, 3.7, 1.4, 9.3000002, 10.5],]

Reference: https://js.tensorflow.org/api/latest/#broadcastTo

My Personal Notes arrow_drop_up
Recommended Articles
Page :