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

【Hackathon No.8】 add gumbel distribution api #46255

Merged
merged 69 commits into from
Oct 17, 2022

Conversation

PureNatural
Copy link
Contributor

@PureNatural PureNatural commented Sep 19, 2022

PR types

New features

PR changes

APIs

Describe

新增gumbel分布API
设计文档:PaddlePaddle/community#254
中文api文档:PaddlePaddle/docs#5290

@paddle-bot
Copy link

paddle-bot bot commented Sep 19, 2022

你的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 contributor External developers status: proposed labels Sep 19, 2022
@paddle-bot
Copy link

paddle-bot bot commented Sep 19, 2022

✅ This PR's description meets the template requirements!
Please wait for other CI results.

@dasenCoding
Copy link
Contributor

@cxxly 麻烦您 review 一下。

@cxxly
Copy link
Contributor

cxxly commented Sep 26, 2022

处理CI失败问题,原则所有CI通过,才允许request review

from paddle.distribution.uniform import Uniform
from paddle.distribution.transformed_distribution import TransformedDistribution
from paddle.distribution.transform import AffineTransform, ExpTransform
from paddle.fluid import framework as framework
Copy link
Contributor

Choose a reason for hiding this comment

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

Args:
loc(int|float): The mean of normal distribution.The data type is int, float.
scale(int|float): The std of normal distribution.The data type is int, float.

Copy link
Contributor

@cxxly cxxly Sep 26, 2022

Choose a reason for hiding this comment

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

int|float|Tensor,Tensor为必须支持数据类型

Copy link
Contributor

Choose a reason for hiding this comment

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

done

Args:
loc(int|float): The mean of normal distribution.The data type is int, float.
scale(int|float): The std of normal distribution.The data type is int, float.

Copy link
Contributor

Choose a reason for hiding this comment

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

The location parameter of gumbel distribution .....

Copy link
Contributor

Choose a reason for hiding this comment

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

done

from paddle.distribution import Gumbel

# Define a single scalar Gumbel distribution.
dist = Gumbel(loc=0., scale=1.)
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

Choose a reason for hiding this comment

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

done

f"Expected type of scale is Real|Variable, but got {type(scale)}"
)
self.loc, self.scale = paddle.broadcast_tensors([loc, scale])
finfo = np.finfo(type(self.loc))
Copy link
Contributor

Choose a reason for hiding this comment

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

如果输入类型是 Real, 此处broadcast会报错

Copy link
Contributor

Choose a reason for hiding this comment

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

done

f"Expected type of scale is Real|Variable, but got {type(scale)}"
)
self.loc, self.scale = paddle.broadcast_tensors([loc, scale])
finfo = np.finfo(type(self.loc))
Copy link
Contributor

Choose a reason for hiding this comment

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

如果 loc为Variable,此处np.finfo(type(self.loc))会报错

Copy link
Contributor

Choose a reason for hiding this comment

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

done

Tensor: The variance value.

"""
return math.pow(self.scale, 2) * math.pow(math.pi, 2) / 6
Copy link
Contributor

Choose a reason for hiding this comment

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

使用Paddle API运算,math.pow不支持Tensor类型

Copy link
Contributor

Choose a reason for hiding this comment

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

done

"""Mean of distribution

The variance is

Copy link
Contributor

Choose a reason for hiding this comment

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

The mean is ...

Copy link
Contributor

Choose a reason for hiding this comment

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

done

Tensor: std value.

"""
return math.sqrt(self.variance)
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

Choose a reason for hiding this comment

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

done


"""
y = (self.loc - value) / self.scale
return math.exp(y - math.exp(y)) / self.scale
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

Choose a reason for hiding this comment

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

done

Tensor: log probability.The data type is same with value.

"""
return math.log(self.prob(value))
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

Choose a reason for hiding this comment

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

done

Tensor: Shannon entropy of gumbel distribution.

"""
return math.log(self.scale) + 1 + np.euler_gamma
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

Choose a reason for hiding this comment

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

非常感谢您的审查意见!我们会逐一修改完善。

@cxxly
Copy link
Contributor

cxxly commented Sep 26, 2022

增加动态图/静态图下相关测试用例

@dasenCoding
Copy link
Contributor

@cxxly 您好,针对上次您提出的review意见我们已经修改,劳烦再次审查一下。

cxxly
cxxly previously approved these changes Oct 13, 2022
Copy link
Contributor

@cxxly cxxly left a comment

Choose a reason for hiding this comment

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

LGTM

Ligoml
Ligoml previously approved these changes Oct 13, 2022
Copy link
Contributor

@Ligoml Ligoml left a comment

Choose a reason for hiding this comment

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

LGTM for docs

from paddle.distribution.gumbel import Gumbel

# Gumbel distributed with loc=0, scale=1
dist = Gumbel(paddle.full([0.0]), paddle.full([1.0]))
Copy link
Contributor

@jeff41404 jeff41404 Oct 14, 2022

Choose a reason for hiding this comment

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

paddle.full([0.0]) will report error? use paddle.to_tensor(0.0) or paddle.full([1], 0.0) instead?

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([1.0]) is also needed to correct.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done.

dist = Gumbel(paddle.full([0.0]), paddle.full([1.0]))
dist.sample()
# Tensor(shape=[1], dtype=float32, place=Place(gpu:0), stop_gradient=True, [4.14814520])
value = paddle.full([0.5])
Copy link
Contributor

@jeff41404 jeff41404 Oct 14, 2022

Choose a reason for hiding this comment

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

use paddle.to_tensor(0.5) or paddle.full([1], 0.5) or instead?

"""
return paddle.log(self.prob(value))

def cdf(self, value):
Copy link
Contributor

Choose a reason for hiding this comment

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

in order to have rfc same with code, need to add this functioncdf in rfc API 实现方案

@PureNatural PureNatural dismissed stale reviews from Ligoml and cxxly via 069cadf October 14, 2022 02:41
@PureNatural
Copy link
Contributor Author

According to your comments, we have fixed parameters and add cdf function in rfc(PaddlePaddle/community#298). Would you please review again ?@jeff41404 .Thanks.

@jeff41404
Copy link
Contributor

According to your comments, we have fixed parameters and add cdf function in rfc(PaddlePaddle/community#298). Would you please review again ?@jeff41404 .Thanks.
reviewed, thanks!

@PureNatural
Copy link
Contributor Author

Sorry about that mistake. We have fixed that. Would you please review again ?@jeff41404 .Thanks.

Ligoml
Ligoml previously approved these changes Oct 14, 2022
Copy link
Contributor

@Ligoml Ligoml left a comment

Choose a reason for hiding this comment

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

LGTM for docs
copyright随后再改吧

@@ -0,0 +1,242 @@
# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
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
# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
# Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

好的,我们随后会跟进修改

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@Ligoml 您好,我们对copyright也进行了修改,麻烦您再review一下,非常感谢。

jeff41404
jeff41404 previously approved these changes Oct 17, 2022
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

Ligoml
Ligoml previously approved these changes Oct 17, 2022
Copy link
Contributor

@Ligoml Ligoml left a comment

Choose a reason for hiding this comment

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

LGTM

@dasenCoding dasenCoding dismissed stale reviews from Ligoml and jeff41404 via c957ab4 October 17, 2022 08:50
Copy link
Contributor

@Ligoml Ligoml left a comment

Choose a reason for hiding this comment

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

LGTM

@Ligoml Ligoml merged commit f1a9f87 into PaddlePaddle:develop Oct 17, 2022
@PureNatural PureNatural deleted the gumbel_api branch October 17, 2022 10:50
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.

6 participants