PyTorch torch.qr 函数
torch.qr 是 PyTorch 中用于计算矩阵 QR 分解的函数。QR 分解将矩阵 A 分解为 A = Q * R,其中 Q 是正交矩阵,R 是上三角矩阵。
函数定义
torch.qr(input, out=None)
参数:
input(Tensor): 输入矩阵。out(tuple, 可选): 输出元组。
返回值:
tuple: 返回 (Q, R) 的元组。
使用示例
实例
import torch
# 创建矩阵
A = torch.tensor([[12.0, -51.0, 4.0],
[6.0, 167.0, -68.0],
[-4.0, 24.0, -41.0]])
# QR 分解
Q, R = torch.qr(A)
print("矩阵 A:")
print(A)
print("n正交矩阵 Q:")
print(Q)
print("n上三角矩阵 R:")
print(R)
print("n验证: Q @ R =")
print(Q @ R)
# 创建矩阵
A = torch.tensor([[12.0, -51.0, 4.0],
[6.0, 167.0, -68.0],
[-4.0, 24.0, -41.0]])
# QR 分解
Q, R = torch.qr(A)
print("矩阵 A:")
print(A)
print("n正交矩阵 Q:")
print(Q)
print("n上三角矩阵 R:")
print(R)
print("n验证: Q @ R =")
print(Q @ R)
输出结果为:
矩阵 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 参考手册