PyTorch torch.slogdet Function


Pytorch torch 参考手册PyTorch torch Reference Manual

torch.slogdetIt 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 Example

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 sign and logarithm of the determinant
sign, logdet = torch.slogdet(A)

print("Matrix A:")
print(A)
print("nDeterminant sign:", sign)
print("Log determinant:", logdet)
print("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.)

Pytorch torch 参考手册PyTorch torch Reference Manual

Other Extensions