lstsq(A, B, rcond=None, *, driver=None) -> (Tensor, Tensor, Tensor, Tensor)¶
Computes a solution to the least squares problem of a system of linear equations.
Letting be or , the least squares problem for a linear system with is defined as
where denotes the Frobenius norm.
Supports inputs of float, double, cfloat and cdouble dtypes. Also supports batches of matrices, and if the inputs are batches of matrices then the output has the same batch dimensions.
driverchooses the LAPACK/MAGMA function that will be used. For CPU inputs the valid values are ‘gels’, ‘gelsy’, ‘gelsd, ‘gelss’. For CUDA input, the only valid driver is ‘gels’, which assumes that
Ais full-rank. To choose the best driver on CPU consider:
Ais well-conditioned (its condition number is not too large), or you do not mind some precision loss.
For a general matrix: ‘gelsy’ (QR with pivoting) (default)
Ais full-rank: ‘gels’ (QR)
Ais not well-conditioned.
‘gelsd’ (tridiagonal reduction and SVD)
But if you run into memory issues: ‘gelss’ (full SVD).
See also the full description of these drivers
condis used to determine the effective rank of the matrices in
driveris one of (‘gelsy’, ‘gelsd’, ‘gelss’). In this case, if are the singular values of A in decreasing order, will be rounded down to zero if . If
cond= None (default),
condis set to the machine precision of the dtype of
This function returns the solution to the problem and some extra information in a named tuple of four tensors (solution, residuals, rank, singular_values). For inputs
Bof shape (*, m, n), (*, m, k) respectively, it cointains
solution: the least squares solution. It has shape (*, n, k).
residuals: the squared residuals of the solutions, that is, . It has shape equal to the batch dimensions of
A. It is computed when m > n and every matrix in
Ais full-rank, otherwise, it is an empty tensor. If
Ais a batch of matrices and any matrix in the batch is not full rank, then an empty tensor is returned. This behavior may change in a future PyTorch release.
rank: tensor of ranks of the matrices in
A. It has shape equal to the batch dimensions of
A. It is computed when
driveris one of (‘gelsy’, ‘gelsd’, ‘gelss’), otherwise it is an empty tensor.
singular_values: tensor of singular values of the matrices in
A. It has shape (*, min(m, n)). It is computed when
driveris one of (‘gelsd’, ‘gelss’), otherwise it is an empty tensor.
While X =
B, this function computes the solution in a faster and more numerically stable way than performing the computations separately.
The default value of
rcondmay change in a future PyTorch release. It is therefore recommended to use a fixed value to avoid potential breaking changes.
A (Tensor) – lhs tensor of shape (*, m, n) where * is zero or more batch dimensions.
B (Tensor) – rhs tensor of shape (*, m, k) where * is zero or more batch dimensions.
rcond (float, optional) – used to determine the effective rank of
rcondis set to the machine precision of the dtype of
Atimes max(m, n). Default: None.
- Keyword Arguments
driver (str, optional) – name of the LAPACK/MAGMA method to be used. If None, ‘gelsy’ is used for CPU inputs and ‘gels’ for CUDA inputs. Default: None.
A named tuple (solution, residuals, rank, singular_values).
>>> a = torch.tensor([[10, 2, 3], [3, 10, 5], [5, 6, 12]], dtype=torch.float) >>> a.unsqueeze_(0) >>> b = torch.tensor([[[2, 5, 1], [3, 2, 1], [5, 1, 9]], [[4, 2, 9], [2, 0, 3], [2, 5, 3]]], dtype=torch.float) >>> x = torch.linalg.lstsq(a, b).solution >>> torch.dist(x, a.pinverse() @ b) tensor(2.0862e-07) >>> sv = torch.linalg.lstsq(a, driver='gelsd').singular_values >>> torch.dist(sv, a.svd().S) tensor(5.7220e-06) >>> a[:, 0].zero_() >>> xx, rank, _ = torch.linalg.lstsq(a, b) >>> rank tensor()