Skip to content

load_saved_cross_encoding_inputs

Read the inputs of the oriented cross-encoding matrix from saved run folders.

Needs neither trainers nor data: every run folder <root>/<run_id>/ holds the saved model (model/) and its cached cross-task encoder evaluation (encoded_values_*_split_<split>.csv), as written by plot_cross_encoding_heatmap and published with the multilingual GRADIEND models.

Parameters:

Name Type Description Default
root str

Folder with one subfolder per run.

required
run_ids Optional[Sequence[str]]

Runs to load. By default every subfolder with a saved model.

None
split str

Split of the cached evaluation ("test").

'test'

Returns:

Type Description
Dict[str, Tuple[str, str]]

(pair_by_id, source_by_id, encoder_summary), the arguments of

Dict[str, str]

compute_anchor_aligned_encoding_matrix.

Raises:

Type Description
FileNotFoundError

If a run has no saved model or no cached evaluation.

ValueError

If a run does not encode exactly two classes or has several cached evaluations.

Source code in gradiend/comparison/cross_encoding.py
def load_saved_cross_encoding_inputs(
    root: str,
    run_ids: Optional[Sequence[str]] = None,
    *,
    split: str = "test",
) -> Tuple[Dict[str, Tuple[str, str]], Dict[str, str], Dict[str, Any]]:
    """Read the inputs of the oriented cross-encoding matrix from saved run folders.

    Needs neither trainers nor data: every run folder ``<root>/<run_id>/`` holds the saved model
    (``model/``) and its cached cross-task encoder evaluation (``encoded_values_*_split_<split>.csv``),
    as written by ``plot_cross_encoding_heatmap`` and published with the multilingual GRADIEND models.

    Args:
        root: Folder with one subfolder per run.
        run_ids: Runs to load. By default every subfolder with a saved model.
        split: Split of the cached evaluation (``"test"``).

    Returns:
        ``(pair_by_id, source_by_id, encoder_summary)``, the arguments of
        ``compute_anchor_aligned_encoding_matrix``.

    Raises:
        FileNotFoundError: If a run has no saved model or no cached evaluation.
        ValueError: If a run does not encode exactly two classes or has several cached evaluations.
    """
    from gradiend.model.utils import read_gradiend_context
    from gradiend.util.paths import has_saved_model

    root = os.fspath(root)
    if run_ids is None:
        run_ids = sorted(
            name for name in os.listdir(root) if has_saved_model(os.path.join(root, name, "model"))
        )
    pair_by_id: Dict[str, Tuple[str, str]] = {}
    source_by_id: Dict[str, str] = {}
    encoder_summary: Dict[str, Any] = {}
    for run_id in run_ids:
        run_dir = os.path.join(root, run_id)
        source, _, direction = read_gradiend_context(os.path.join(run_dir, "model"))
        if not direction or len(direction) != 2:
            raise ValueError(f"Run {run_id!r} must encode exactly two classes, got {direction!r}")
        caches = sorted(glob.glob(os.path.join(run_dir, f"encoded_values*_split_{split}.csv")))
        if not caches:
            raise FileNotFoundError(f"Run {run_id!r} has no cached encoder evaluation for split {split!r} in {run_dir}")
        if len(caches) > 1:
            raise ValueError(f"Run {run_id!r} has several cached encoder evaluations for split {split!r}: {caches}")
        pair_by_id[run_id] = tuple(direction)
        source_by_id[run_id] = source
        encoder_summary[run_id] = {"encoder_df": pd.read_csv(caches[0])}
    return pair_by_id, source_by_id, encoder_summary