Open In App

Tensorflow.js tf.Tensor .toString() Method

Last Updated : 17 May, 2021
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.

The tf.tensor.toString() method is used to log the tensor in the human-readable form if you console.log() just tf.tensor then it will log the whole tensor object but after using the .toString() method it will log the tensor’s value.

Syntax:

tf.tensor.toString(verbose?)

Parameter: This function accepts single parameter which are illustrated below:

  • verbose: A boolean value. If it is true then the details of the tensor (dtype, rank, shape, values) will be returned otherwise only the tensor value will be returned. Although this is an optional parameter and by default it is false.

Return value: It returns the tensor value or tensor’s details (dtype, rank, shape, values) in string form.

Example 1: In this example, we are logging the tensor value before and after applying the toString() method.

Javascript




// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
   
// Creating the tensor
var val1 = tf.tensor([1, 2]);
 
// Applying the toString() method
var val2 = val1.toString()
 
// logging the tensor
console.log(val1)
console.log(val2)


Output:

Example 2: In this example, we apply the boolean parameters in the toString() method and see the results.

Javascript




// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
   
// Creating the tensor
var val = tf.tensor([1, 2]);
 
// Applying the toString() method
var val1 = val.toString(true)
var val2 = val.toString(false)
// logging the tensor
console.log(val1)
console.log(val2)


Output:

Tensor
  dtype: float32
  rank: 1
  shape: [2]
  values:
    [1, 2]
  Tensor
    [1, 2]

Similar Reads

Tensorflow.js tf.Tensor class .buffer() Method
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. It also helps the developers to develop ML models in JavaScript language and can use ML directly in the browser or in Node.js. The tf.Tensor class.buffer() method is used to return a p
1 min read
Tensorflow.js tf.Tensor class .dispose() Method
Tensorflow.js is an open-source library that is developed by Google for running machine learning models as well as deep learning neural networks in the browser or node environment. The .dispose() function is used to dispose the stated tf.Tensor from the memory. Syntax: dispose()Parameters: This method does not hold any parameters. Return Value: It
1 min read
Tensorflow.js tf.Tensor class .print() Method
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.print() function is used to Prints information about the tf.Tensor including its data. Syntax: tf.print(value, verbose) Parameters: value: The value of the tensor which can be a
2 min read
Tensorflow.js tf.Tensor class .clone() Method
Tensorflow.js is an open-source library for creating machine learning models in Javascript that allows users to run the models directly in the browser. The tf.clone() is a function defined in the class tf.Tensor. It's used to create a replica of a tensor. Syntax : tf.clone( values ) Parameters: values: It can be a tensor of values or an array of va
1 min read
Tensorflow.js tf.Tensor class .data() Method
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. It also helps the developers to develop ML models in JavaScript language and can use ML directly in the browser or in Node.js. The tf.Tensor class .data() method is used to asynchronou
1 min read
Tensorflow.js tf.Tensor class .arraySync() Method
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. It also helps the developers to develop ML models in JavaScript language and can use ML directly in the browser or in Node.js The tf.Tensor class .arraySync() method is used to return
1 min read
Tensorflow.js tf.Tensor class .bufferSync() Method
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. It also helps the developers to develop ML models in JavaScript language and can use ML directly in the browser or in Node.js. The tf.Tensor class.bufferSync() method is used to return
1 min read
Tensorflow.js tf.Tensor class .dataSync() Method
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. It also helps the developers to develop ML models in JavaScript language and can use ML directly in the browser or in Node.js. The tf.Tensor class .dataSync() method is used to synchro
1 min read
Tensorflow.js tf.Tensor class .array() Method
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. It also helps the developers to develop ML models in JavaScript language and can use ML directly in the browser or in Node.js. The .array() method is used to return the tensor input li
1 min read
Tensorflow.js tf.tensor() Function
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 .tensor() function is used to create a new tensor with the help of value, shape, and data type. Syntax : tf.tensor( value, shape, dataType) Parameters: Value: The value of the tens
3 min read