torchbox.linalg package
Submodules
torchbox.linalg.decomposition module
- class torchbox.linalg.decomposition.MatrixSquareRoot(*args, **kwargs)
Bases:
torch.autograd.function.Function
Square root of a positive definite matrix.
- NOTE: matrix square root is not differentiable for matrices with
zero eigenvalues.
- static backward(ctx, grad_output)
Defines a formula for differentiating the operation with backward mode automatic differentiation (alias to the vjp function).
This function is to be overridden by all subclasses.
It must accept a context
ctx
as the first argument, followed by as many outputs as theforward()
returned (None will be passed in for non tensor outputs of the forward function), and it should return as many tensors, as there were inputs toforward()
. Each argument is the gradient w.r.t the given output, and each returned value should be the gradient w.r.t. the corresponding input. If an input is not a Tensor or is a Tensor not requiring grads, you can just pass None as a gradient for that input.The context can be used to retrieve tensors saved during the forward pass. It also has an attribute
ctx.needs_input_grad
as a tuple of booleans representing whether each input needs gradient. E.g.,backward()
will havectx.needs_input_grad[0] = True
if the first input toforward()
needs gradient computated w.r.t. the output.
- static forward(ctx, input)
This function is to be overridden by all subclasses. There are two ways to define forward:
Usage 1 (Combined forward and ctx):
@staticmethod def forward(ctx: Any, *args: Any, **kwargs: Any) -> Any: pass
It must accept a context ctx as the first argument, followed by any number of arguments (tensors or other types).
See combining-forward-context for more details
Usage 2 (Separate forward and ctx):
@staticmethod def forward(*args: Any, **kwargs: Any) -> Any: pass @staticmethod def setup_context(ctx: Any, inputs: Tuple[Any, ...], output: Any) -> None: pass
The forward no longer accepts a ctx argument.
Instead, you must also override the
torch.autograd.Function.setup_context()
staticmethod to handle setting up thectx
object.output
is the output of the forward,inputs
are a Tuple of inputs to the forward.See extending-autograd for more details
The context can be used to store arbitrary data that can be then retrieved during the backward pass. Tensors should not be stored directly on ctx (though this is not currently enforced for backward compatibility). Instead, tensors should be saved either with
ctx.save_for_backward()
if they are intended to be used inbackward
(equivalently,vjp
) orctx.save_for_forward()
if they are intended to be used for injvp
.
- torchbox.linalg.decomposition.eig(A, cdim=None, dim=(- 2, - 1), keepdim=False)
Computes the eigenvalues and eigenvectors of a square matrix.
- Parameters
A (Tensor) – any size tensor, both complex and real representation are supported. For real representation, the real and imaginary dimension is specified by
cdim
orcaxis
.cdim (int or None, optional) – if
A
andB
are complex tensors but represented in real format,cdim
orcaxis
should be specified (Default isNone
).dim (tulpe or list) – dimensions for multiplication (default is (-2, -1))
keepdim (bool) – keep dimensions? (include complex dim, defalut is
False
)
- torchbox.linalg.decomposition.eigvals(A, cdim=None, dim=(- 2, - 1), keepdim=False)
Computes the eigenvalues of a square matrix.
- Parameters
A (Tensor) – any size tensor, both complex and real representation are supported. For real representation, the real and imaginary dimension is specified by
cdim
orcaxis
.cdim (int or None, optional) – if
A
andB
are complex tensors but represented in real format,cdim
orcaxis
should be specified (Default isNone
).dim (tulpe or list) – dimensions for multiplication (default is (-2, -1))
keepdim (bool) – keep dimensions? (include complex dim, defalut is
False
)
- torchbox.linalg.decomposition.svd_rank(A, svdr='auto')
compute rank of the truncated Singular Value Decomposition
Gavish, Matan, and David L. Donoho, The optimal hard threshold for singular values is, IEEE Transactions on Information Theory 60.8 (2014): 5040-5053.
torchbox.linalg.orthogonalization module
- torchbox.linalg.orthogonalization.orth(x)
Orthogonalization
A function like MATLAB’s
orth
. After orthogonalizing, each column is a orthogonal basis.- Parameters
x (Tensor) – The matrix to be orthogonalized.
Examples
code:
x = th.tensor([[1, 2.], [3, 4], [5, 6]]) y = orth(x) print(x) print(y) print((y[0, :] * y[1, :] * y[2, :]).sum()) print((y[:, 0] * y[:, 1]).sum())
result:
tensor([[1., 2.], [3., 4.], [5., 6.]]) tensor([[-0.2298, 0.8835], [-0.5247, 0.2408], [-0.8196, -0.4019]]) tensor(-0.1844) tensor(-1.7881e-07)