Cosine similarity between tensors of different shapes

Hi everyone,
I’m working on implementing a paper and I need to calculate the cosine similarity between a tensor of size [L,C] and a tensor of size [B,C,H,W], where B is the batch size, C the number of channels, H and W the dimension of the image. L is a parameter usually set to around 50
I have the code extract that kind of do the job but is really slow since I use for loops.

L=50

x = torch.randn(16,128,32,32) # [b,c,h,w]
b,c,h,w=x.size()
keys = torch.randn(L, 128) # 
values=torch.randn(L,128)
x=x.view(x.size(0),x.size(1),-1).permute(0,2,1)

keys = keys.unsqueeze(0).expand(x.size(0), -1, -1)
similarity_scores = torch.zeros(16, L, 1024)


for i in range(16): 
    for j in range(L):
        for k in range(1024):
            similarity_scores[i, j, k] = F.cosine_similarity(keys[i, j], x[i, k], dim=0)

It is possible to do this operation without for loops ? I cannot find a way on my own.
Thanks a lot in advance