-
Notifications
You must be signed in to change notification settings - Fork 569
[MRG] Fix GMM rand map overflow #872
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
Open
jonathan-legrand
wants to merge
8
commits into
PythonOT:master
Choose a base branch
from
jonathan-legrand:gmm-map-overflow
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
daaaa27
Add overflow challenge for T_rand
9172eb9
Implement logsumexp trick
e8eb2e1
Handle the no scaling case
c7f2f66
Remove comment
174d032
Require scaling factor in logsumexp
99c7556
Test logsumexp for torch and jax backends
a63c236
Compute T_mean with logsumexp
145f2ed
Merge branch 'master' into gmm-map-overflow
rflamary File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -249,6 +249,36 @@ def gmm_ot_plan(m_s, m_t, C_s, C_t, w_s, w_t, log=False): | |
| return emd(w_s, w_t, D, log=log) | ||
|
|
||
|
|
||
| def logsumexp(a, scaling_factor, axis=None): | ||
| """ | ||
| Computes log(sum(scaling_factor * exp(x))) stably using the log-sum-exp trick | ||
| with per-element weight. The backend nx.logsumexp does not allow passing | ||
| scaling weights. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| a : array-like | ||
| Log-values to sum. | ||
| scaling_factor : array-like | ||
| Weights for each term, must be of the same shape as a. | ||
|
|
||
| Returns | ||
| ------- | ||
| float | ||
| log(sum(scaling_factor * exp(a))), computed stably. | ||
|
|
||
| References | ||
| ---------- | ||
| Gundersen, G. (2020). The Log-Sum-Exp trick. Blog Post. Retrieved from https://gregorygundersen.com/blog/2020/02/09/log-sum-exp/ | ||
| """ | ||
| nx = get_backend(a, scaling_factor) | ||
| if scaling_factor is None: | ||
| scaling_factor = 1 | ||
| shift = nx.max(a) | ||
| y = shift + nx.log(nx.sum(scaling_factor * nx.exp(a - shift), axis=axis)) | ||
| return y | ||
|
|
||
|
|
||
| def gmm_ot_apply_map( | ||
| x, m_s, m_t, C_s, C_t, w_s, w_t, plan=None, method="bary", seed=None | ||
| ): | ||
|
|
@@ -322,10 +352,14 @@ def gmm_ot_apply_map( | |
|
|
||
| # gaussian mapping between components i and j applied to x | ||
| T_ij_x = x @ A + b | ||
| z = w_s[:, None, None] * nx.exp(logpdf - logpdf[i][None, :, :]) | ||
| denom = nx.sum(z, axis=0) | ||
|
|
||
| out = out + plan[i, j] * T_ij_x / denom | ||
| log_g_i_x = logpdf[i] | ||
| # Could be optimized, that's not too smart to compute denom here at each iteration | ||
| denom = logsumexp( | ||
|
Collaborator
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. please call |
||
| logpdf.squeeze(), scaling_factor=w_s.reshape((-2, 1)), axis=0 | ||
| ) | ||
| p_ij_x = plan[i, j] * nx.exp(log_g_i_x - denom.reshape((-2, 1))) | ||
| out = out + p_ij_x * T_ij_x | ||
|
|
||
| return out | ||
|
|
||
|
|
@@ -334,8 +368,8 @@ def gmm_ot_apply_map( | |
| # i and j, b[i, j] is the translation part | ||
| rng = np.random.RandomState(seed) | ||
|
|
||
| A = nx.zeros((k_s, k_t, d, d)) | ||
| b = nx.zeros((k_s, k_t, d)) | ||
| A = nx.zeros((k_s, k_t, d, d), type_as=C_s) | ||
| b = nx.zeros((k_s, k_t, d), type_as=A) | ||
|
|
||
| # only need to compute for non-zero plan entries | ||
| for i, j in zip(*nx.where(plan > 0)): | ||
|
|
@@ -354,10 +388,10 @@ def gmm_ot_apply_map( | |
|
|
||
| for i_sample in range(n_samples): | ||
| log_g = logpdf[i_sample] | ||
| log_diff = log_g[:, None] - log_g[None, :] | ||
| weighted_exp = w_s[:, None] * nx.exp(log_diff) | ||
| denom = nx.sum(weighted_exp, axis=0)[:, None] * nx.ones(plan.shape[1]) | ||
| p_mat = plan / denom | ||
| log_denom = logsumexp(log_g, scaling_factor=w_s) | ||
| p_mat = plan * nx.exp( | ||
| log_g.reshape((k_s, 1)) - log_denom | ||
| ) # shape (k_s, k_t): p_mat[i,j] = plan[i,j]*g_i(x)/D(x) | ||
|
|
||
| p = p_mat.reshape(k_s * k_t) # stack line-by-line | ||
| # sample between 0 and k_s * k_t - 1 | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 agree that the value is the same when the$\approx$
iindices are equal. We could convertzip(*nx.where(plan > 0))to a dictionary withis as keys andjs as values and have two nested loops to optimise this a little. In practice, whenk_sk_t, the redundancy is not very big does this doesn't matter too much. If you're willing to do it that's great, if not, no problem.