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

Clone all params of distribution to fix a bug #113

Merged
merged 2 commits into from
May 25, 2020

Conversation

ktaaaki
Copy link
Collaborator

@ktaaaki ktaaaki commented Apr 7, 2020

I found a bug regarding the saving/loading Pixyz object.

1. Probrem

First, I make Normal distributions with the same tensors. (p.loc=q.loc=q.scale)

>>> from pixyz.distributions import Normal
>>> ones=torch.ones(2)
>>> p=Normal(loc=torch.zeros(2), scale=ones)
>>> q=Normal(loc=ones, scale=ones)

Next, I make new distribution with different params & load them to old one.

>>> save_path = "./tmp.pt"
>>> torch.save(Normal(loc=torch.zeros(2), scale=2 * torch.ones(2)).state_dict(), save_path)
>>> q.load_state_dict(torch.load(save_path))
<All keys matched successfully>

Then, loaded params are not correct. Inplace-updates overwrites linked params.

>>> q.scale
tensor([[2., 2.]])
>>> q.loc
tensor([[2., 2.]])
>>> p.scale
tensor([[2., 2.]])

2. Change

This is because DistributionBase class treats buffered parameters as variables of Reference types.

Therefore, I added the clone() method when registering features. Although it wastes memory, it correctly works.

And this change is related to pull request #110. Just cloning solve its issue, so I removed some lines.

@ktaaaki ktaaaki changed the title Clone all params of distribution Clone all params of distribution to fix a bug Apr 14, 2020
@masa-su masa-su merged commit 3ef087e into develop/v0.2.0 May 25, 2020
@masa-su masa-su deleted the fix/overwritten_dists_param branch May 25, 2020 01:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants