-
Notifications
You must be signed in to change notification settings - Fork 39
[Accuracy diff No.25、101] Fix accuracy diff for paddle.linalg.eigh API #276
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
Thanks for your contribution! |
|
@NKNaN 同学好😊!这应该是属于 paddle 与 torch 在设计上不一致,我们 apitest 要做的就是尽量将 torch 的行为与 paddle 对齐~ 如果两者的差异在理论上是可以接受的(均是正确的特征向量),我们就需要修改 torch 的输出,不改 paddle 的输出,具体修改应该属于 paddle2torch 模块,位于:tester/paddle_to_torch 需要的修改有:
这样即可正常进行测试,不必修改 tester/accuracy.py~~ |
|
嗯嗯了解,但是问题是输出的特征向量是拼接在一起的一个矩阵,可能有的特征向量是一样的有的是不一样的,而且好像也没有一个特定的规律,比如得到的特征向量矩阵 paddle 是 [v1^T, v2^T, v3^T, v4^T],但 torch 可能是 [-v1^T, v2^T, -v3^T, v4^T] 或者 [v1^T, -v2^T, v3^T, -v4^T] 或者 [e^{i \theta_1} * v1^T, e^{i \theta_2} * v2^T, e^{i \theta_3} * v3^T, v4^T] (theta 可以是任意角度)都有可能,这样的话好像也找不到一个统一的方法去修改 torch 的输出? |
哦~我明白你的意思了,那paddle与torch输出的特征向量顺序是否一定相同呢?是否可能有 paddle 是 [v1^T, v2^T, v3^T, v4^T], torch 是 [-v2^T, v1^T, -v4^T, v3^T]? 此外,在此处直接torch_assert_accuracy处理,不利于走完全流程,同学可以考虑做做如下处理,让这个api显得不那么特立独行:
|
特征向量顺序是一致的,和所得特征值的相对顺序一样。
修改了一下,回测也可以通过。 |
| paddle_output.append([coef_vector, abs_coef]) | ||
| torch_output.append([coef_vector_approx, one]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
我的想法是:
paddle_output = [coef_vector, abs_coef]
torch_output = [coef_vector_approx, one]
这样可以同时比较绝对误差和相对误差,不需要 paddle_output_cache~
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
嗯嗯,忘记删了
Co-authored-by: 苍天荒 <[email protected]>
cangtianhuang
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
|
@ luotao1 |
直接修改标题就行~ |
问题的原因是实对称矩阵或hermitian matrix的特征向量是不唯一的,可能因为底层实现的不同导致了 paddle 和 torch 的输出不同,所以应该对 paddle.linalg.eigh 的特征向量输出做单独的精度判断:一个简单的判断办法是将 paddle 和 torch 对应输出的特征向量相除,得到的应该是 e^{i \theta} * vec[1, 1, ..., 1]^T ,所以判断相除所得向量的所有元素(大致)相等,以及相除所得向量每个元素的模长为1即可。
另外由于 eigh_grad 的计算依赖于前向计算得到的特征向量,因此反向计算的结果也会有差异,所以建议不测 eigh 的反向算子

回测结果:


GPU:
CPU: