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

mindnlp.core.nn.init.uniform函数生成异常 #301

Open
ShangJingLi opened this issue Aug 13, 2024 · 2 comments
Open

mindnlp.core.nn.init.uniform函数生成异常 #301

ShangJingLi opened this issue Aug 13, 2024 · 2 comments

Comments

@ShangJingLi
Copy link

Environment

Hardware Environment(Ascend/GPU/CPU):

CPU

Software Environment:

  • MindSpore version (source or binary):2.2.14
  • Python version (e.g., Python 3.7.5):3.9.19
  • OS platform and distribution (e.g., Linux Ubuntu 16.04): Window 11

Describe the current behavior

mindnlp套件的nn.init.uniform在一些情况下生成异常,该函数基于mindspore的Initializer类和其他初始化相关模块封装。当下界大于或等于0时,输出结果正常;下界小于0,上界大于0时,输出结果异常,异常结果:输出均为上界;当上界小于0时,输出结果异常,输出逻辑不明。当设置下界大于上界时,应抛出错误,但实际未抛出且输出结果逻辑不明

Describe the expected behavior

当上界和下界被设置时且上界大于下界时,生成的序列应在上界和下界之间呈现均匀分布;当下界被设置大于上界时应抛出错误信息

Steps to reproduce the issue

a1 = np.ones(shape=(1, 10)).astype(np.float32)
ta1 = mindnlp.core.nn.init.uniform_(Tensor(a1), a=-1, b=1) # 不正常
ta2 = mindnlp.core.nn.init.uniform_(Tensor(a1), a=0, b=1) # 看似正常
ta3 = mindnlp.core.nn.init.uniform_(Tensor(a1), a=1, b=10) # 看似正常
ta4 = mindnlp.core.nn.init.uniform_(Tensor(a1), a=-2, b=0) # 不正常
print(ta1, "理论上下界为-1,上界为1的均匀分布\n")
print(ta2, "理论上下界为0,上界为1的均匀分布\n")
print(ta3, "理论上为下界为1,上界为10的均匀分布\n")
print(ta4, "理论上为下界为-2,上界为0的均匀分布\n")

Related log / screenshot

image

Special notes for this issue

@moyanxinxu
Copy link

moyanxinxu commented Aug 15, 2024

mindspore-lab/mindnlp@56774dd

  • 上面是mindnlp的owner的bug fix
  • 至于下界大于上界在代码中不会出现错误的情况可以参考mindspore的实现逻辑,其中value = uniform_real * (maxval - minval) + minval。这段代码如果maxval小于minval,那么将得到一个反向的均匀分布,其中生成的随机数会从minval开始递减到maxval
  • rebase最新的代码

@moyanxinxu
Copy link

理论上下界为-1上界为1的均匀分布:
[[-0.24242505  0.18378146  0.3106798  -0.33906373  0.6060065  -0.7903174   -0.95910054  0.00910801 -0.37601295  0.25596344]]

理论上下界为0上界为1的均匀分布:
[[0.71938884 0.6745291  0.69897014 0.9580319  0.05868613 0.22249985    0.75021434 0.4646567  0.290779   0.9318139 ]]

理论上为下界为1上界为10的均匀分布:
[[3.4718785 4.2812524 7.0038333 4.4340143 9.26519   4.8634377 3.8028338    6.1175847 3.104072  5.806433 ]]

理论上为下界为-2上界为0的均匀分布:
[[-0.7266476  -1.8067375  -0.7297912  -1.4816263  -0.7031752  -0.04069502   -0.8378217  -1.6065764  -1.4900833  -1.3968813 ]]

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

No branches or pull requests

2 participants