Tensorflow.js tf.tile() Function
Last Updated :
12 May, 2021
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
import * as tf from "@tensorflow/tfjs"
const a = tf.tensor1d([1, 2, 3, 4]);
const x = a.tile([2]);
x.print();
|
Output:
Tensor
[1, 2, 3, 4, 1, 2, 3, 4]
Example 2:
Javascript
import * as tf from "@tensorflow/tfjs"
const a = tf.tensor2d([1, 2, 3, 4, 5, 6],[2, 3]);
const x = a.tile([1,2]);
x.print();
|
Output:
Tensor
[[1, 2, 3, 1, 2, 3],
[4, 5, 6, 4, 5, 6]]
Example 3:
Javascript
import * as tf from "@tensorflow/tfjs"
const a = tf.tensor2d([1, 2, 3, 4, 5, 6],[2, 3]);
const x = a.tile([3,2]);
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
Share your thoughts in the comments
Please Login to comment...