cond

paddle.linalg. cond ( x, p=None, name=None ) [源代码]

根据范数种类 p 计算一个或一批矩阵的条件数,也可以通过 paddle.cond 来调用。

参数

  • x (Tensor):输入可以是形状为 (*, m, n) 的矩阵 Tensor, * 为零或更大的批次维度,此时 p2-2;也可以是形状为 (*, n, n) 的可逆(批)方阵 Tensor,此时 p 为任意已支持的值。数据类型为 float32 或 float64 。

  • p (float|string,可选):范数种类。目前支持的值为 fronuc1-12-2inf-inf。默认值为 None,即范数种类为 2

  • name (str,可选) - 具体用法请参见 Name,一般无需设置,默认值为 None。

返回

Tensor,条件数的计算结果,数据类型和输入 x 的一致。

代码示例

>>> import paddle
>>> paddle.seed(2023)
>>> x = paddle.to_tensor([[1., 0, -1], [0, 1, 0], [1, 0, 1]])

>>> # compute conditional number when p is None
>>> out = paddle.linalg.cond(x)
>>> print(out)
Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
1.41421378)

>>> # compute conditional number when order of the norm is 'fro'
>>> out_fro = paddle.linalg.cond(x, p='fro')
>>> print(out_fro)
Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
3.16227770)

>>> # compute conditional number when order of the norm is 'nuc'
>>> out_nuc = paddle.linalg.cond(x, p='nuc')
>>> print(out_nuc)
Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
9.24264145)

>>> # compute conditional number when order of the norm is 1
>>> out_1 = paddle.linalg.cond(x, p=1)
>>> print(out_1)
Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
2.)

>>> # compute conditional number when order of the norm is -1
>>> out_minus_1 = paddle.linalg.cond(x, p=-1)
>>> print(out_minus_1)
Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
1.)

>>> # compute conditional number when order of the norm is 2
>>> out_2 = paddle.linalg.cond(x, p=2)
>>> print(out_2)
Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
1.41421378)

>>> # compute conditional number when order of the norm is -1
>>> out_minus_2 = paddle.linalg.cond(x, p=-2)
>>> print(out_minus_2)
Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
0.70710671)

>>> # compute conditional number when order of the norm is inf
>>> out_inf = paddle.linalg.cond(x, p=float("inf"))
>>> print(out_inf)
Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
2.)

>>> # compute conditional number when order of the norm is -inf
>>> out_minus_inf = paddle.linalg.cond(x, p=-float("inf"))
>>> print(out_minus_inf)
Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
1.)

>>> a = paddle.randn([2, 4, 4])
>>> print(a)
Tensor(shape=[2, 4, 4], dtype=float32, place=Place(cpu), stop_gradient=True,
[[[ 0.06132207,  1.11349595,  0.41906244, -0.24858207],
  [-1.85169315, -1.50370061,  1.73954511,  0.13331604],
  [ 1.66359663, -0.55764782, -0.59911072, -0.57773495],
  [-1.03176904, -0.33741450, -0.29695082, -1.50258386]],
 [[ 0.67233968, -1.07747352,  0.80170447, -0.06695852],
  [-1.85003340, -0.23008066,  0.65083790,  0.75387722],
  [ 0.61212337, -0.52664012,  0.19209868, -0.18707706],
  [-0.00711021,  0.35236868, -0.40404350,  1.28656745]]])

>>> a_cond_fro = paddle.linalg.cond(a, p='fro')
>>> print(a_cond_fro)
Tensor(shape=[2], dtype=float32, place=Place(cpu), stop_gradient=True,
[6.37173700 , 35.15114594])

>>> b = paddle.randn([2, 3, 4])
>>> print(b)
Tensor(shape=[2, 3, 4], dtype=float32, place=Place(cpu), stop_gradient=True,
[[[ 0.03306439,  0.70149767,  0.77064633, -0.55978841],
  [-0.84461296,  0.99335045, -1.23486686,  0.59551388],
  [-0.63035583, -0.98797107,  0.09410731,  0.47007179]],
 [[ 0.85850012, -0.98949534, -1.63086998,  1.07340240],
  [-0.05492965,  1.04750168, -2.33754158,  1.16518629],
  [ 0.66847134, -1.05326962, -0.05703246, -0.48190674]]])

>>> b_cond_2 = paddle.linalg.cond(b, p=2)
>>> print(b_cond_2)
Tensor(shape=[2], dtype=float32, place=Place(cpu), stop_gradient=True,
[2.86566353, 6.85834455])