autointent.modules.scoring.KNNScorer#
- class autointent.modules.scoring.KNNScorer(k, embedder_config=None, weights='distance')#
Bases:
autointent.modules.base.BaseScorer
K-nearest neighbors (KNN) scorer for intent classification.
This module uses a vector index to retrieve nearest neighbors for query utterances and applies a weighting strategy to compute class probabilities.
- Parameters:
embedder_config (autointent.configs.EmbedderConfig | str | dict[str, Any] | None) – Config of the embedder used for vectorization
k (pydantic.PositiveInt) – Number of closest neighbors to consider during inference
weights (autointent.custom_types.WeightType) –
Weighting strategy:
”uniform”: Equal weight for all neighbors
”distance”: Weight inversely proportional to distance
”closest”: Only the closest neighbor of each class is weighted
Examples:#
from autointent.modules.scoring import KNNScorer utterances = ["hello", "how are you?"] labels = [0, 1] scorer = KNNScorer( embedder_config="sergeyzh/rubert-tiny-turbo", k=5, ) scorer.fit(utterances, labels) test_utterances = ["hi", "what's up?"] probabilities = scorer.predict(test_utterances)
- name = 'knn'#
Name of the module.
- supports_multilabel = True#
Whether the module supports multilabel classification
- supports_multiclass = True#
Whether the module supports multiclass classification
- embedder_config#
- k#
- weights = 'distance'#
- classmethod from_context(context, k, weights='distance', embedder_config=None)#
Create a KNNScorer instance using a Context object.
- Parameters:
context (autointent.Context) – Context containing configurations and utilities
k (pydantic.PositiveInt) – Number of closest neighbors to consider during inference
weights (autointent.custom_types.WeightType) – Weighting strategy for scoring
embedder_config (autointent.configs.EmbedderConfig | str | None) – Config of the embedder, or None to use the best embedder
- Return type:
- get_embedder_config()#
Get the name of the embedder.
- fit(utterances, labels, clear_cache=False)#
Fit the scorer by training or loading the vector index.
- Parameters:
- Raises:
ValueError – If the vector index mismatches the provided utterances
- Return type:
None
- predict(utterances)#
Predict class probabilities for the given utterances.
- predict_with_metadata(utterances)#
Predict class probabilities along with metadata for the given utterances.
- clear_cache()#
Clear cached data in memory used by the vector index.
- Return type:
None