Encoder gate

retrain.encoder.EncoderSpec

Recipe for one encoder gate arm: the base model plus its fine-tune and calibration knobs.

Usage

Source

retrain.encoder.EncoderSpec(
    model_id,
    max_length=512,
    epochs=3.0,
    lr=2e-05,
    batch_size=16,
    seed=1729,
    weight_decay=0.01,
    warmup_ratio=0.06,
    val_frac=0.1,
    calibrate=True
)

val_frac and calibrate are the calibration knobs: a stratified val_frac slice is held out of training to fit the temperature (skipped, leaving T = 1, when calibrate is False). The specific bake-off ids are experiment-local; PRESETS carries a couple of representative recipes.

Parameter Attributes

model_id: str
max_length: int = 512
epochs: float = 3.0
lr: float = 2e-05
batch_size: int = 16
seed: int = 1729
weight_decay: float = 0.01
warmup_ratio: float = 0.06
val_frac: float = 0.1
calibrate: bool = True

Example

>>> EncoderSpec(model_id="bert-base-uncased", epochs=3.0, lr=2e-5)

retrain.encoder.EncoderModel

A fine-tuned encoder head plus its fitted temperature and held-out calibration error.

Usage

Source

retrain.encoder.EncoderModel(model, tokenizer, spec, temperature, val_ece)

probs() applies the temperature to the model’s fire-margin logits, so its output is the calibrated P(fire) that score_frozen() persists.

Parameter Attributes

model: PreTrainedModel
tokenizer: PreTrainedTokenizerBase
spec: EncoderSpec
temperature: float
val_ece: float

retrain.encoder.train_encoder()

Fine-tune the encoder head on train_frame, temperature-scaled on a held-out val carve.

Usage

Source

retrain.encoder.train_encoder(spec, train_frame, *, output_dir=None)

Loads spec.model_id as a two-label sequence classifier, trains it under spec’s knobs, then fits the temperature on a stratified spec.val_frac carve — the lexical lane’s calibration idiom — and stamps the val expected calibration error onto the artifact. Uses the GPU only when CUDA is present, else CPU (never MPS), so a fine-tune is reproducible in spec.seed on the eval hosts.

Returns

EncoderModel
The fine-tuned EncoderModel carrying the fitted temperature and val ECE.

retrain.encoder.score_frozen()

Score the frozen gate eval and persist the calibrated P(fire) through write_probs.

Usage

Source

retrain.encoder.score_frozen(
    model, frame, *, version, render=evalset.RENDER_VERSION, root=None
)

The per-row probabilities and their fire AUC land through ~cc_steer.retrain.evalset.write_probs(), render-tagged and stamped with the frame digest, so the encoder arm is paired-comparable with the lexical gate and the incumbent on the same frame.

Parameters

model: EncoderModel

The fine-tuned, temperature-scaled encoder.

frame: EncoderFrame

The frozen gate eval frame to score.

version: str

The registry version label the stored probs are keyed under.

render: int = evalset.RENDER_VERSION

The render version stamped into the probs store.

root: Path | None = None
Eval root override; defaults to ~/.cc-steer/eval.

Returns

Path
The path the per-row probabilities were written to.