-
Notifications
You must be signed in to change notification settings - Fork 5.6k
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
Release Distribution base class and Add Normal, Uniform class #26355
Release Distribution base class and Add Normal, Uniform class #26355
Conversation
e9443c2
to
97ed688
Compare
python/paddle/distribution.py
Outdated
broadcasting (e.g., `high - low` is a valid operation). | ||
|
||
Args: | ||
low(float|list|numpy.ndarray|Variable): The lower boundary of uniform distribution.The data type is float32 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Variable -》 Tensor ,the following is the same
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
pytorch文档没写支持int,但实际是支持的。考虑int的情况较常用,建议也支持一下
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
python/paddle/distribution.py
Outdated
* :math:`Z`: is the normalizing constant. | ||
|
||
The parameters `low` and `high` must be shaped in a way that supports | ||
broadcasting (e.g., `high - low` is a valid operation). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
加一个broadcast semantic的链接
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
python/paddle/distribution.py
Outdated
.. code-block:: python | ||
|
||
import numpy as np | ||
from paddle.fluid import layers |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
避免出现paddle.fluid
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
python/paddle/distribution.py
Outdated
# Complete example | ||
value_npdata = np.array([0.8], dtype="float32") | ||
value_tensor = layers.create_tensor(dtype="float32") | ||
layers.assign(value_npdata, value_tensor) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
layers接口替换为新版本的paddle接口
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done, use paddle.to_tensor
method
python/paddle/distribution.py
Outdated
zero_tmp, zero_tmp.shape, min=0., max=1., seed=seed) | ||
output = uniform_random_tmp * (zero_tmp + self.high - self.low | ||
) + self.low | ||
return nn.reshape(output, output_shape) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return应该要加个name吧,return nn.reshape(output, output_shape, name=name)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
python/paddle/distribution.py
Outdated
|
||
""" | ||
name = self.name + '_probs' | ||
return ops.exp(self.log_prob(value)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
建议直接用概率定义计算
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
python/paddle/distribution.py
Outdated
* :math:`Z`: is the normalization constant. | ||
|
||
Args: | ||
loc(float|list|numpy.ndarray|Variable): The mean of normal distribution.The data type is float32. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
同Uniform,Variable -》Tensor, 建议支持int
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
python/paddle/distribution.py
Outdated
# Complete example | ||
value_npdata = np.array([0.8], dtype="float32") | ||
value_tensor = layers.create_tensor(dtype="float32") | ||
layers.assign(value_npdata, value_tensor) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
value_tensor建议用paddle.to_tensor创建
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
python/paddle/distribution.py
Outdated
|
||
""" | ||
name = self.name + '_probs' | ||
return ops.exp(self.log_prob(value)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
同Uniform,建议直接计算pdf
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
self.assertEqual(uniform1.name, name) | ||
|
||
uniform2 = Uniform(0.0, 1.0) | ||
self.assertEqual(uniform2.name, 'Uniform') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
补充method name的单测用例
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
30b37f2
to
8232ad8
Compare
8232ad8
to
8188031
Compare
[broadcasting](https://www.paddlepaddle.org.cn/documentation/docs/en/develop/beginners_guide/basic_concept/broadcasting_en.html) (e.g., `high - low` is a valid operation). | ||
|
||
Args: | ||
low(int|float|list|numpy.ndarray|Tensor): The lower boundary of uniform distribution.The data type is float32 or int |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
漏了list|numpy.ndarray|Tensor
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done, refer to PR #26535
|
||
Args: | ||
low(int|float|list|numpy.ndarray|Tensor): The lower boundary of uniform distribution.The data type is float32 or int | ||
high(int|float|list|numpy.ndarray|Tensor): The higher boundary of uniform distribution.The data type is float32 or int |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
同上
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done, refer to PR #26535
lb = tensor.cast(lb_bool, dtype=value.dtype) | ||
ub = tensor.cast(ub_bool, dtype=value.dtype) | ||
return elementwise_sub( | ||
nn.log(lb * ub), nn.log(self.high - self.low), name=name) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
动态图不需要加name,cast和elementwise_sub最好用core.ops
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done, refer to PR #26535
ub_bool = value < self.high | ||
lb = tensor.cast(lb_bool, dtype=value.dtype) | ||
ub = tensor.cast(ub_bool, dtype=value.dtype) | ||
return elementwise_div((lb * ub), (self.high - self.low), name=name) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
同上
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done, refer to PR #26535
|
||
Mathematical details | ||
|
||
The probability density function (pdf) is, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
去掉逗号
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done, refer to PR #26535
|
||
Args: | ||
loc(int|float|list|numpy.ndarray|Tensor): The mean of normal distribution.The data type is float32 or int. | ||
scale(int|float|list|numpy.ndarray|Tensor): The std of normal distribution.The data type is float32 or int. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
data type缺少list|numpy.ndarray|Tensor
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done, refer to PR #26535
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
PR types
Others
PR changes
APIs
Describe
Implement Probability Distribution related classes.
Distribution
abstract base class and itssample
,log_prob
,entropy
,probs
methods.Normal
class andUniform
class in paddle/fluid/layers/distributions.py path to paddle/distribution.py.name
of each class API.name
is initialized in__init__
function, and every function of the class has its ownname
.probs
method forNormal
class andUniform
class.