📜  Tensorflow.js tf.norm()函数

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

Tensorflow.js tf.norm()函数

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

tf.norm()函数用于计算矩阵、向量和标量的范数。此函数还可以计算其他几个向量范数,例如 1 范数、2 范数或欧几里得、inf 范数,以及通常 p > 0 的 p 范数和矩阵范数。

句法:

tf.norm (x, ord?, axis?, keepDims?)

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

  • x:指定输入数组。
  • ord:这是可选的。它指定规范的顺序。
  • 轴:这是可选的。 如果axis为空,则将输入视为一个向量,并在张量中的整个值集上计算单个向量范数,即 norm(x, ord) 等价于 norm(x.reshape([-1] ), ord)。而如果axis是一个整数,那么输入作为一批向量进行检查,axis确定计算向量范数的x轴。如果axis是整数的2元组,则将其视为一批矩阵,并且axis确定Nd数组中计算矩阵范数的轴。
  • keepDims:这是可选的。如果设置为 true,则范数与输入具有相同的维度。

返回值:返回 tf.tensor。

下面是说明使用 tf.norm()函数的示例。

示例 1:

Javascript
// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Initializing a tensor of some elements
let Norm = tf.tensor1d([15, 14, 23, 52]);
  
// Calling the .norm() function over
// the above tensor as its parameter
// and printing the result.
Norm.norm().print();


Javascript
// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Initializing a tensor of some elements
let Norm = tf.tensor1d([5, 4, 3, 2]);
  
// Calling the .norm() function over
// the above tensor as its parameter
// and printing the result.
tf.norm(Norm).print();


输出:

Tensor
    60.44832992553711

示例 2:

Javascript

// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Initializing a tensor of some elements
let Norm = tf.tensor1d([5, 4, 3, 2]);
  
// Calling the .norm() function over
// the above tensor as its parameter
// and printing the result.
tf.norm(Norm).print();

输出:

Tensor
    7.348469257354736

参考: https://js.tensorflow.org/api/latest/#norm