📜  Tensorflow.js tf.Tensor 类 .print() 方法

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

Tensorflow.js tf.Tensor 类 .print() 方法

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

tf.print()函数用于打印有关 tf.Tensor 的信息,包括其数据。

句法:

tf.print(value, verbose)

参数:

  • value:张量的值,可以是简单的或嵌套的 Array 或 TypedArray 的数字。如果数组元素是字符串,那么它们将编码为 UTF-8 并保存为 Uint8Array[]。
  • verbose:是一个布尔值,表示是否打印 Tensor 的详细信息,包括 dtype 和 size,verbose 的默认值为 False。

下面的例子演示了tf.print()函数:

示例 1:在此示例中,我们使用 ts.tensor2d 创建一个张量,我们使用 tf.print函数使用详细值作为 true 进行打印。

Javascript
// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Creating the tensor
const verbose = true;
var val = tf.tensor2d(["geeks","for","geeks","website"], [2, 2]);
  
// Printing the tensor
val.print(verbose);


Javascript
// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Creating the tensor
const verbose = false;
var val = tf.tensor(["geeks","for","geeks"]);
  
// Printing the tensor
val.print(verbose);


输出:

Tensor
  dtype: string
  rank: 2
  shape: [2,2]
  values:
    [['geeks', 'for'    ],
     ['geeks', 'website']]

示例 2:在此示例中,我们使用 ts.tensor2d 创建一个张量,并且我们使用 tf.print函数使用详细值作为 false 进行打印。

Javascript

// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Creating the tensor
const verbose = false;
var val = tf.tensor(["geeks","for","geeks"]);
  
// Printing the tensor
val.print(verbose);

输出:

Tensor
    ['geeks', 'for', 'geeks']

参考: https://js.tensorflow.org/api/latest/#tf.Tensor.print