Tensorflow.js tf.Tensor Class
Last Updated :
12 Dec, 2022
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.
A tf.Tensor object represents an immutable, multidimensional array of numbers that has a shape and a data type. Tensors are the core data-structure of TensorFlow.js They are a generalization of vectors and matrices to potentially higher dimensions.
Syntax:
Tensor(value);
Properties: This class has the following properties:
- rank: It defines the number of dimensions that the tensor contains.
- shape: It defines the size of each dimension of the data.
- dtype: It defines the data type of the tensor.
Return value: It returns a Tensor object with provided values.
The examples below demonstrate the Tensor class and its various methods.
Example 1: In this example, we will create a Tensor class and see the example of the print() method. This method is used to print the Tensor class.
Javascript
import * as tf from "@tensorflow/tfjs"
let c = tf.tensor([1, 2, 3, 4])
c.print();
|
Output:
Tensor
[[1, 2],
[3, 4]]
Example 2: In this example, we will see the clone() method of the Tensor class. The clone() method is used to copy the existing Tensor class.
Javascript
import * as tf from "@tensorflow/tfjs"
const a = tf.tensor([1, 2, 3, 4],[4,1]);
let b = a.clone();
b.print();
|
Output:
Tensor[[1],
[2],
[3],
[4]]
Example 3: In this example, we use the toString() method of the Tensor class. This method is used to make Tensor class data in human readable form.
Javascript
import * as tf from "@tensorflow/tfjs"
const a = tf.tensor([1, 2, 3, 4]);
let b = a.toString( true );
console.log(b);
|
Example 4: In this example, we will see the data() method of the Tensor class. It returns a Promise which, in resolve returns the values of the Tensor.
Javascript
import * as tf from "@tensorflow/tfjs"
const a = tf.tensor([1, 2, 3, 4]);
let b = a.data();
b.then((x)=>console.log(x),
(b)=>console.log( "Error while copying" ));
|
Output:
1, 2, 3, 4
Example 5: In this example, we will use the dataSync() method of Tensor class. This method copies the values of the Tensor class and returns them.
Javascript
import * as tf from "@tensorflow/tfjs"
const a = tf.tensor([1, 2, 3, 4]);
let b = a.dataSync();
console.log(b);
|
Output:
1, 2, 3, 4
Example 6: In this example, we will use the buffer() method of the Tensor class. It returns the promise of tf.TensorBuffer, which holds the data of underlying data.
Javascript
import * as tf from "@tensorflow/tfjs"
const a = tf.tensor([1, 2, 3, 4]);
let b = a.buffer();
b.then((x)=>console.log(x),
(b)=>console.log( "Error while copying" ) );
|
Output:
TensorBuffer {
dtype:"float32",
shape:(1) [4],
size:4,
values:1,2,3,4,
strides:(0) [ ]
}
Example 7: In this example, we will use the bufferSync() method. It returns a tf.TensorBuffer that holds the underlying data.
Javascript
import * as tf from "@tensorflow/tfjs"
const a = tf.tensor([1, 2, 3, 4]);
let b = a.bufferSync();
console.log(b);
|
Output:
TensorBuffer {
dtype:"float32",
shape:(1) [4],
size:4,
values:1,2,3,4,
strides:(0) []
}
Example 8: In this example, we will use the array() method of the Tensor class. It returns the Promise of the tensor data as a nested array.
Javascript
import * as tf from "@tensorflow/tfjs"
const a = tf.tensor([1, 2, 3, 4]);
let b = a.array();
b.then((x)=>console.log(x),
(b)=>console.log( "Error while copying" ));
|
Output:
[1, 2, 3, 4]
Example 9: In this example, we will use the arraySync() method of Tensor class. It returns the Tensor data in nested form.
Javascript
import * as tf from "@tensorflow/tfjs"
const a = tf.tensor([1, 2, 3, 4]);
let b = a.arraySync();
console.log(b);
|
Output:
[1, 2, 3, 4]
Example 10: In this example, we will use the dispose() method of the Tensor class. It disposes the tf.Tensor from memory.
Javascript
import * as tf from "@tensorflow/tfjs"
const b = tf.tensor([1, 2, 3, 4]);
b.dispose();
b.print();
|
Output:
Tensor is disposed.