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 6th No.31】paddle.distribution.Normal support complex normal distribution -part #65103

Merged
merged 5 commits into from
Jun 28, 2024

Conversation

NKNaN
Copy link
Contributor

@NKNaN NKNaN commented Jun 13, 2024

PR Category

User Experience

PR Types

New features

Description

Upgarde paddle.distribution.Normal to support complex normal distribution

Copy link

paddle-bot bot commented Jun 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 Jun 13, 2024
Copy link

paddle-ci-bot bot commented Jun 21, 2024

Sorry to inform you that 93adb8c's CIs have passed for more than 7 days. To prevent PR conflicts, you need to re-run all CIs manually.


.. math::

Z = \pi \sigma^2

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.

应该没有的,
$Z = X + iY$ ,且 $X \sim N(\mu, \sigma^2)$ , $Y \sim N(\mu, \sigma^2)$$X$$Y$ 相互独立,则 $Z \sim CN(\mu+i\mu, 2\sigma^2)$
$\mu+i\mu = \mu_z \in \mathbb{C}$$2\sigma^2 = \sigma_z^2 \in \mathbb{R}$ ,则其概率密度函数为:

$$ \begin{aligned} p_Z(z) = p_{xy}(x, y) & = \frac{1}{\sqrt{2 \pi \sigma^2}} \exp[-\frac{(x-\mu)^2}{2 \sigma^2}] \cdot \frac{1}{\sqrt{2 \pi \sigma^2}} \exp[-\frac{(y-\mu)^2}{2 \sigma^2}] \\ & = \frac{1}{\pi \cdot 2 \sigma^2} \exp[-\frac{(x-\mu)^2+(y-\mu)^2}{2 \sigma^2}] \\ & = \frac{1}{\pi \cdot 2 \sigma^2} \exp[-\frac{|(x-\mu)+(y-\mu) \cdot i |^2}{2 \sigma^2}] \\ & = \frac{1}{\pi \cdot 2 \sigma^2} \exp[-\frac{|(x+y \cdot i)-(\mu+\mu \cdot i) |^2}{2 \sigma^2}] \\ & =\frac{1}{\pi\sigma_z^2}\exp[-\frac{|z - \mu_z|^2}{\sigma_z^2}] \end{aligned}$$

if isinstance(scale, (tuple, list)):
scale = np.array(scale)
if scale.dtype == np.float64:
scale = scale.astype('float32')

Choose a reason for hiding this comment

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

这种情况下为啥需要转换成float32呢?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

这里是想把 tuple 和 list 先转成 np.adarray,因为numpy的 np.array 默认是 np.float64,这里是转成paddle的默认类型。是不是这样更好一点:
if isinstance(scale, (tuple, list)):
scale = np.array(scale, dtype=np.float32)

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.

已修改


.. math::

entropy(\sigma) = \log (\pi e \sigma^2) + 1

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.

Entopy:

$$ \begin{aligned} H(Z) & = - \int_z p(z)\log p(z) dz \\ & = - \mathbb{E}[\log p(z)] \\ & = - \mathbb{E}[-\log(\pi \sigma_z^2)-\frac{1}{\sigma_z^2}|z-\mu_z|^2] \\ & = \log(\pi \sigma_z^2) + \frac{1}{\sigma_z^2}\mathbb{E}[|z-\mu_z|^2] \\ & = \log(\pi \sigma_z^2) + 1 \end{aligned}$$

KL-DIV:

$$ \begin{aligned} \mathcal{D}_{KL}(p || q) & = \int_z p(z)\log \frac{p(z)}{q(z)} dz \\ & = \mathbb{E}_p[\log \frac{p(z)}{q(z)}] \\ & = \mathbb{E}_p[\log[\frac{\sigma_q^2}{\sigma_p^2} \exp(-\frac{|z-\mu_p|^2}{\sigma_p^2}+\frac{|z-\mu_q|^2}{\sigma_q^2})]] \\ & = -\log \frac{\sigma_p^2}{\sigma_q^2} - \frac{1}{\sigma_p^2}\mathbb{E}_p(z-\mu_p)^2 + \frac{1}{\sigma_q^2}\mathbb{E}_p(z-\mu_q)^2\\ & = -2\log \frac{\sigma_p}{\sigma_q} - 1 + \frac{1}{\sigma_q^2}\mathbb{E}_p(z^cz -\mu_q^cz-z^c\mu_q+\mu_q^c\mu_q) \\ & = -2\log \frac{\sigma_p}{\sigma_q} - 1 + \frac{1}{\sigma_q^2}(\sigma_p^2+\mu_p^c\mu_p -\mu_q^c\mu_p-\mu_p^c\mu_q+\mu_q^c\mu_q) \\ & = -2\log \frac{\sigma_p}{\sigma_q} - 1 + \frac{1}{\sigma_q^2}(\sigma_p^2+|\mu_p-\mu_q|^2) \\ & = \frac{\sigma_p^2}{\sigma_q^2} - 1 + \frac{|\mu_p-\mu_q|^2}{\sigma_q^2}-2\log \frac{\sigma_p}{\sigma_q} \end{aligned} $$

# used to construct another Normal object to calculate kl_divergence
m2 = int((np.random.ranf() - 0.5) * 8)
self.other_loc_np = m2 + m2 * 1j
self.other_scale_np = int((np.random.ranf() - 0.5) * 8)

Choose a reason for hiding this comment

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

这里为啥scale_np和other_scale_np后面的系数不一样

Copy link
Contributor Author

Choose a reason for hiding this comment

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

已修改为一致

self.other_scale_np = np.random.randn(batch_size, dims).astype(
'float32'
)
while not np.all(self.scale_np > 0):

Choose a reason for hiding this comment

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

这里是不是应该是判断other_scale_np

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

@zhiminzhang0830 zhiminzhang0830 left a comment

Choose a reason for hiding this comment

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

LGTM

@luotao1 luotao1 merged commit dc217e5 into PaddlePaddle:develop Jun 28, 2024
30 of 32 checks passed
@luotao1
Copy link
Contributor

luotao1 commented Jun 28, 2024

请更新对应的中文文档

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.

None yet

3 participants