Skip to content

Conversation

@NKNaN
Copy link
Contributor

@NKNaN NKNaN commented May 13, 2024

PR Category

User Experience

PR Types

New features

Description

新增 bernoulli_ API

@paddle-bot
Copy link

paddle-bot bot commented May 13, 2024

你的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 May 13, 2024
zeros_mask = x < p
x.masked_fill_(ones_mask, 1.0)
out = x.masked_fill_(zeros_mask, 0.0)
return out
Copy link
Contributor

Choose a reason for hiding this comment

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

x.masked_fill_(ones_mask, 1.0)
x.masked_fill_(zeros_mask, 0.0)
return x

这样吧

Copy link
Contributor Author

Choose a reason for hiding this comment

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

已修改

def set_np_compare_func(self):
self.np_compare = np.array_equal

def inplace_api_processing(self, var):
Copy link
Contributor

Choose a reason for hiding this comment

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

也测一下类方法调用吧,x.bernoulli_

Copy link
Contributor Author

Choose a reason for hiding this comment

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

已增加

return var.bernoulli_(self.p)

def non_inplace_api_processing(self, var):
return paddle.bernoulli(paddle.zeros(self.shape) + self.p)
Copy link
Contributor

Choose a reason for hiding this comment

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

这个改成 paddle.full(self.shape, p) 更好

Copy link
Contributor Author

Choose a reason for hiding this comment

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

已修改

def non_inplace_api_processing(self, var):
return paddle.bernoulli(paddle.zeros(self.shape) + self.p)

def test_inplace_api(self):
Copy link
Contributor

Choose a reason for hiding this comment

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

测一下反向,随机数反向应该全0

Copy link
Contributor Author

Choose a reason for hiding this comment

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

已添加反向测试

non_inplace_var.numpy().var(), inplace_var.numpy().var(), atol=0.01
)

def test_inplace_class_method(self):
Copy link
Contributor

Choose a reason for hiding this comment

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

测一下反向,随机数反向应该全0

Copy link
Contributor Author

Choose a reason for hiding this comment

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

已添加反向测试

@zhwesky2010
Copy link
Contributor

@NKNaN 提交了吗

@NKNaN
Copy link
Contributor Author

NKNaN commented May 15, 2024

@NKNaN 提交了吗

已提交

zhwesky2010
zhwesky2010 previously approved these changes May 15, 2024
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

Args:
x(Tensor): The input tensor to be filled with random values.
p (float|Tensor, optional): The success probability parameter of the output Tensor's bernoulli distribution.
Copy link
Contributor

@jeff41404 jeff41404 May 16, 2024

Choose a reason for hiding this comment

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

Should we support scenarios where p can broadcast to x? if so, we should add description p must be broadcast to x here, and add unit tests to check error if cannot broadcast. If not, it needs to be clarified that p can only be float and Tensor which must have same shape with x, and add unit tests to check error if p.shape != x.shape

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We could support the scenarios. Let me update the code.

Copy link
Contributor

@jeff41404 jeff41404 left a comment

Choose a reason for hiding this comment

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

LGTM

"""
This is the inplace version of api ``bernoulli``, which returns a Tensor filled
with random values sampled from a bernoulli distribution. The output Tensor will
be inplaced with input ``x``. Please refer to :ref:`api_tensor_bernoulli`.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
be inplaced with input ``x``. Please refer to :ref:`api_tensor_bernoulli`.
be inplaced with input ``x``. Please refer to :ref:`api_paddle_bernoulli`.

>>> import paddle
>>> x = paddle.randn([3, 4])
>>> x.bernoulli_()
>>> # doctest: +SKIP('random check')
Copy link
Contributor

Choose a reason for hiding this comment

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

输出能固定吗?通过固定随机数

Copy link
Contributor

@sunzhongkai588 sunzhongkai588 left a comment

Choose a reason for hiding this comment

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

文档问题重新提一个 PR 改吧,这个先 approve

@luotao1 luotao1 merged commit fba4675 into PaddlePaddle:develop May 21, 2024
co63oc pushed a commit to co63oc/Paddle that referenced this pull request May 23, 2024
…64252)

* add bernoulli_

* update

* update docs

* fix typo

* update code and test

* add test case for backward

* add test broadcast error and update docs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants