Skip to content

TransitionSpec

Describe an extra transition group to include during evaluation.

Attributes:

Name Type Description
kind Literal['pair', 'identity']

Either "pair" for contrastive transitions or "identity" for self/self transitions.

source str

Source class id.

target str

Target class id. For kind="identity", this must equal source.

symmetric bool

Whether to include both source -> target and target -> source. This is meaningful for kind="pair" and ignored for kind="identity".

kind instance-attribute

kind

source instance-attribute

source

symmetric class-attribute instance-attribute

symmetric = True

target instance-attribute

target

edges

edges()

Expand the spec into concrete directed transitions.

Returns:

Type Description
FrozenSet[TransitionEdge]

A frozenset of (source, target) tuples.

Source code in gradiend/trainer/core/transition_selection.py
def edges(self) -> FrozenSet[TransitionEdge]:
    """
    Expand the spec into concrete directed transitions.

    Returns:
        A frozenset of ``(source, target)`` tuples.
    """
    if self.kind == "identity":
        return frozenset({(self.source, self.source)})
    if self.symmetric:
        return frozenset({(self.source, self.target), (self.target, self.source)})
    return frozenset({(self.source, self.target)})

Helpers

pair

pair(source, target, *, symmetric=True)

Create a contrastive transition spec.

Parameters:

Name Type Description Default
source str

Source class id.

required
target str

Target class id.

required
symmetric bool

If True (default), include both source -> target and target -> source during evaluation.

True

Returns:

Type Description
TransitionSpec

TransitionSpec describing the pair.

Source code in gradiend/trainer/core/transition_selection.py
def pair(source: str, target: str, *, symmetric: bool = True) -> TransitionSpec:
    """
    Create a contrastive transition spec.

    Args:
        source:
            Source class id.
        target:
            Target class id.
        symmetric:
            If True (default), include both ``source -> target`` and
            ``target -> source`` during evaluation.

    Returns:
        TransitionSpec describing the pair.
    """
    return TransitionSpec(kind="pair", source=str(source), target=str(target), symmetric=bool(symmetric))

identity

identity(class_id)

Create an identity-transition spec.

Parameters:

Name Type Description Default
class_id str

Class id for the self/self transition.

required

Returns:

Type Description
TransitionSpec

TransitionSpec describing class_id -> class_id.

Source code in gradiend/trainer/core/transition_selection.py
def identity(class_id: str) -> TransitionSpec:
    """
    Create an identity-transition spec.

    Args:
        class_id:
            Class id for the self/self transition.

    Returns:
        TransitionSpec describing ``class_id -> class_id``.
    """
    cls = str(class_id)
    return TransitionSpec(kind="identity", source=cls, target=cls, symmetric=False)