# Multiply a tensor to a list of list of tensors of different sizes efficiently

I am looking for a way to perform operations on the following two structures efficiently:

I have a tensor of size (32, 40, 7). I also have a list of list of tensors with the outer list of length 32 and each inner list of length 40. Within the inner list, it has a tensor of (n, 7) where n can be different for each inner list.

For each (1, 1, 7) vector in the (32, 40, 7), I would like to perform certain operations with its corresponding (n, 7) tensor (e.g. let’s say dot product to each row).

What would be an efficient way to do that? Right now I am simply looping over the tensor which is highly inefficient. Sorry if I miss any obvious solution to this and thank you very much for your help in advance

Hi. I believe you’re looking for `torch.bmm` or the batch matrix multiplication. An example is,

``````In : import torch

In : t1 = torch.rand((32, 40, 7))

In : t2 = torch.rand((32, 80, 7))

In : bm = torch.bmm(t1, t2.transpose(2,1))

In : bm.shape
Out: torch.Size([32, 40, 80])

In :
``````

Hope that helps