qr(A, mode='reduced', *, out=None) -> (Tensor, Tensor)¶
Computes the QR decomposition of a matrix.
Letting be or , the full QR decomposition of a matrix is defined as
where is orthogonal in the real case and unitary in the complex case, and is upper triangular.
When m > n (tall matrix), as R is upper triangular, its last m - n rows are zero. In this case, we can drop the last m - n columns of Q to form the reduced QR decomposition:
The reduced QR decomposition agrees with the full QR decomposition when n >= m (wide matrix).
Supports input of float, double, cfloat and cdouble dtypes. Also supports batches of matrices, and if
Ais a batch of matrices then the output has the same batch dimensions.
modechooses between the full and reduced QR decomposition. If
Ahas shape (*, m, n), denoting k = min(m, n)
mode= ‘reduced’ (default): Returns (Q, R) of shapes (*, m, k), (*, k, n) respectively.
mode= ‘complete’: Returns (Q, R) of shapes (*, m, m), (*, m, n) respectively.
mode= ‘r’: Computes only the reduced R. Returns (Q, R) with Q empty and R of shape (*, k, n).
Differences with numpy.linalg.qr:
mode= ‘raw’ is not implemented.
Unlike numpy.linalg.qr, this function always returns a tuple of two tensors. When
mode= ‘r’, the Q tensor is an empty tensor. This behavior may change in a future PyTorch release.
The elements in the diagonal of R are not necessarily positive.
mode= ‘r’ does not support backpropagation. Use
mode= ‘reduced’ instead.
The QR decomposition is only unique up to the sign of the diagonal of R when the first k = min(m, n) columns of
Aare linearly independent. If this is not the case, different platforms, like NumPy, or inputs on different devices, may produce different valid decompositions.
Gradient computations are only supported if the first k = min(m, n) columns of every matrix in
Aare linearly independent. If this condition is not met, no error will be thrown, but the gradient produced will be incorrect. This is because the QR decomposition is not differentiable at these points.
- Keyword Arguments
out (tuple, optional) – output tuple of two tensors. Ignored if None. Default: None.
A named tuple (Q, R).
>>> a = torch.tensor([[12., -51, 4], [6, 167, -68], [-4, 24, -41]]) >>> q, r = torch.linalg.qr(a) >>> q tensor([[-0.8571, 0.3943, 0.3314], [-0.4286, -0.9029, -0.0343], [ 0.2857, -0.1714, 0.9429]]) >>> r tensor([[ -14.0000, -21.0000, 14.0000], [ 0.0000, -175.0000, 70.0000], [ 0.0000, 0.0000, -35.0000]]) >>> torch.mm(q, r).round() tensor([[ 12., -51., 4.], [ 6., 167., -68.], [ -4., 24., -41.]]) >>> torch.mm(q.t(), q).round() tensor([[ 1., 0., 0.], [ 0., 1., -0.], [ 0., -0., 1.]]) >>> q2, r2 = torch.linalg.qr(a, mode='r') >>> q2 tensor() >>> torch.equal(r, r2) True >>> a = torch.randn(3, 4, 5) >>> q, r = torch.linalg.qr(a, mode='complete') >>> torch.allclose(torch.matmul(q, r), a, atol=1e-5) True >>> torch.allclose(torch.matmul(q.transpose(-2, -1), q), torch.eye(4), atol=1e-5) True