artefactual.EPR#
- class artefactual.EPR(estimator=None, k=None)[source]#
Bases:
TokenProbaMixin,PretrainedMixin,SavePretrainedMixin,ClassifierMixin,BaseEstimatorScores logprob arrays with one pooled feature, the Entropy Production Rate.
Class 1 is the hallucination class: predict_proba(…)[:, 1] is the score of interest. Input is the (n, tokens, width) array LogProbParser emits, or a numeric 2-D (n, width) table read as one token per row. Targets are binary. To score completion responses, put a LogProbParser in front.
- Parameters:
estimator (estimator, default=None) – Final classifier with fit and predict_proba, never fitted in place: a clone is trained. None is the unregularised logistic regression the published detectors were fit with. A FrozenEstimator holds pretrained weights, which fit keeps.
k (int, default=None) – Candidates per token the detector accepts. None accepts any width. EPR pools to one feature whatever the width, so k is the only guard against input from another k.
- Variables:
fitted_pipeline (Pipeline) – The pipeline fit trained; absent on a detector that only carries frozen weights.
pipeline (Pipeline) – The pipeline that scores: the width check when k is set, the entropy step and the classifier. fitted_pipeline_ once fit ran, else rebuilt from a FrozenEstimator.
classes (ndarray of shape (2,)) – The class labels, sorted; column i of predict_proba is class classes_[i].
n_features_in (int) – Last dimension of the input fitted on.
- Raises:
TypeError – From fit and predict*, if given completion responses instead of logprob arrays.
ValueError – From fit and predict*, if k is set and the input has another width.
Examples
>>> detector = make_pipeline(LogProbParser(k=15), EPR(k=15)).fit( ... responses, y ... ) >>> detector = make_pipeline( ... LogProbParser(k=15), EPR.from_pretrained("artefactory/epr-phi4", k=15) ... )
Methods
cached_fileThe local .skops file identifier names, downloading it if it names a repository.
Require k, the width the loaded weights were trained at.
The classifier's decision function.
Fit a clone of estimator on the pooled features; a frozen one keeps its weights.
Wrap a loaded classifier, frozen, as a detector.
from_pretrainedAn instance carrying published weights, ready to score.
Names of the pooled features.
get_metadata_routingGet metadata routing of this object.
get_paramsGet parameters for this estimator.
load_fileRead a fitted estimator from a .skops file, refusing types not asked for.
Predicted labels.
Class probabilities; column i is classes_[i].
predict_token_probaPer-token probabilities of class 1, for locating where a response drifts.
save_pretrainedWrite this object's trained estimator to a .skops file.
scoreReturn accuracy on provided data and labels.
set_paramsSet the parameters of this estimator.
Configure whether metadata should be requested to be passed to the
scoremethod.- classmethod check_options(kwargs)[source]#
Require k, the width the loaded weights were trained at.
- Return type:
- property classes_: ndarray#
The class labels, sorted; column i of predict_proba is classes_[i].
- Raises:
AttributeError – If the detector is not fitted.
- fit(X, y)[source]#
Fit a clone of estimator on the pooled features; a frozen one keeps its weights.
- Parameters:
X (array-like of shape (n_samples, n_tokens, width) or (n_samples, width)) – Per-rank logprobs, NaN-padded in the 3-D form.
y (array-like of shape (n_samples,)) – Binary labels; the second in sorted order is class 1.
- Returns:
self
- Return type:
- classmethod from_estimator(estimator, identifier, **kwargs)[source]#
Wrap a loaded classifier, frozen, as a detector.
- Parameters:
- Returns:
A detector whose estimator is FrozenEstimator(estimator).
- Return type:
- Raises:
ValueError – If estimator is not a fitted binary classifier.
- property pipeline_: Pipeline#
The entropy step and the classifier that score.
The pipeline fit trained once it ran. A FrozenEstimator was never fitted here, so the pipeline is rebuilt from the parameters on each read, never stored.
- Raises:
AttributeError – If neither holds: the detector is not fitted.
- set_score_request(*, sample_weight: bool | None | str = '$UNCHANGED$') EPR#
Configure whether metadata should be requested to be passed to the
scoremethod.Note that this method is only relevant when this estimator is used as a sub-estimator within a meta-estimator and metadata routing is enabled with
enable_metadata_routing=True(seesklearn.set_config()). Please check the User Guide on how the routing mechanism works.The options for each parameter are:
True: metadata is requested, and passed toscoreif provided. The request is ignored if metadata is not provided.False: metadata is not requested and the meta-estimator will not pass it toscore.None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.str: metadata should be passed to the meta-estimator with this given alias instead of the original name.
The default (
sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.Added in version 1.3.