[Accuracy diff No.78、142、143] Improve get_numpy_tensor for rpow and pow #528
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
受影响API
paddle.Tensor.__pow__paddle.Tensor.__rpow__修改思路
这些 API 的计算公式为$y = a^b$ ,因此需要根据输入类型分三种情况考虑:
合入后剩余case
推导A
设MAX是dtype支持的最大值,B是常数底,不妨设B>1
则
y = B^x
dy/dx = y*lnB
为了保证不溢出,需要y<MAX,dy/dx <MAX
令y = MAX,则
B^x=MAX
xlnB=lnMAX
x = lnMAX/lnB
令dy/dx = MAX
则 x = ln(MAX/lnB)/lnB
注:
当0<B<1时,可以通过1/B限制,因为此时y和dy/dx取得最大值的时机是x取最小值,x的最小值和最大值互为相反数,所以这与令B变为倒数是等价的
推导B
设MAX是dtype支持的最大值,B是常数,假设设x!=0,B!=1,B!=0(有特殊处理)
则
y = x^B
dy/dx = Bx^(B-1)
为了保证不溢出,需要y<MAX,dy/dx <MAX
令y = MAX,则
x^B=MAX
x=MAX^(1/B)
令dy/dx = MAX
则 x = (MAX/B)^(1/(B-1))
为了简化实现和优化性能,当B小于2的时候,采用固定值
因为梯度的限制计算过于繁琐,考虑进行放缩,找到一个更小的好计算的上界即可
(MAX/B)^(1/(B-1)) >(MAX/B)^(1/B)
发现MAX^(1/B)>(MAX/B)^(1/B),所以只需要满足x<(MAX/B)^(1/B)即可。
相关 PR
备注
cast和astype相关精度问题已在其他PR修复。