Let’s go through all operations separately in the calls:


toch.cdist(a, b, p) calculates the p-norm distance between each pair of the two collections of row vectos, as explained above

.squeeze() will remove all dimensions of the result tensor where tensor.size(dim) == 1


.transpose(0, 1) will permute dim0 and dim1, i.e. it’ll “swap” these dimensions

torch.unsqueeze(tensor, dim) will add a new dimension specific by dim
 expand() will manipulate the meta data to create a view with the new shape (no copy of the data)
 permuteis similar totranspose` for multiple dimensions