Geometry and structural losses
Masked pairwise distances and hard distance-bin targets
AlphaFold2RoseTTAFoldRoseTTAFold2ESMFoldBoltz-2
Free account with email, Google or GitHub. Your code runs in an isolated PyTorch sandbox.
Your task
Compare every point in one set with every point in another. Return masked distances and discrete bin IDs; the reported-distance smoothing and binning rules are separate.
Function signature
pairwise_distance_encodings(
x: torch.Tensor,
y: torch.Tensor,
mask_x: torch.Tensor,
mask_y: torch.Tensor,
boundaries: torch.Tensor,
squared: bool = False,
bin_space: str = 'euclidean',
eps: float = 1e-08,
) -> tuple[torch.Tensor, torch.Tensor]Worked example
One pair, two distance conventions
Take x=[[0,0,0]], y=[[3,4,0]], both masks=[True], boundaries=[3,5,6], squared=False, bin_space="euclidean" and eps=11. The unregularized squared distance is 3²+4²=25, while the reported distance is sqrt(25+11)=6. Only boundaries 3 and 5 satisfy 25≥boundary², so bins=[[2]]. Binning the reported 6 would incorrectly pass boundary 6 as well. distances=[[6]].
Parameters
| Parameter | Shape and meaning |
|---|---|
xtorch.Tensor | [\ldots, N, 3]Cartesian coordinates of the first point set. |
ytorch.Tensor | [\ldots, M, 3]Cartesian coordinates of the second point set; shares the leading batch axes with x. |
mask_xtorch.Tensor | [\ldots, N]Bool [\ldots,N], True means valid. |
mask_ytorch.Tensor | [\ldots, M]Bool [\ldots,M], True means valid. |
boundariestorch.Tensor | [K]Nonnegative ascending bin boundaries; hard metadata, no gradient. |
squaredboolDefault False | Return squared rather than smoothed Euclidean distances when True. |
bin_spacestrDefault 'euclidean' | Interpret supplied boundaries in "euclidean" or "squared" distance space, independently of the reported-distance choice. |
epsfloatDefault 1e-08 | Positive value added inside \sqrt{d^2+\varepsilon} for the reported nonsquared distances. Distance-bin labels still use the specified unsmoothed distance domain. |
Return
tuple[torch.Tensor, torch.Tensor]
Return these tensors in the stated order.
Required behavior
- Let
x [\ldots,N,3]andy [\ldots,M,3]have identical leading batch axes, withNandMindependent. All values, including padding, are finite.Truemask_x[\ldots,N]andmask_y[\ldots,M]mean valid atoms;\mathrm{valid}(i,j)=\mathrm{mask\_x}[i]\land \mathrm{mask\_y}[j]. - Compute
\mathrm{d2\_ij}=\sum_{c} (x_{\mathrm{ic}}-y_{\mathrm{jc}})^{2}.squared(bool, defaultFalse) selects output d2, or\sqrt{\mathrm{d2}+\varepsilon}with\varepsilon>0(default10^{-8}, inside the square root). Invaliddistancesare exactly zero. - The
Kboundariesare finite, strictly increasing, nonnegative, and nondifferentiable. Configbin_spaceis 'euclidean' (default) or 'squared': compare the UNREGULARIZED d2 against\mathrm{boundaries}^{2}orboundariesrespectively. - For euclidean bin_space, count boundaries satisfying
d^2\ge b_k^2; for squared bin_space, countd^2\ge b_k. Equality belongs to the upper bin. Invalid pairs use -1; with no boundaries a valid pair has bin 0. Coincident valid points are retained, and empty point axes remain empty. - No coordinates or masks are mutated; no RBF features are requested.
Why this matters
Two singleton point axes create all pair differences; continuous lengths and hard class boundaries then use different treatments of squared distance.
Starter code
You write the body of this function in the BioTorch workspace. Your code runs in an isolated PyTorch sandbox; Run checks the public examples and Submit grades the full test suite, including gradients.
import torch
def pairwise_distance_encodings(
x: torch.Tensor, # (..., N, 3)
y: torch.Tensor, # (..., M, 3)
mask_x: torch.Tensor, # (..., N)
mask_y: torch.Tensor, # (..., M)
boundaries: torch.Tensor, # (K,)
squared: bool = False,
bin_space: str = 'euclidean',
eps: float = 1e-08,
) -> tuple[torch.Tensor, torch.Tensor]:
"""Return tuple[torch.Tensor, torch.Tensor]:
distances: (..., N, M).
bins: (..., N, M)."""
# Implement the operation. Inputs and test setup are supplied by BioTorch.
raise NotImplementedError("Implement pairwise_distance_encodings")
Ready to implement it?
3 progressive hints, a solution walkthrough, and grading that checks values, shapes and gradients are waiting in the workspace.
Free account with email, Google or GitHub. Your code runs in an isolated PyTorch sandbox.
Sources and further reading
A primitive exercise, not an end-to-end reproduction of the named models. No affiliation or endorsement is implied.