简体   繁体   English

Pytorch torch.linalg.svd 返回不正交的 U 和 V^T

[英]Pytorch torch.linalg.svd returning U and V^T, which are not orthogonal

Using U,S, VT = torch.linalg.svd(M), the matrix 'M' is large, so I am getting the matrices U and VT as non orthogonal.使用 U、S、VT = torch.linalg.svd(M),矩阵“M”很大,所以我得到的矩阵 U 和 VT 是非正交的。 When I compute torch.norm(torch.mm(matrix, matrix.t()) - identity_matrix)) its 0.004 and also when I print MM^T, the diagonal entries are not 1, rather 0.2 or 0.4 and non diagonals are not 0, but 0.0023.当我计算 torch.norm(torch.mm(matrix, matrix.t()) - identity_matrix)) 它的 0.004 以及当我打印 MM^T 时,对角线条目不是 1,而是 0.2 或 0.4,非对角线不是0,但 0.0023。 IS there a way to get SVD with orthogonal U and V^T?有没有办法通过正交 U 和 V^T 获得 SVD? But the singular values ie diagonal elements of S are nera to 1 only.但是奇异值,即 S 的对角元素仅小于 1。

matrix = torch.randn(4096, 4096) 
u, s, vh = torch.linalg.svd(matrix) 
matrix = torch.mm(u, vh)
print('norm ||WTW - I||: ',torch.norm(torch.mm(matrix, matrix.t()) - torch.eye(matrix.shape[0]))) 
print(matrix)

I have done some numerical analysis, and it seems Pytorch's linalg_svd is not returning orthogonal u and vh.我做了一些数值分析,Pytorch 的 linalg_svd 似乎没有返回正交的 u 和 vh。 Can others verify this behaviour is with others too or I am doing something wrong?其他人是否也可以验证这种行为是否与其他人有关,或者我做错了什么?

Matlab: I tried inbuilt svd decomposition in matlab, and there norm(u*transpose(u) - eye(4096)) , there its 1E-13. Matlab:我在 matlab 中尝试了内置 svd 分解,那里有norm(u*transpose(u) - eye(4096)) ,那里有它的 1E-13。

Why do you expect matrix @ matrix.T to be close to I ?为什么你期望matrix @ matrix.T接近I

SVD is a decomposition of the input matrix matrix . SVD是输入矩阵matrix分解 It does not alter it, it only produces three matrices u , s and vh st matrix = u @ s @ vh .它不会改变它,它只会产生三个矩阵usvh st matrix = u @ s @ vh The special thing about SVD is that the matrices u , s and vh are not arbitrary, but unique: u and v are orthogonal, and s is diagonal. SVD的特殊之处在于矩阵usvh不是任意的,而是唯一的: uv是正交的,而s是对角线的。

What you should actually expect is:您实际上应该期望的是:

matrix = torch.randn(4096, 4096) 
u, s, vh = torch.linalg.svd(matrix)

print(f'||uuT - I|| = {torch.norm(u@u.t() - torch.eye(u.shape[0]))}')
print(f'||vvT - I|| = {torch.norm(vh.t()@vh - torch.eye(vh.shape[0]))}')

Note that due to numeric issues the difference ||uuT -I||请注意,由于数字问题,差异||uuT -I|| is not likely to be exactly zero, but some small number depending on the dimensions of your matrix (the larger the matrix -- the greater the error), and the precision of the dtype you used: float32 (aka single ) will likely to result with larger error compared to float64 (aka double ).不太可能完全为零,而是一些小数字,具体取决于矩阵的维度(矩阵越大——误差越大),以及您使用的dtype的精度: float32 (又名single )可能会导致与float64 (又名double )相比误差更大。


PS, the operator @ stands for matrix multiplication. PS,运算符@代表矩阵乘法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 np.linalg.qr(A)或scipy.linalg.orth(A)用于查找正交基础(python) - np.linalg.qr(A) or scipy.linalg.orth(A) for finding the orthogonal basis (python) 带有 u 和 v 分量的 gnuplot 上的向量场 - vector field on gnuplot with u and v components 生成与特定方向正交的两个正交矢量 - generating two orthogonal vectors that are orthogonal to a particular direction Matlab计算数组中所有(u,v)向量的最近邻居距离 - Matlab calculating nearest neighbour distance for all (u, v) vectors in an array 计算卫星数据中 u 和 v 风分量的旋度 - Python - calculating the curl of u and v wind components in satellite data - Python Rust 是否执行自<vec<t> &gt; 对于 Vec <u>,如果我已经实现 From</u><t> <u>为了你?</u> </t></vec<t> - Does Rust implement From<Vec<T>> for Vec<U> if I have already implemented From<T> for U? 如何静态检查模板的类型T是否为std :: vector <U>,其中U是float,double或integral - How to statically check if type T of a template is std::vector<U>, where U is either float, double or integral 为什么 t() 返回一个“向量”? - Why is t() returning a 'vector'? 如何以有效方式传播两个向量u和v以填充矩形框 - How to propagate two vectors u and v to fill a rectangle box in an efficient way 在处理中将点正交投影到一条线上? - Orthogonal projection of a point onto a line in processing?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM