PairwiseDistance

https://pytorch.org/docs/stable/generated/torch.nn.PairwiseDistance.html

pairwise_distance_demo.py
import svetoviz_webgpu as sv

# 1. Define Distance Parameters
# p=2.0: Euclidean Distance (standard straight-line)
p_norm = 2.0
dist_layer = nn.PairwiseDistance(p=p_norm, eps=1e-6)

# 2. Prepare Input Tensors
input1 = torch.randn(1, 128)
# Point B: Very close to A
input2 = input1 + (torch.randn(1, 128) * 0.05)
# Point C: Far from A
input3 = torch.randn(1, 128) * 5.0

def terminal_callback(buffer, message, images, files):
    # 3. Calculate distances
    dist_close = dist_layer(input1, input2)
    dist_far = dist_layer(input1, input3)

    buffer.send_system_message(f"Distance Metric: L{int(p_norm)} Norm")
    buffer.send_system_message(f"Distance (Near): {dist_close.item():.4f}")
    buffer.send_system_message(f"Distance (Far): {dist_far.item():.4f}")

# 4. Start the interactive session
sv.pytorch_web(module=dist_layer, terminal_callback=terminal_callback)
Euclidean vs Manhattan Metrics
PairwiseDistance activations
PairwiseDistance computes the L^p distance between vectors. Unlike Cosine Similarity, this metric is sensitive to magnitude, measuring the actual spatial gap between points in the manifold.