artefactual.EPR#

class artefactual.EPR(estimator=None, k=None)[source]#

Bases: TokenProbaMixin, PretrainedMixin, SavePretrainedMixin, ClassifierMixin, BaseEstimator

Scores 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_file

The local .skops file identifier names, downloading it if it names a repository.

check_options

Require k, the width the loaded weights were trained at.

decision_function

The classifier's decision function.

fit

Fit a clone of estimator on the pooled features; a frozen one keeps its weights.

from_estimator

Wrap a loaded classifier, frozen, as a detector.

from_pretrained

An instance carrying published weights, ready to score.

get_feature_names_out

Names of the pooled features.

get_metadata_routing

Get metadata routing of this object.

get_params

Get parameters for this estimator.

load_file

Read a fitted estimator from a .skops file, refusing types not asked for.

predict

Predicted labels.

predict_proba

Class probabilities; column i is classes_[i].

predict_token_proba

Per-token probabilities of class 1, for locating where a response drifts.

save_pretrained

Write this object's trained estimator to a .skops file.

score

Return accuracy on provided data and labels.

set_params

Set the parameters of this estimator.

set_score_request

Configure whether metadata should be requested to be passed to the score method.

classmethod check_options(kwargs)[source]#

Require k, the width the loaded weights were trained at.

Return type:

None

property classes_: ndarray#

The class labels, sorted; column i of predict_proba is classes_[i].

Raises:

AttributeError – If the detector is not fitted.

decision_function(X)[source]#

The classifier’s decision function.

Return type:

ndarray

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:

EPR

classmethod from_estimator(estimator, identifier, **kwargs)[source]#

Wrap a loaded classifier, frozen, as a detector.

Parameters:
  • estimator (estimator) – A fitted binary classifier.

  • identifier (str or Path) – What named it, for error messages.

  • **kwargs (Any) – Passed to the constructor.

Returns:

A detector whose estimator is FrozenEstimator(estimator).

Return type:

EPR

Raises:

ValueError – If estimator is not a fitted binary classifier.

get_feature_names_out(input_features=None)[source]#

Names of the pooled features.

Return type:

ndarray

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.

predict(X)[source]#

Predicted labels.

Return type:

ndarray

predict_proba(X)[source]#

Class probabilities; column i is classes_[i].

Return type:

ndarray

set_score_request(*, sample_weight: bool | None | str = '$UNCHANGED$') → EPR#

Configure whether metadata should be requested to be passed to the score method.

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 (see sklearn.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 to score if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to score.

  • 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.

Parameters:

sample_weight (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for sample_weight parameter in score.

Returns:

self – The updated object.

Return type:

object