-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Fix MvNormal.random #4207
Fix MvNormal.random #4207
Conversation
The shape issues discussed in #4206 , directly affect the failing of test So, I imagine #4206 getting solved first. |
Codecov Report
@@ Coverage Diff @@
## master #4207 +/- ##
==========================================
+ Coverage 87.44% 87.57% +0.12%
==========================================
Files 88 88
Lines 14342 14321 -21
==========================================
- Hits 12542 12541 -1
+ Misses 1800 1780 -20
|
fe12432
to
38cf2b0
Compare
This looks like a great improvement 🤩 What's left on this here @Sayam753 ? What do you need to push it through the finish line? |
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.
Thanks @Sayam753 and sorry for the delay in reviewing. I left bunch of comments on things that we should change/address before being able to merge. I think that the PR looks promising but we can improve it
Hi @AlexAndorra
|
Thanks for the update @Sayam753, and for you work on this, which would clearly be a great improvement 🤩 |
de1fa05
to
ae9dd49
Compare
Hi @Spaak , The underneath reason behind failing test cases earlier was a wrong condition, which I have corrected. I came across the str representation issue in the pdb debugger. So, #4243 is not a blocker. |
I also invite @junpenglao to review. |
@Sayam753 if this is ready for review from your end, can you stop from draft? |
These changes will be covered by the tests from #4206 |
As per discussions with @lucianopaz , I have changed the broadcasting logic to use But there are still weird things going on with >>> import pymc3 as pm
>>> import numpy as np
>>> from pymc3.distributions.shape_utils import broadcast_dist_samples_to
>>>
>>> cov = np.eye(3)
>>> broad_samples = broadcast_dist_samples_to(to_shape=(10, 3, 3), samples=[cov], size=(3, ))[0]
>>> broad_samples.shape
(3, 10, 3, 3)
>>> broad_samples[0, 0]
array([[1., 0., 0.],
[1., 0., 0.],
[1., 0., 0.]]) I expected to see identity matrix here. Is this a bug or am I missing something? |
The tests passed. The PR is ready for review. |
|
The problem is that the first dimension of the cov array is being interpreted as the sample_shape. You will have to add some checks based on the symbolic theano tensors ndim to ensure that the first dimension of cov and mu isn't sample shape just by chance. |
This is confusing. We have theano tensors defined at distribution level. So, they can only represent batch and event shapes. Why should there be a check for |
So, does this mean, we need to have checks to prevent batch/event shape being misinterpreted as sample shape? |
Yes, exactly. In the example that you posted before, the covariance matrix event shape was (3, 3), but the first dimension was interpreted as the sample shape because it happened to match the requested What i mean that we could do is something like core_dims = self.cov.ndim
sampled_dims = cov.ndim
if core_dims == sampled_dims:
# We are sure that this means that the drawn cov only has batch and event dimensions
# We can prevent the first dimensions from being misinterpreted by adding singleton dimensions to the left of cov
cov = np.reshape(cov, (1,)*len(sample_shape) + cov.shape)
else:
# We don't do anything because we assume the extra dimensions come from sample_shape
pass
# At this point we use the shape utils broadcasting We should also have to do something like this for |
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.
Finally, the PR is ready to get merged from my side. I have written test cases for related issues and for different combinations of shapes as well. Ping @lucianopaz for final review.
Just a small comment below.
Added batch dimensions to all parametrization
147231c
to
3d2ebd7
Compare
Thanks @Sayam753 this was a big one! |
Hi @Sayam753 Is it now always necessary to specify pm.MvNormal.dist(mu=np.zeros(3), cov=np.eye(3)).random() now gives Traceback (most recent call last):
File "t.py", line 4, in <module>
pm.MvNormal.dist(mu=np.zeros(3), cov=np.eye(3)).random()
File "/home/marco/pymc3-dev/pymc3/distributions/multivariate.py", line 273, in random
param = np.broadcast_to(param, shape=output_shape + dist_shape[-1:])
File "<__array_function__ internals>", line 5, in broadcast_to
File "/home/marco/miniconda3/envs/pymc3-dev-py38/lib/python3.8/site-packages/numpy/lib/stride_tricks.py", line 180, in broadcast_to
return _broadcast_to(array, shape, subok=subok, readonly=True)
File "/home/marco/miniconda3/envs/pymc3-dev-py38/lib/python3.8/site-packages/numpy/lib/stride_tricks.py", line 118, in _broadcast_to
raise ValueError('cannot broadcast a non-scalar to a scalar array')
ValueError: cannot broadcast a non-scalar to a scalar array , but passing |
Yes. It is necessary. In fact, for each distribution it is important as per discussions with @lucianopaz . |
That's where I ran into this :) Sure, I'll make an issue for this in pymc-examples |
Thank your for opening a PR!
Depending on what your PR does, here are a few things you might want to address in the description:
This PR is a potential fix to related issues Shape issues with MvNormal.random method #4185 , Shape issues in pm.MvNormal #3706 , Shape Parameter Behavior Confusing for Normal and MvNormal #2848 , Shape issues when sampling prior predictive with MvNormal #3829 , Issue with prior_predictive_sample and MvNormal #3758
To achieve deterministic nature with random method, the best I could think of -
mu
andparametrizations
to respective shapes.Yes, tests are written
Related notebook