Open In App

Tensorflow.js tf.broadcastTo() Function

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.



Note:

Syntax :  



tf.broadcastTo(x, shape)

 

Parameters:  

Return Value: It returns the tf.Tensor object.

Example 1:  




// 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:




// 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

Article Tags :