Skip to content

Score units

A score step may declare that its contribution is a sum of one log weight per unit. A trace of the program then holds a SiteUnits at the score's site: the log weight of each unit, the unit that each row of a response belongs to, and the log likelihood of each row where the rows factor. UnfactoredObservationError says why a response has no log likelihood per row and which program has one.

units

The units of a score, read from one run.

A score step may declare that its contribution is a sum of one log weight per unit (see UnitFactorization). SiteUnits is that declaration with the values of one run: it reads the log weight of each unit, the unit of each row of a response, and, where the rows factor, the log likelihood of each row.

UnfactoredObservationError

Bases: TypeError

An observation's rows have no log likelihoods of their own.

The message says why the rows do not factor, and which program has a log likelihood per row.

SiteUnits didactic-model

SiteUnits(*, factorization: UnitFactorization = field(opaque=True, extras=OPAQUE_BY_VALUE), environment: dict[str, Tensor] = field(opaque=True, extras=OPAQUE_BY_IDENTITY), occurrence: str = '')

Bases: ValueModel

The units of one score site, at the values of one run.

PARAMETER DESCRIPTION
factorization

What the score step declares.

TYPE: UnitFactorization DEFAULT: field(opaque=True, extras=OPAQUE_BY_VALUE)

environment

The run's values the factorization reads.

TYPE: dict[str, Tensor] DEFAULT: field(opaque=True, extras=OPAQUE_BY_IDENTITY)

occurrence

The suffix of the site's occurrence, "@<n>" for a score a scan reaches once per position, else empty.

TYPE: str DEFAULT: ''

axes property

axes: tuple[str, ...]

The objects whose product indexes the units.

RETURNS DESCRIPTION
tuple[str, ...]

The names, outermost first; empty for a score with one unit.

sizes property

sizes: tuple[int, ...]

The extent of each unit axis.

RETURNS DESCRIPTION
tuple[int, ...]

The extents.

responses property

responses: tuple[str, ...]

The labels of the responses whose rows the units hold.

RETURNS DESCRIPTION
tuple[str, ...]

The labels, in the order the program observes them.

log_weights

log_weights() -> Tensor

The log weight of each unit, as the site scored it.

RETURNS DESCRIPTION
Tensor

The weights, of shape sizes. They sum to the score the step contributes, before any score transformer on the stack.

Source code in src/quivers/effects/units.py
def log_weights(self) -> torch.Tensor:
    """The log weight of each unit, as the site scored it.

    Returns
    -------
    torch.Tensor
        The weights, of shape `sizes`. They sum to the score the step
        contributes, before any score transformer on the stack.
    """
    return self.factorization.weights(self.environment).reshape(self.sizes)

log_likelihood

log_likelihood() -> Tensor

The log density of each unit's rows given the run's latents.

RETURNS DESCRIPTION
Tensor

The log densities, of shape sizes, without likelihood powers.

RAISES DESCRIPTION
UnfactoredObservationError

If the score's weights are not log densities.

Source code in src/quivers/effects/units.py
def log_likelihood(self) -> torch.Tensor:
    """The log density of each unit's rows given the run's latents.

    Returns
    -------
    torch.Tensor
        The log densities, of shape `sizes`, without likelihood powers.

    Raises
    ------
    UnfactoredObservationError
        If the score's weights are not log densities.
    """
    self._require_density()
    return self.factorization.raw_weights(self.environment).reshape(self.sizes)

row_units

row_units(response: str) -> Tensor

The unit each row of a response belongs to.

PARAMETER DESCRIPTION
response

The response's label.

TYPE: str

RETURNS DESCRIPTION
Tensor

(rows,), counted row-major over the unit axes.

RAISES DESCRIPTION
KeyError

If the score reads no such response.

Source code in src/quivers/effects/units.py
def row_units(self, response: str) -> torch.Tensor:
    """The unit each row of a response belongs to.

    Parameters
    ----------
    response : str
        The response's label.

    Returns
    -------
    torch.Tensor
        ``(rows,)``, counted row-major over the unit axes.

    Raises
    ------
    KeyError
        If the score reads no such response.
    """
    for label, item in zip(self.responses, self.factorization.rows, strict=True):
        if label == response:
            return item.units(self.environment)
    raise KeyError(response)

row_log_likelihood

row_log_likelihood(response: str) -> Tensor

The log likelihood of each row of a response, where it exists.

A row has one when its unit holds that row and no other row of any response of the score. The log likelihood of the row is then the log weight of its unit.

PARAMETER DESCRIPTION
response

The response's label.

TYPE: str

RETURNS DESCRIPTION
Tensor

(rows,), without likelihood powers.

RAISES DESCRIPTION
KeyError

If the score reads no such response.

UnfactoredObservationError

If a unit holds several rows, or the score's weights are not log densities.

Source code in src/quivers/effects/units.py
def row_log_likelihood(self, response: str) -> torch.Tensor:
    """The log likelihood of each row of a response, where it exists.

    A row has one when its unit holds that row and no other row of any
    response of the score. The log likelihood of the row is then the
    log weight of its unit.

    Parameters
    ----------
    response : str
        The response's label.

    Returns
    -------
    torch.Tensor
        ``(rows,)``, without likelihood powers.

    Raises
    ------
    KeyError
        If the score reads no such response.
    UnfactoredObservationError
        If a unit holds several rows, or the score's weights are not
        log densities.
    """
    self._require_density()
    rows = self.row_units(response)
    units = max(1, int(torch.tensor(self.sizes).prod()) if self.sizes else 1)
    counts = torch.zeros(units, dtype=torch.long, device=rows.device)
    for label in self.responses:
        held = self.row_units(label)
        counts = counts + torch.bincount(held, minlength=units)
    shared = counts[rows] > 1
    if bool(shared.any()):
        raise UnfactoredObservationError(
            self._refusal(response, rows, counts, int(rows[shared][0]))
        )
    return self.log_likelihood().reshape(-1)[rows]