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.
// 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).
Â
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.
Â
// 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
Â