Tensorflow.js tf.clone() Function
Last Updated :
19 Oct, 2021
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
import * as tf from "@tensorflow/tfjs"
const x = tf.tensor([6, 1]);
const y = tf.clone(x);
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"
const x = tf.tensor([12, 2]);
const y = x.clone();
y.print();
|
Output :
[12, 2]
Example 3: Have a look at the example below.
Javascript
const x = tf.tensor([2, 2]);
const y = x.clone();
const a = tf.tensor([2, 2]);
const b = a;
console.log(x == y);
console.log(x === y);
console.log(a == x);
console.log(a == b);
console.log(a === b);
|
Output :
false
false
false
true
true
Reference: https://js.tensorflow.org/api/latest/#clone
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...