hi,
I have a tensor with the following shape B x C x 2 x NrVerticies x 2
and now I want to do a cross product for the last dim. I found the function
torch.cross
however, this function only supports 3D vectors, so I was wondering what would be the torch way to handle the cross product in 2D.
I think you might misunderstand the docs, as the specified dim
should have the size 3 and you can use multiple dimensions as seen here:
a = torch.randn(4, 5, 6, 7, 8, 3)
b = torch.randn(4, 5, 6, 7, 8, 3)
torch.cross(a, b, dim=5)
For your example it won’t work, since your last dimension has a size of 2.
Thanks for the feedback. I understood the docs, but I wanted to use the cross product in 2D.
Finally, I ended up doing this, if someone is having the same issue.
# pad the last dim to have 3D vector
s_pad = F.pad(s, (0, 1))
# compute the cross product
A = torch.cross(s_pad[:, index_list], s_pad[:, index_list_plus], dim=2)
# use the last dim which is the same as if the cross product would be done in 2D
A = A[..., 2]