Open In App

Tensorflow.js tf.Tensor Class

Last Updated : 12 Dec, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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




// Importing the tensorflow library
import * as tf from "@tensorflow/tfjs"
 
// Creating Tensor with values
let c = tf.tensor([1, 2, 3, 4])
 
// Using the print() method of Tensor class
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




// Importing tensorflow library
import * as tf from "@tensorflow/tfjs"
 
// Creating Tensor class with value and [4, 1] shape
const a = tf.tensor([1, 2, 3, 4],[4,1]);
 
// Using the clone() method on a Tensor
let b = a.clone();
 
// Printing the clone Tensor
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




// Importing tensorflow library
import * as tf from "@tensorflow/tfjs"
 
// Creating tensor
const a = tf.tensor([1, 2, 3, 4]);
// Using toString() method in Tensor class
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




// Importing tensorflow library
import * as tf from "@tensorflow/tfjs"
 
// Creating tensor
const a = tf.tensor([1, 2, 3, 4]);
 
// Using data method on Tensor class
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




// Importing tensorflow library
import * as tf from "@tensorflow/tfjs"
 
// Creating tensor
const a = tf.tensor([1, 2, 3, 4]);
 
// Using the dataSync() method
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




// Importing tensorflow library
import * as tf from "@tensorflow/tfjs"
 
// Creating tensor
const a = tf.tensor([1, 2, 3, 4]);
 
// Using the buffer() method on Tensor class
let b = a.buffer();
 
// Printing result of Promise
 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




// Importing tensorflow library
import * as tf from "@tensorflow/tfjs"
// Creating tensor
const a = tf.tensor([1, 2, 3, 4]);
// Using bufferSync method on Tensor class
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




// Importing tensorflow library
import * as tf from "@tensorflow/tfjs"
 
// Creating tensor
const a = tf.tensor([1, 2, 3, 4]);
 
// Using the array() method on Tensor class
let b = a.array();
 
// Printing result of Promise
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




// Importing tensorflow library
import * as tf from "@tensorflow/tfjs"
 
// Creating tensor
const a = tf.tensor([1, 2, 3, 4]);
 
// Using the arraySync() method on Tensor class
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




// Importing tensorflow library
import * as tf from "@tensorflow/tfjs"
 
// Creating tensor
const b = tf.tensor([1, 2, 3, 4]);
 
// Using the dispose() method on Tensor class
b.dispose();
b.print();


Output:

Tensor is disposed.


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads