how to compute l-inf distance between two tensors with pytorch
- User
how to compute l-inf distance between two tensors with pytorch
Assistant
In PyTorch, you can compute the L-infinity (L∞) distance between two tensors using the
torch.max()function combined with absolute difference. The L∞ distance is the maximum absolute difference between corresponding elements:import torch def l_inf_distance(tensor1, tensor2): return torch.max(torch.abs(tensor1 - tensor2))For example:
# Example usage a = torch.tensor([1.0, 2.0, 3.0]) b = torch.tensor([1.5, 1.8, 4.0]) distance = l_inf_distance(a, b) print(distance) # Output: tensor(1.)Would you like me to explain or break down this code?
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.