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