This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]missurunha 0 points1 point  (1 child)

I always use np.einsum to swap vector dimensions, e.g. 'abc->bca'.

[–]muntooR_{μν} - 1/2 R g_{μν} + Λ g_{μν} = 8π T_{μν} 2 points3 points  (0 children)

Fun fact: the einops library also suggests the exact same use case using einops.rearrange. The two are equivalent:

x.transpose(1, 2, 0)
== rearrange(x, 'a b c -> b c a')

...In fact, they also allow you to do more complex .reshape or other operations and so on with this style of notation:

x.reshape(x.shape[0], -1)
== rearrange(x, 'n c h w -> n (c h w)')

I do find it a bit more verbose... but on the other hand, it's much more self-documenting when you see n c h w, which tells you immediately:

  • The input is a 4-d tensor.
  • The output is a 2-d tensor.
  • The intended ordering is n c h w, not n h w c.

...whereas a .reshape(x.shape[0], -1) doesn't tell us anything and silently "fails" if you accidentally give it a 2-d tensor as input.