Tensorflow.js tf.tile() Function

TensorFlow.js is a library for machine learning in JavaScript. It helps developers to develop ML models in JavaScript, and use ML directly in the browser or in Node.js.
The tf.tile() function is used to create a Tensor by repeating the number of times given by reps.
Syntax:
tf.tile(x, reps)
Note: This function creates a new tensor by replicating the input reps times. For example, tiling [1, 2, 3, 4] by [3] produces [1, 2, 3, 4,1, 2, 3, 4,1, 2, 3, 4].
Parameter: This function accepts the following two parameters.
- x: The tensor passed to the tile. It can be tf.Tensor, TypedArray, or Array.
- reps: This parameter defines the number of replications per dimension.
Return: It returns tf.Tensor object.
Example 1:
Javascript
// Importing the tensorflow.js library import * as tf from "@tensorflow/tfjs" // Creating a tensor1d const a = tf.tensor1d([1, 2, 3, 4]); // Creating the tensor with the help of tile() const x = a.tile([2]); // Printing the tensor x.print(); |
Output:
Tensor [1, 2, 3, 4, 1, 2, 3, 4]
Example 2:
Javascript
// Importing the tensorflow.js library import * as tf from "@tensorflow/tfjs" // Creating a tensor2d const a = tf.tensor2d([1, 2, 3, 4, 5, 6],[2, 3]); // Creating the tensor with the help of tile() const x = a.tile([1,2]); // Printing the tensor x.print(); |
Output:
Tensor
[[1, 2, 3, 1, 2, 3],
[4, 5, 6, 4, 5, 6]]
Example 3:
Javascript
// Importing the tensorflow.js library import * as tf from "@tensorflow/tfjs" // Creating a tensor2d const a = tf.tensor2d([1, 2, 3, 4, 5, 6],[2, 3]); // Creating the tensor with the help of tile() const x = a.tile([3,2]); // Printing the tensor x.print(); |
Output:
Tensor
[[1, 2, 3, 1, 2, 3],
[4, 5, 6, 4, 5, 6],
[1, 2, 3, 1, 2, 3],
[4, 5, 6, 4, 5, 6],
[1, 2, 3, 1, 2, 3],
[4, 5, 6, 4, 5, 6]]
Reference: https://js.tensorflow.org/api/latest/#tile
Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, zambiatek Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!



