Skip to content
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

paddle.uniform api生成数值超过指定范围 #45713

Closed
TUDelftHao opened this issue Sep 3, 2022 · 10 comments
Closed

paddle.uniform api生成数值超过指定范围 #45713

TUDelftHao opened this issue Sep 3, 2022 · 10 comments

Comments

@TUDelftHao
Copy link
Contributor

TUDelftHao commented Sep 3, 2022

bug描述 Describe the Bug

paddle版本:2.3.2
系统:MacOS, M1芯片

在使用paddle.uniform生成(-1,1)区间随机数时,会有一定概率出现返回值为-1或1的情况,导致后续计算log1p出现异常值。因为是随机生成,复现该bug可能需多次重复执行代码。

import paddle
import numpy as np

eps = 0
shape = (30000, 2)
for i in range(10000):
    uniform = paddle.uniform(shape=shape,
                    min=float(np.nextafter(-1, 1)) + eps / 2,
                    max=1. - eps / 2,
                    dtype='float32')
    res = paddle.log1p(-uniform.abs())
    if paddle.any(paddle.isinf(res)):
        position = paddle.where(paddle.isinf(res))
        print("inf occurs: ", [position[0][0], position[1][0]])
        print("badcase: ", uniform[[position[0][0], position[1][0]]])

某次复现出的结果:
image

辛苦看下是我使用的问题还是uniform 算子实现存在bug。

其他补充信息 Additional Supplementary Information

No response

@paddle-bot
Copy link

paddle-bot bot commented Sep 3, 2022

您好,我们已经收到了您的问题,会安排技术人员尽快解答您的问题,请耐心等待。请您再次检查是否提供了清晰的问题描述、复现代码、环境&版本、报错信息等。同时,您也可以通过查看官网API文档常见问题历史IssueAI社区来寻求解答。祝您生活愉快~

Hi! We've received your issue and please be patient to get responded. We will arrange technicians to answer your questions as soon as possible. Please make sure that you have posted enough message to demo your request. You may also check out the APIFAQGithub Issue and AI community to get the answer.Have a nice day!

@TUDelftHao
Copy link
Contributor Author

TUDelftHao commented Sep 3, 2022

It's my fault. Ignore it, please.

@paddle-bot paddle-bot bot added status/close 已关闭 and removed status/new-issue 新建 labels Sep 3, 2022
@TUDelftHao TUDelftHao reopened this Sep 3, 2022
@paddle-bot paddle-bot bot added status/reopen 重新打开 and removed status/close 已关闭 labels Sep 3, 2022
@zhiqiu
Copy link
Contributor

zhiqiu commented Sep 5, 2022

hi,我本地跑出了-1,但是没有出现1。uniform是产生[min, max)的随机数, 建议设置 min > -1 试试。

@TUDelftHao
Copy link
Contributor Author

TUDelftHao commented Sep 5, 2022

np.nextafter(-1, 1)

hi,我使用了np.nextafter(-1, 1)这个函数生成的数就是趋近于-1但是不等于-1的值,所以-1这个数字应当也是不在uniform范围内的吧?
另外我重新跑了代码确实存在结果为1的情况
image

hi,我本地跑出了-1,但是没有出现1。uniform是产生[min, max)的随机数, 建议设置 min > -1 试试。

@yuwoyizhan
Copy link

楼主说uniform是产生(min, max)的随机数,下面的zhiqiu说uniform是产生[min, max)的随机数,请问2位,这个结论是谁告诉你们的?
我听说飞桨底层都是C++实现的,上面采用python封装.据我了解,C++的uniform是产生[min,max],只不过由于min到max有无穷多个浮点数,导致了生成min和max都需要不少时间,有可能需要几小时才能复现.但是我很明确告诉你们,C++的uniform肯定可以产生min和max这两个值的,至少visual studio2022可以做到.我测试过很多次.

@zhiqiu
Copy link
Contributor

zhiqiu commented Sep 6, 2022

楼主说uniform是产生(min, max)的随机数,下面的zhiqiu说uniform是产生[min, max)的随机数,请问2位,这个结论是谁告诉你们的? 我听说飞桨底层都是C++实现的,上面采用python封装.据我了解,C++的uniform是产生[min,max],只不过由于min到max有无穷多个浮点数,导致了生成min和max都需要不少时间,有可能需要几小时才能复现.但是我很明确告诉你们,C++的uniform肯定可以产生min和max这两个值的,至少visual studio2022可以做到.我测试过很多次.

是的,uniform的c++ kernel用的是std::uniform_real_distribution实现,c++文档说是[min, max)。参考:cppreference
image

@zhiqiu
Copy link
Contributor

zhiqiu commented Sep 6, 2022

np.nextafter(-1, 1)

hi,我使用了np.nextafter(-1, 1)这个函数生成的数就是趋近于-1但是不等于-1的值,所以-1这个数字应当也是不在uniform范围内的吧? 另外我重新跑了代码确实存在结果为1的情况 image

hi,我本地跑出了-1,但是没有出现1。uniform是产生[min, max)的随机数, 建议设置 min > -1 试试。

我推测你能得到-1和1还有一种可能,就是 你用的float32,精度不够,你可以使用float64再试试。

@TUDelftHao
Copy link
Contributor Author

np.nextafter(-1, 1)

hi,我使用了np.nextafter(-1, 1)这个函数生成的数就是趋近于-1但是不等于-1的值,所以-1这个数字应当也是不在uniform范围内的吧? 另外我重新跑了代码确实存在结果为1的情况 image

hi,我本地跑出了-1,但是没有出现1。uniform是产生[min, max)的随机数, 建议设置 min > -1 试试。

我推测你能得到-1和1还有一种可能,就是 你用的float32,精度不够,你可以使用float64再试试。

hi, 用了float64的话确实没有出现这个情况,但是float32是paddle默认数据类型,这个是不是考虑严格支持一下?paddle.uniform默认也是float32。

@zhiqiu
Copy link
Contributor

zhiqiu commented Sep 7, 2022

好的,我们会讨论下怎么修复哈。

@paddle-bot paddle-bot bot added status/following-up 跟进中 and removed status/reopen 重新打开 labels Sep 7, 2022
@luotao1 luotao1 self-assigned this Jan 13, 2023
@paddle-bot paddle-bot bot closed this as completed Jan 16, 2024
Copy link

paddle-bot bot commented Jan 16, 2024

Since you haven't replied for more than a year, we have closed this issue/pr.
If the problem is not solved or there is a follow-up one, please reopen it at any time and we will continue to follow up.
由于您超过一年未回复,我们将关闭这个issue/pr。
若问题未解决或有后续问题,请随时重新打开,我们会继续跟进。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants