Table of Contents

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

T

The 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:

  1. Extract features from all training and test samples
  2. For each test sample, find k nearest training samples
  3. Predict class by majority voting among neighbors
  4. 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

encoder INeuralNetwork<T>

The pretrained encoder.

k int

Number of neighbors to consider (default: 20).

useCosine bool

Use cosine similarity instead of L2 distance (default: true).

temperature double

Temperature for weighted voting (default: 0.07).

Properties

K

Gets the k value for k-NN.

public int K { get; }

Property Value

int

Methods

Evaluate(Tensor<T>, int[])

Evaluates k-NN classification on test data.

public double Evaluate(Tensor<T> testData, int[] testLabels)

Parameters

testData Tensor<T>

Test data [num_samples, features].

testLabels int[]

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

testData Tensor<T>

Test data.

testLabels int[]

Test labels.

kValues int[]

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

trainData Tensor<T>

Training data [num_samples, features].

trainLabels int[]

Training labels [num_samples].

Predict(Tensor<T>)

Predicts classes for multiple test samples.

public int[] Predict(Tensor<T> testData)

Parameters

testData Tensor<T>

Test data.

Returns

int[]

Predicted class labels.