Skip to main content

likelihoods.from_log_prob

likelihoods.from_log_prob(fn, /, mask_fn=None)

Wrap a per-example log-likelihood fn(f, y) (contrastive, ranking, a custom energy).

Losses that couple examples (pairwise or listwise across the batch) don’t fit this contract.

Parameters

Name Type Description Default
fn Callable[[Outputs, Targets], Tensor] fn(f, y) returns \(\log p(y \mid f)\) shaped [N] or [N, *positions]. required
mask_fn Callable[[Outputs, Inputs], Tensor | None] | None mask_fn(f, inputs) returns a bool tensor shaped like fn’s output, False at positions that don’t count (padding, say), or None when every position counts. Without it, every position counts. None

Returns

Name Type Description
LogProb The likelihood, a LogProb. It has only log_prob and mask.