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

How do I reverse rearrange with ellipsis composed? e.g. "b ... -> b (...)" #219

Open
drscotthawley opened this issue Oct 29, 2022 · 3 comments
Labels
question Further information is requested

Comments

@drscotthawley
Copy link

drscotthawley commented Oct 29, 2022

Francois Fleuret suggested that it'd be nice if there were an function whereby einops.rearrange could be easily 'inverted' i.e. undone or "transformed back".

I replied that a "wrapper" function or class shouldn't be too hard, and wrote one at the following link, which includes a few examples:
https://gist.github.com/drscotthawley/81865a5c5e729b769486efb9c3f2249d

Whether such a functionality just remains as an "external wrapper" that users can add-on, or somehow gets added to the einops codebase (maybe not as this class, but something similar) is up to you, but wanted to share it here to add to the conversation!

One way to include it into the existing codebase could be to rename einops.rearrange to einops._rearrange and then have the new einops.rearrange =RearrangeWrapper() where RearrangeWrapper's sub-methods call _rearrange (as shown in my gist example). If that sounds interesting then I could submit a PR.

@arogozhnikov
Copy link
Owner

arogozhnikov commented Nov 8, 2022

implicit storage of information for reversal has flaws, and you already figured some of them

I've implemented this:
https://github.com/arogozhnikov/einops/blob/master/docs/4-pack-and-unpack.ipynb

  • more restricted, but addresses a scenario originally asked in that discussion.
  • can revert more than one tensor
  • inverse can have different pattern compared to forward

TLDR:

x_packed, ps = pack([x], "b *")  # assuming we want to pack e.g. b x y z -> b (x y z), ps keeps dims 
[x] = unpack(x, ps, "b *")

@lucidrains
Copy link
Contributor

@arogozhnikov love it! https://github.com/lucidrains/audiolm-pytorch/blob/main/audiolm_pytorch/hubert_kmeans.py#L46-L55

@arogozhnikov
Copy link
Owner

arogozhnikov commented Nov 11, 2022

👍 that's a good example @lucidrains

PS: now I remember I actually encountered a number of cases when I wanted to apply sklearn models to tensors not matrices.

@arogozhnikov arogozhnikov changed the title [Feature suggestion] Easy inverse for "rearrange" (with code suggestion) How do I reverse rearrange with ellipsis composed? e.g. "b ... -> b (...)" Jan 24, 2024
@arogozhnikov arogozhnikov added question Further information is requested and removed feature suggestion labels Jan 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants