Class KNNEvaluator<T>
- Namespace
- AiDotNet.SelfSupervisedLearning.Evaluation
- Assembly
- AiDotNet.dll
k-Nearest Neighbors (k-NN) evaluation for SSL representation quality.
public class KNNEvaluator<T>
Type Parameters
TThe numeric type used for computations.
- Inheritance
-
KNNEvaluator<T>
- Inherited Members
Remarks
For Beginners: k-NN evaluation is a simple way to test SSL representations without any training. We classify each test sample based on the labels of its k nearest neighbors in the representation space.
Why k-NN evaluation?
- No training required - instant evaluation
- Tests if similar images are close in representation space
- Good sanity check during pretraining (run every few epochs)
- Correlates well with linear evaluation accuracy
Typical protocol:
- Extract features from all training and test samples
- For each test sample, find k nearest training samples
- Predict class by majority voting among neighbors
- Report top-1 accuracy
Common settings: k=20, cosine similarity or L2 distance
Constructors
KNNEvaluator(INeuralNetwork<T>, int, bool, double)
Initializes a new instance of the KNNEvaluator class.
public KNNEvaluator(INeuralNetwork<T> encoder, int k = 20, bool useCosine = true, double temperature = 0.07)
Parameters
encoderINeuralNetwork<T>The pretrained encoder.
kintNumber of neighbors to consider (default: 20).
useCosineboolUse cosine similarity instead of L2 distance (default: true).
temperaturedoubleTemperature for weighted voting (default: 0.07).
Properties
K
Gets the k value for k-NN.
public int K { get; }
Property Value
Methods
Evaluate(Tensor<T>, int[])
Evaluates k-NN classification on test data.
public double Evaluate(Tensor<T> testData, int[] testLabels)
Parameters
testDataTensor<T>Test data [num_samples, features].
testLabelsint[]Test labels [num_samples].
Returns
- double
Top-1 accuracy.
EvaluateMultipleK(Tensor<T>, int[], int[])
Evaluates k-NN with different k values.
public Dictionary<int, double> EvaluateMultipleK(Tensor<T> testData, int[] testLabels, int[] kValues)
Parameters
testDataTensor<T>Test data.
testLabelsint[]Test labels.
kValuesint[]List of k values to evaluate.
Returns
- Dictionary<int, double>
Dictionary of k value to accuracy.
Fit(Tensor<T>, int[])
Fits the k-NN classifier with training data.
public void Fit(Tensor<T> trainData, int[] trainLabels)
Parameters
trainDataTensor<T>Training data [num_samples, features].
trainLabelsint[]Training labels [num_samples].
Predict(Tensor<T>)
Predicts classes for multiple test samples.
public int[] Predict(Tensor<T> testData)
Parameters
testDataTensor<T>Test data.
Returns
- int[]
Predicted class labels.