Skip to content

修改bernoulli的inplace实现(p为随机值为1的概率) #73271

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

Merged
merged 1 commit into from
Jun 12, 2025

Conversation

Qin-sx
Copy link
Contributor

@Qin-sx Qin-sx commented Jun 12, 2025

PR Category

User Experience

PR Types

Bug fixes

Description

之前Bernoulli的inplace实现有bug,p实现成了随机值为0的概率:与非inplace版本的paddle.bernoulli、pytorch实现均不同。

现在修改成p是随机值为1的概率,与非inplace版本以及Pytorch均对齐了。

测试代码:

import paddle
import torch
import numpy as np

np.random.seed(23)
paddle.seed(23)
torch.manual_seed(23)

probs_np = np.random.rand(3, 3).astype(np.float32)

probs_paddle = paddle.to_tensor(probs_np)
probs_torch = torch.tensor(probs_np)

print("\n概率值 (输入):")
print(probs_paddle)
probs_paddle.bernoulli_(p=0.99)
print("\nPaddle bernoulli_ 结果:")
print(probs_paddle)

print("\n概率值 (输入):")
print(probs_torch)
probs_torch.bernoulli_(p=0.99)
print("\nPyTorch bernoulli_ 结果:")
print(probs_torch)
概率值 (输入):
Tensor(shape=[3, 3], dtype=float32, place=Place(gpu:0), stop_gradient=True,
       [[0.51729786, 0.94696259, 0.76545978],
        [0.28239584, 0.22104536, 0.68622208],
        [0.16713920, 0.39244246, 0.61805236]])

Paddle bernoulli_ 结果:
Tensor(shape=[3, 3], dtype=float32, place=Place(gpu:0), stop_gradient=True,
       [[1., 1., 1.],
        [1., 1., 1.],
        [1., 1., 1.]])

概率值 (输入):
tensor([[0.5173, 0.9470, 0.7655],
        [0.2824, 0.2210, 0.6862],
        [0.1671, 0.3924, 0.6181]])

PyTorch bernoulli_ 结果:
tensor([[1., 1., 1.],
        [1., 1., 1.],
        [1., 1., 1.]])

	modified:   python/paddle/tensor/random.py
Copy link

paddle-bot bot commented Jun 12, 2025

你的PR提交成功,感谢你对开源项目的贡献!
请关注后续CI自动化测试结果,详情请参考Paddle-CI手册
Your PR has been submitted. Thanks for your contribution!
Please wait for the result of CI firstly. See Paddle CI Manual for details.

@paddle-bot paddle-bot bot added the contributor External developers label Jun 12, 2025
Copy link
Contributor

@zhwesky2010 zhwesky2010 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@codecov-commenter
Copy link

Codecov Report

All modified and coverable lines are covered by tests ✅

Please upload report for BASE (develop@917ecbd). Learn more about missing BASE report.

Additional details and impacted files
@@             Coverage Diff             @@
##             develop    #73271   +/-   ##
===========================================
  Coverage           ?   100.00%           
===========================================
  Files              ?         1           
  Lines              ?         2           
  Branches           ?         0           
===========================================
  Hits               ?         2           
  Misses             ?         0           
  Partials           ?         0           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@zhwesky2010 zhwesky2010 merged commit 6fcebc2 into PaddlePaddle:develop Jun 12, 2025
51 checks passed
DrRyanHuang pushed a commit to DrRyanHuang/Paddle that referenced this pull request Jun 14, 2025
modified:   python/paddle/tensor/random.py
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
contributor External developers
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants