Geometry and structural losses
Geometric confidence targets and observation masks
Free account with email, Google or GitHub. Your code runs in an isolated PyTorch sandbox.
Your task
Build structure-confidence supervision from predicted and observed geometry. Return distance/error bins, local lDDT labels, resolution labels, and a separate validity mask for each target.
Function signature
structure_confidence_targets(
activations: dict,
masks: dict,
plddt_bins: int = 50,
lddt_radius: float = 15.0,
lddt_eps: float = 1e-10,
) -> dict[str, torch.Tensor]Worked example
Distance error and aligned error answer different questions
For the pair-target stage, use observed target points (0,0,0),(3,0,0) and predicted points (0,0,0),(4,0,0), with all point/frame masks and resolution eligibility true. Frame origins equal their corresponding points. All rotations are identity except predicted frame 0, which is a positive quarter-turn about z. The off-diagonal target distance squared is 9 and distance-error squared is (4-3)²=1 in both directions. In frame 0, predicted point 1 has local coordinates (0,-4,0) while target point 1 has (3,0,0), so aligned-error squared is 25. In frame 1, the corresponding point-0 locals are (-4,0,0) and (-3,0,0), giving aligned-error squared 1. With distogram_edges=[2,3] and error_edges=[1,4], the off-diagonal distance-bin labels are 1, PDE labels are 0, and directed PAE labels are 2 and 0. Equality with an edge stays in the lower bin.
Parameters
| Parameter | Shape and meaning |
|---|---|
activationsdict | Supplied intermediate tensors. |
masksdict | Supplied masks. Each field below defines what True includes or permits. |
plddt_binsintDefault 50 | Number of local-confidence bins. |
lddt_radiusfloatDefault 15.0 | Target-distance neighborhood radius. |
lddt_epsfloatDefault 1e-10 | Positive offset added to both the hard-lDDT score numerator and neighbor-count denominator. A valid point with no neighbors therefore has score one; an invalid point remains zero. |
Return
dict[str, torch.Tensor]
Return exactly the named fields applicable to this operation.
Required behavior
- Construct geometric confidence targets on
B\ge 1batches andN\ge 0supplied representative points. Coordinates are in angstroms.R [b,i]is a3x3 local-to-global rotation whose columns are local basis vectors;t [b,i]is its origin. Frames are supplied, not reconstructed. All boolean masks use\mathrm{true}=\mathrm{valid}. - Let
e [b]beresolution_eligible: a supplied experiment/training-policy gate.o=\mathrm{exists}ANDresolvedANDe;v=oANDpred_mask;f=\mathrm{frame\_mask}ANDe. Frame validity must already incorporate the atom availability needed to construct both predicted and target frames. - Target coordinates outside
o, predicted coordinates outsidev, and frames outsidefmay contain arbitrary nonfinite padding and must be sanitized before arithmetic. Parameters/valid coordinates are finite. Model-specific representative-atom choices (CA, pseudo-\beta, token atoms, all atoms) are made before this function. - For ordered pairs
i,jcomputeD2_ij=\Vert \mathrm{target\_i}-\mathrm{target\_j}\Vert^2; predicted distancesd_ij=\Vert \mathrm{pred\_i}-\mathrm{pred\_j}\Vert; target distancest_{\mathrm{ij}}=\sqrt{\mathrm{D2\_ij}}. Distogram mask iso_iANDo_j. - PDE mask is
v_iANDv_j, with squared distance-error\mathrm{E2\_ij}=(d_{\mathrm{ij}}-t_{\mathrm{ij}})^{2}. PAE mask isf_iANDv_j:A2_ij=\Vert \mathrm{Rpred\_i}^{\top}(\mathrm{pred\_j}-\mathrm{tpred\_i})-\mathrm{Rtarget\_i}^{\top}(\mathrm{target\_j}-\mathrm{ttarget\_i})\Vert^2. - Axis
ichooses the alignment frame andjchooses the point. Return all three squared continuous diagnostics, zero outside their individual masks. PAE generally is not symmetric. Distogram/PDE are symmetric. Self pairs are included for distance/error bin targets. - Quantize distances/errors using strictly increasing nonnegative angstrom edges:
\mathrm{label}(\mathrm{q2},\mathrm{edges})=\sum_{k}[\mathrm{q2}>\mathrm{edges\_k}^{2}]. Equality stays in the LOWER bin (strict '>'); this differs from bucketizers using '\ge'. distogram_binsuses D2 withdistogram_edges;pde_binsandpae_binsuse\mathrm{E2}/\mathrm{A2}witherror_edges. Empty edge vectors give one catch-all bin. Every invalid label is-1and its returned mask is false.- Construct local hard lDDT with neighbors
M_{\mathrm{ij}}=v_{i}ANDv_jANDi\ne jANDt_{\mathrm{ij}}<\mathrm{lddt\_radius}. A pair contributesmean_k[|d_{\mathrm{ij}}-t_{\mathrm{ij}}|<\mathrm{lddt\_thresholds\_k}]. - Then
\mathrm{local\_lddt\_i}=(\mathrm{lddt\_eps}+\sum_{j} M_{\mathrm{ij}}\cdot \mathrm{pair\_score\_ij})/(\mathrm{lddt\_eps}+\sum_{j} M_{\mathrm{ij}}). Invalidv_ireturns zero. A valid point with no neighbors returns one, matching the explicit\epsilon-over-\epsilonconvention; it is not evidence of a reliable isolated prediction. \mathrm{plddt\_bins}=\operatorname{min}(\operatorname{floor}(\mathrm{local\_lddt}\cdot \mathrm{plddt\_bins}),\mathrm{plddt\_bins}-1), or-1when invalid. Threshold comparisons are strict; no smooth approximation is used.- Finally
\mathrm{resolved\_mask}=\mathrm{exists}ANDeand\mathrm{resolved\_bins}=1for experimentallyresolved,0for unresolved existing sites,-1outside that mask. An unresolved but chemically existing atom is a valid NEGATIVE resolution target, while absent chemistry is not a target.resolvedmust be a subset ofexists. - Do not confuse these masks with the coordinate-supervision masks. Config:
\mathrm{plddt\_bins}=50is a positive integer;\mathrm{lddt\_radius}=15.0angstroms and\mathrm{lddt\_eps}=10^{-10}are positive.lddt_thresholdsis a nonempty positive float vector. - Invalid configured counts/radius/
\epsilonor empty thresholds raiseValueError; other shape/domain violations are outside the contract. EmptyNand wholly ineligible/missing structures return empty/zero diagnostics,-1invalid labels and false masks. - The diagnostics use exact Euclidean lengths, with zero defined at coincidence; gradient fixtures avoid coincident DISTINCT valid points. This does not produce a differentiable lDDT loss or backpropagate through hard labels.
Why this matters
Training labels depend on what was actually observed. Distance labels compare point pairs; aligned-error labels compare a point in two local frames; lDDT labels count how often neighboring distances meet strict accuracy thresholds.
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 structure_confidence_targets(
activations: dict,
masks: dict,
plddt_bins: int = 50,
lddt_radius: float = 15.0,
lddt_eps: float = 1e-10,
) -> dict[str, torch.Tensor]:
"""Return dict[str, torch.Tensor]:
target_distance_squared: (B, N, N).
distance_error_squared: (B, N, N).
aligned_error_squared: (B, N, N).
distogram_bins: (B, N, N).
pde_bins: (B, N, N).
pae_bins: (B, N, N).
local_lddt: (B, N).
plddt_bins: (B, N).
resolved_bins: (B, N).
distogram_mask: (B, N, N).
pde_mask: (B, N, N).
pae_mask: (B, N, N).
plddt_mask: (B, N).
resolved_mask: (B, N)."""
# Implement the operation. Inputs and test setup are supplied by BioTorch.
raise NotImplementedError("Implement structure_confidence_targets")
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.