forked from ccf-ai-infra/GPUCodeForces
46 lines
1.2 KiB
Python
46 lines
1.2 KiB
Python
import torch
|
|
import torch.nn as nn
|
|
|
|
class Model(nn.Module):
|
|
"""
|
|
Euclidean Distance implementation.
|
|
Computes the Euclidean distance between two sets of vectors.
|
|
"""
|
|
def __init__(self):
|
|
super(Model, self).__init__()
|
|
|
|
def forward(self, x: torch.Tensor, y: torch.Tensor) -> torch.Tensor:
|
|
"""
|
|
Compute Euclidean distance between x and y.
|
|
|
|
Args:
|
|
x (torch.Tensor): First set of vectors [batch_size, feature_dim]
|
|
y (torch.Tensor): Second set of vectors [batch_size, feature_dim]
|
|
|
|
Returns:
|
|
torch.Tensor: Euclidean distances [batch_size]
|
|
"""
|
|
# Compute squared differences
|
|
diff = x - y
|
|
squared_diff = diff * diff
|
|
|
|
# Sum along feature dimension
|
|
sum_squared = torch.sum(squared_diff, dim=1)
|
|
|
|
# Take square root
|
|
distances = torch.sqrt(sum_squared)
|
|
|
|
return distances
|
|
|
|
batch_size = 256
|
|
feature_dim = 512
|
|
|
|
def get_inputs():
|
|
# Generate two sets of vectors
|
|
x = torch.randn(batch_size, feature_dim)
|
|
y = torch.randn(batch_size, feature_dim)
|
|
return [x, y]
|
|
|
|
def get_init_inputs():
|
|
return [] # No special initialization inputs needed
|