Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

PyTorch torch.qr function

PyTorch torch.qr function


PyTorch torch reference manual PyTorch torch reference manual

torch.qr is a function in PyTorch used to compute the QR decomposition of a matrix. QR decomposition factorizes a matrix A into A = Q * R, where Q is an orthogonal matrix and R is an upper triangular matrix.

Function definition

torch.qr(input, out=None)

Parameters:

Return Value:


Usage examples

Example

import

torch

Create a matrix

A

=

torch.

tensor

(

[

[

12.0

,

51.0

,

4.0

]

,

                 

[

6.0

,

167.0

,

68.0

]

,

                 

[

4.0

,

24.0

,

41.0

]

]

)

QR decomposition

Q

,

R

=

torch.

qr

(

A

)

print

(

"Matrix A:"

)

print

(

A

)

print

(

"nOrthogonal matrix Q:"

)

print

(

Q

)

print

(

"nUpper triangular matrix R:"

)

print

(

R

)

print

(

"nVerify: Q @ R ="

)

print

(

Q

@

R

)

The output is:

矩阵 A:

tensor([[ 12., -51.,   4.],

        [  6., 167., -68.],

        [ -4.,  24., -41.]])

正交矩阵 Q:

tensor([[-0.8571,  0.3943,  0.3314],

        [-0.4286, -0.9029, -0.0343],

        [ 0.2857, -0.1714,  0.9428]])

上三角矩阵 R:

tensor([[ -14.,  -21.,   14.],

        [   0., -175.,   70.],

        [   0.,    0.,  -35.]])

验证: Q @ R =

tensor([[ 12., -51.,   4.],

        [  6., 167., -68.],

        [ -4.,  24., -41.]])


PyTorch torch reference manual PyTorch torch reference manual