You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hello EMD is already optimized C++ and is not paralelizable directly but you can use jobli to comptue multiple emd in parallel. here is some code that was sent to me by the great @tomMoral
# %%importnumpyasnpimportotfromjoblibimportParallel, delayed# %%n=1000k=50M=np.random.rand(k, n, n)
a=np.ones(n) /n# %% loopdefemd(M, axes=None):
returnot.emd(a, a, M)
R_loop=np.zeros((k, n, n))
ot.tic()
foriinrange(k):
R_loop[i] =emd(M[i])
ot.toc()
# %% numpy take/stackdefapply_across_axis(func, M, axis=0):
returnnp.stack([
func(M.take(i, axis))
foriinrange(M.shape[axis])
], axis=axis)
ot.tic()
R_numpy=apply_across_axis(emd, M, 0)
ot.toc()
# %% joblib?defapply_across_axis_joblib(func, M, axis=0, n_jobs=4):
res=Parallel(n_jobs=n_jobs, max_nbytes=None)(
delayed(func)(M.take(i, axis))
foriinrange(M.shape[axis])
)
returnnp.stack(res, axis=axis)
R_joblib=apply_across_axis_joblib(emd, M[:4], 0)
ot.tic()
R_joblib=apply_across_axis_joblib(emd, M, 0)
ot.toc()
I think it can be easily adapted to your problem (I dont know what is going to happen with torch tensors though)
How can I leverage the code to compute the EMD btween a pair of 3D-tensor, and return a 4D-tensor as the EMD matrix?