Calculating Eucledian Distance Between Two tensors

How do we calculate Eucledian distance between two tensors of same size. The tensors have size of [1,1, 512,1]?

Reshape it to 1 d and then find the euclidean distance.

a = torch.randn(1, 1, 512, 1)

b = troch.randn(1, 1, 512, 1)

euclidena_dist = sum(((a - b)**2).reshape(512))

This worked for me. One more question:
What if the size of the tensors are [3,1,512,1] and [1,1,512,1]?

In this case, your (1,1,512,1) shaped Tensor will copy itself to match the target dimension is (3,1,512,1), a technique known as Broadcasting.
You just need to write the code for Eucledian distance, Pytorch will perform Broadcasting inherently.
Here’s the code

A= torch.randn(1,1,512,1).reshape(-1,512)
B = torch.randn(3,1,512,1).reshape(-1,512)

Distance = ((A-B)**2).sum(axis=0)

Another way:

torch.cdist(a,b)**2