Python PyTorch – torch.linalg.cond() 函数

您所在的位置:网站首页 矩阵的条件数cond无穷怎么计算 Python PyTorch – torch.linalg.cond() 函数

Python PyTorch – torch.linalg.cond() 函数

2024-04-06 17:44| 来源: 网络整理| 查看: 265

在本文中,我们将讨论如何在 PyTorch 中计算矩阵的条件数。我们可以使用 torch.linalg.cond() 方法获取矩阵的条件数。

torch.linalg.cond() 方法

此方法用于计算矩阵相对于矩阵范数的条件数。此方法接受一个矩阵和一批矩阵作为输入。它将返回一个带有计算条件数的张量Tensors。它支持 double、float、cfloat 和 cdouble 数据类型的输入。在继续之前,让我们看看这个方法的语法。

语法:torch.linalg.cond(M, P=None)

参数:

M(张量Tensors):是一个矩阵或一组矩阵。 P (int, inf, -inf, ‘fro’, ‘nuc’, optional):它定义了计算的矩阵范数。 P 2-norm 的默认值。

返回:它将返回一个带有计算条件数的张量Tensors。

示例1

在这个例子中,我们使用 torch.tensor 定义了一个张量Tensors,我们将在 Torch 的帮助下计算矩阵的条件数。 linalg.cond 方法。

Python3实现

# import the required library import torch   # define a tensor (matrix) M = torch.tensor([[-0.1345, -0.7437, 1.2377],                   [0.9337, 1.6473, 0.4346],                   [-1.6345, 0.9344, -0.2456]])   # display input tensor print(" Input Matrix M: ", M)   # compute the condition number Output = torch.linalg.cond(M)   # Display result print(" Condition Number: ", Output)

输出:

示例 2

在本例中,我们将借助 torch.linalg.cond 方法计算不同 P 值的矩阵的条件数。

Python3实现

# import the required library import torch   # define a tensor (matrix) M = torch.tensor([[-0.1345, -0.7437, 1.2377],                   [0.9337, 1.6473, 0.4346],                   [-1.6345, 0.9344, -0.2456]])   # display input tensor print(" Input Matrix M: ", M)   print("When P is fro = ", torch.linalg.cond(M, p='fro')) print("When P is nuc =",  torch.linalg.cond(M, p='nuc')) print("When P is inf =", torch.linalg.cond(M, p=float('inf'))) print("When P is -inf =", torch.linalg.cond(M, p=float('-inf'))) print("When P is 1 =", torch.linalg.cond(M, p=1)) print("When P is -1 =", torch.linalg.cond(M, p=-1)) print("When P is 2 =", torch.linalg.cond(M, p=2)) print("When P is -2 =", torch.linalg.cond(M, p=-2))

输出:

示例 3

在本例中,我们将借助 torch.linalg.cond 方法计算一批矩阵的条件数。

Python3实现

# import the required library import torch   # define a tensor (matrix) M = torch.tensor([[[-0.1345, -0.7437, 1.2377],                    [0.9337, 1.6473, 0.4346],                    [-1.6345, 0.9344, -0.2456]],                   [[1.3343, -1.3456, 0.7373],                    [1.4334, 0.2473, 1.1333],                    [-1.5341, 1.5348, -1.4567]]])   # display input tensor print(" Input Matrix M: ", M)   # compute the condition number Output = torch.linalg.cond(M)   # Display result print(" Condition Number: ", Output)

输出:



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3