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

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

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

Tensorflow.js 是谷歌开发的一个开源库,用于在浏览器或节点环境中运行机器学习模型和深度学习神经网络。它还可以帮助开发人员用 JavaScript 语言开发 ML 模型,并且可以直接在浏览器或 Node.js 中使用 ML。

.array() 方法用于像嵌套数组一样返回张量输入。此外,数据的重定位是异步进行的。

句法:

array()

参数:此方法不保存任何参数。

返回值:返回 number[] 的 promise。

示例 1:

Javascript
// Importing tensorflow library
import * as tf from "@tensorflow/tfjs"
  
// Creating a tensor
const tn = tf.tensor([5, 6, 3, 4]);
  
// Calling array() method
const res = await tn.array();
  
// Printing output
console.log(res)


Javascript
// Importing tensorflow library
import * as tf from "@tensorflow/tfjs"
  
// Calling array() method and
// Printing output
console.log(await tf.tensor2d([1.7, 3.7, 9.7, null], [4, 1]).array());


输出:

5,6,3,4

示例 2:

Javascript

// Importing tensorflow library
import * as tf from "@tensorflow/tfjs"
  
// Calling array() method and
// Printing output
console.log(await tf.tensor2d([1.7, 3.7, 9.7, null], [4, 1]).array());

输出:

1.7000000476837158,3.700000047683716,9.699999809265137,0

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