I have two tensors, shapes are shown below.
input1 = torch.randn(1, 3, 10, 8) 
input2 = torch.randn(1, 1, 128, 8)

I would like to do following operation:
cos = nn.CosineSimilarity(dim=2, eps=1e-6)
output = cos(input1, input2)

But I get an error saying that dim 2  has to be the same
Therefore, I had to use for loop as an alternative solution as below
cos = nn.CosineSimilarity(dim=2, eps=1e-6)
output = []
for idx in range(0,10):
     output.append(cos(input1.[:, :, idx, :].unsqueeze(0).unsqueeze(1), input2))

This solution works fine. But is it possible to do the same operation without using for loop?
Any hints are helpful.
Thanks!