PyTorch torch.slogdet function
PyTorch torch reference manual
torch.slogdet is a function in PyTorch used to compute the sign and logarithm of the determinant of a matrix. It is more numerically stable for cases where the determinant is very large or very small.
Function definition
torch.slogdet(input, out=None)
Parameters:
input(Tensor): Input square matrix.out(tuple, optional): Output tuple.
Return Value:
tuple: Returns a tuple of (sign, log value).
Usage examples
Example
import
torch
Create a square matrix
A
=
torch.
tensor
(
[
[
1.0
,
2.0
,
3.0
]
,
[
0.0
,
4.0
,
5.0
]
,
[
0.0
,
0.0
,
6.0
]
]
)
Compute the determinant sign and logarithm
sign
,
logdet
=
torch.
slogdet
(
A
)
(
"Matrix A:"
)
(
A
)
(
"nDeterminant sign:"
,
sign
)
(
"Log determinant:"
,
logdet
)
(
"Determinant:"
,
sign * torch.
exp
(
logdet
)
)
The output is:
矩阵 A:
tensor([[1., 2., 3.],
[0., 4., 5.],
[0., 0., 6.]])
行列式符号: tensor(1.)
行列式对数: tensor(3.5835)
行列式: tensor(24.)