Tensorflow.js is an open-source library developed by Google for running machine learning models and deep learning neural networks in the browser or node environment.
The tf.clone() function is used to create a copy of a tensor. The tf.clone() function creates a new tensor of the same shape and value of another tensor.
Syntax:
tf.clone( x )
Parameters:
- x: It is the tensor which we want to clone. Its value can be tensor, Array, TypedArray type.
Returns: It returns a Tensor Object.
Example 1: In this example, we are creating a copy of the tensor x and storing it into y.
Javascript
// Importing the tensorflow.js library import * as tf from "@tensorflow/tfjs" // Creating a tensor object const x = tf.tensor([6, 1]); // Cloning the tensor x and // storing it into y const y = tf.clone(x); // Printing the tensor y.print(); |
Output :
[6, 1]
Example 2: In this example, we cloned the tensor x using x.clone() instead of tf.clone(x).
Javascript
import * as tf from "@tensorflow/tfjs" // Creating a tensor object const x = tf.tensor([12, 2]); // Cloning the tensor x const y = x.clone(); // Printing the tensor y.print(); |
Output :
[12, 2]
Example 3: Have a look at the example below.
Javascript
// Creating a tensor x const x = tf.tensor([2, 2]); // Creating a clone of tensor x // using clone() function const y = x.clone(); // Creating a tensor a and // storing the same value as x const a = tf.tensor([2, 2]); // Copying the value of a into // b using assignment operator const b = a; console.log(x == y); // false console.log(x === y); // false console.log(a == x); // false console.log(a == b); // true console.log(a === b); // true |
Output :
false false false true true
Reference: https://js.tensorflow.org/api/latest/#clone