📜  Tensorflow.js tf.Tensor .toString() 方法

📅  最后修改于: 2022-05-13 01:56:21.918000             🧑  作者: Mango

Tensorflow.js tf.Tensor .toString() 方法

Tensorflow.js 是谷歌开发的一个开源库,用于在浏览器或节点环境中运行机器学习模型和深度学习神经网络。

tf.tensor.toString()方法用于以人类可读的形式记录张量,如果你 console.log() 只是 tf.tensor 那么它将记录整个张量对象,但在使用 . toString()方法,它将记录张量的值。

句法:

tf.tensor.toString(verbose?)

参数:此函数接受单个参数,如下所示:

  • 详细:一个布尔值。如果为,则将返回张量的详细信息( dtype、rank、shape、values ),否则将仅返回张量值。虽然这是一个可选参数,默认情况下它是false。

返回值:以字符串形式返回张量值或张量的详细信息(dtype、rank、shape、values)。

示例 1:在此示例中,我们在应用toString()方法之前和之后记录张量值。

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)


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)


输出:

示例 2:在此示例中,我们在toString( ) 方法中应用布尔参数并查看结果。

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)

输出:

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