-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Fix random sampling in Mixture #3004
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
Changes from 7 commits
c8a39d2
e0ac44b
e396d95
4181303
d336457
3189d35
d259df0
45324ac
33dc2d1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -158,18 +158,33 @@ def random_choice(*args, **kwargs): | |
| return np.random.choice(k, p=w, *args, **kwargs) | ||
|
|
||
| w = draw_values([self.w], point=point)[0] | ||
|
|
||
| comp_tmp = self._comp_samples(point=point, size=None) | ||
| if self.shape.size == 0: | ||
| distshape = np.asarray(np.broadcast(w, comp_tmp).shape)[..., :-1] | ||
| else: | ||
| distshape = self.shape | ||
| w_samples = generate_samples(random_choice, | ||
| w=w, | ||
| broadcast_shape=w.shape[:-1] or (1,), | ||
| dist_shape=self.shape, | ||
| dist_shape=distshape, | ||
| size=size).squeeze() | ||
| comp_samples = self._comp_samples(point=point, size=size) | ||
|
|
||
| if comp_samples.ndim > 1: | ||
| return np.squeeze(comp_samples[np.arange(w_samples.size), w_samples]) | ||
| if (size is None) or (distshape.size == 0): | ||
| comp_samples = self._comp_samples(point=point, size=size) | ||
| if comp_samples.ndim > 1: | ||
| samples = np.squeeze(comp_samples[np.arange(w_samples.size), ..., w_samples]) | ||
| else: | ||
| samples = np.squeeze(comp_samples[w_samples]) | ||
| else: | ||
| return np.squeeze(comp_samples[w_samples]) | ||
| samples = np.zeros((size,)+tuple(distshape)) | ||
| for i in range(size): | ||
| w_tmp = w_samples[i, :] | ||
| comp_tmp = self._comp_samples(point=point, size=None) | ||
| if comp_tmp.ndim > 1: | ||
| samples[i, :] = np.squeeze(comp_tmp[np.arange(w_tmp.size), ..., w_tmp]) | ||
| else: | ||
| samples[i, :] = np.squeeze(comp_tmp[w_tmp]) | ||
|
|
||
| return samples | ||
|
|
||
|
|
||
| class NormalMixture(Mixture): | ||
|
|
@@ -197,21 +212,21 @@ class NormalMixture(Mixture): | |
| the component standard deviations | ||
| tau : array of floats | ||
| the component precisions | ||
| distshape : shape of the Normal component | ||
| notice that it should be different than the shape | ||
| of the mixture distribution, with one axis being | ||
| the number of component. | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. small typo: "number of components". I am also not sure I understand the docstring either -- would adding "a mixture of three 2d normal distributions would have
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah this is a tricky one. |
||
|
|
||
| Note: You only have to pass in sd or tau, but not both. | ||
| """ | ||
|
|
||
| def __init__(self, w, mu, *args, **kwargs): | ||
| def __init__(self, w, mu, distshape=(), *args, **kwargs): | ||
| _, sd = get_tau_sd(tau=kwargs.pop('tau', None), | ||
| sd=kwargs.pop('sd', None)) | ||
|
|
||
| distshape = np.broadcast(mu, sd).shape | ||
| self.mu = mu = tt.as_tensor_variable(mu) | ||
| self.sd = sd = tt.as_tensor_variable(sd) | ||
|
|
||
| if not distshape: | ||
| distshape = np.broadcast(mu.tag.test_value, sd.tag.test_value).shape | ||
|
|
||
| super(NormalMixture, self).__init__(w, Normal.dist(mu, sd=sd, shape=distshape), | ||
| *args, **kwargs) | ||
|
|
||
|
|
||
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.
it looks like this is already called
dist_shapeingenerate_samples(instead ofdistshape)... is it easy to change that here as well?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.
I will change it to
comp_shapeto avoid confusion.