Genomics and RNA

Stable masked Poisson objective for genomic counts

MediumRaw PyTorchAbout 20 min2 public examples plus private tests

AlphaGenomeBorzoi

Write and run your solution Sign in

Free account with email, Google or GitHub. Your code runs in an isolated PyTorch sandbox.

Your task

Compute a Poisson objective for observed genomic counts. Apply the additive rate stabilizer and masks, then return the requested global reduction and per-track diagnostic.

Function signature

masked_poisson_deviance(
    counts: torch.Tensor,
    rate: torch.Tensor,
    mask: torch.Tensor,
    objective: str = 'deviance',
    reduction: str = 'mean',
    eps: float = 1e-06,
) -> tuple[torch.Tensor, torch.Tensor]

Worked example

Zero-count and perfectly predicted entries

Adding \varepsilon=0.125 gives predicted rates [[1,2],[3,4]]. Counts2 and3 match their rates, so their deviance is zero. The zero counts contribute twice the rate: 2 and8. The global mean is (2+0+0+8)/4=2.5, and the two track means are [1,4].

import torch

counts = torch.tensor([[[0.0, 2.0], [3.0, 0.0]]], dtype=torch.float64)
rate = torch.tensor([[[0.875, 1.875], [2.875, 3.875]]], dtype=torch.float64, requires_grad=True)
mask = torch.tensor([[[True, True], [True, True]]], dtype=torch.bool)
objective = 'deviance'
reduction = 'mean'
eps = 0.125

result = masked_poisson_deviance(counts=counts, rate=rate, mask=mask, objective=objective, reduction=reduction, eps=eps)

Expected return

(torch.tensor(2.5, dtype=torch.float64), torch.tensor([1.0, 4.0], dtype=torch.float64))

Parameters

ParameterShape and meaning
countstorch.Tensor[B, L, T]Observed nonnegative genomic counts.
ratetorch.Tensor[B, L, T]Predicted nonnegative count rates before the additive eps stabilizer.
masktorch.TensorBroadcastable to [B, L, T]bool broadcastable to [B,L,T], True means observed
objectivestrDefault 'deviance'Choose "deviance" for the specified Poisson deviance objective or "nll" for negative log likelihood without the target-only log-factorial constant.
reductionstrDefault 'mean'Choose a global "sum" or "mean" over observed entries. Per-track reductions use the same choice on that track’s observed entries.
epsfloatDefault 1e-06Strictly positive offset added once to the supplied rate before every likelihood/deviance calculation: effective_mean = rate + eps.

Return

tuple[torch.Tensor, torch.Tensor]

Return these tensors in the stated order.

Required behavior

  • Compute a masked Poisson objective and a per-track reduction with stable zero-count behavior. counts and rate have identical shape [B,L,T], with B,T>0 and L\ge 0. Floating inputs share dtype/device (float32 or float64); reductions preserve that dtype. mask is bool and broadcastable to [B,L,T], with True means observed.
  • For example use [B,L,1] for a positional mask, [1,1,T] for a track mask, or a scalar bool. At observed positions \mathrm{counts}\ge 0 and \mathrm{rate}\ge 0 must be finite. Missing payloads may be arbitrary, including negative values, NaN, or infinity: replace them before any logarithm/arithmetic.
  • Do not infer missingness from a count of zero. options include eps, written as \varepsilon (positive float, default 10^{-6}), objective ('deviance', default, or 'nll'), and reduction ('mean', default, or 'sum'). The effective mean is \mu=\mathrm{rate}+\varepsilon; \varepsilon is additive, not a clamp and not added to counts.
  • For each observed entry compute either deviance: d=2\cdot (\mu-\mathrm{counts}+\mathrm{counts}\cdot (\operatorname{log}(\mathrm{counts})-\operatorname{log}(\mu))), nll: d=\mu-\mathrm{counts}\cdot \operatorname{log}(\mu). Define \mathrm{counts}\cdot \operatorname{log}(\mathrm{counts})=0 when \mathrm{counts}=0.
  • Make the log-count argument safe before selecting a branch, so even unused branches have finite derivatives. The nll option omits the target-only \operatorname{log}(\mathrm{counts}!) constant; deviance includes the conventional factor 2 and target-optimal subtraction, not a source-specific half/shifted deviance.
  • Missing entries contribute zero, not \varepsilon. Return loss, a scalar reduction over B,L,T, and per_track [T], reducing over B,L for each track.
  • For mean, divide each sum by its own number of valid elements after broadcasting the mask. Global loss is not the unweighted mean of per-track means. Any empty/all-masked reduction is exactly zero (denominator at least 1), including L=0. No residual, regularization, or learned weights are added.
  • A zero-count observed entry still contributes \mu (nll) or 2\cdot \mu (deviance). Negative observed targets are outside the input contract.

Why this matters

Poisson deviance compares a predicted rate with the best rate for that observation; a zero count still carries information about an excessive prediction.

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 masked_poisson_deviance(
    counts: torch.Tensor,  # (B, L, T)
    rate: torch.Tensor,  # (B, L, T)
    mask: torch.Tensor,  # Broadcastable to (B, L, T)
    objective: str = 'deviance',
    reduction: str = 'mean',
    eps: float = 1e-06,
) -> tuple[torch.Tensor, torch.Tensor]:
    """Return tuple[torch.Tensor, torch.Tensor]:
    
    loss: Scalar tensor, shape ().
    per_track: (T,)."""
    # Implement the operation. Inputs and test setup are supplied by BioTorch.
    raise NotImplementedError("Implement masked_poisson_deviance")

Ready to implement it?

3 progressive hints, a solution walkthrough, and grading that checks values, shapes and gradients are waiting in the workspace.

Write and run your solution Sign in

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.