KNNPersonalization<T>

Models & Types · AiDotNet.FederatedLearning.Personalization

Implements kNN-Per — kNN-based personalization at inference time with zero extra training cost.

Inheritance: FederatedLearningComponentBase<T> → KNNPersonalization<T>

For Beginners

kNN-Per is the simplest way to personalize a federated model: after FL training, each client builds a small cache of (feature, label) pairs from their local data using the global model’s feature extractor. At inference time, the global model’s prediction is combined with a kNN lookup in this local cache. If the test input is similar to local training examples, the kNN component dominates; if it’s novel, the global model dominates. This adds zero extra training cost — just a one-time cache construction.

How It Works

Prediction (Marfoq et al., 2022, Eqs. 4 and 5):

where N_k(x) are the k cached points closest to x under the distance d (Euclidean by default, as in the paper), tau is a temperature (1 in the paper’s equation) and lambda is tuned by cross-validation or set heuristically.

Reference: Marfoq, O., Neglia, G., Kameni, L., Vidal, R. (2022). “Personalized Federated Learning through Local Memorization.” ICML 2022.

Constructors

Constructor Summary
KNNPersonalization(int k, double lambda, double temperature, DistanceMetricType distanceMetric) Creates a new kNN-Per personalization strategy.

Properties

Property Summary
DistanceMetricType DistanceMetric { get; } Gets the distance used to find and weight neighbors.
double Lambda { get; } Gets the kNN mixing weight (lambda).
double Temperature { get; } Gets the temperature tau of the exp(-d / tau) neighbor weights.
int CacheSize { get; } Gets the current cache size.
int K { get; } Gets the number of neighbors (k).

Methods

Method Summary
T[] CombinedPredict(T[] queryFeatures, T[] globalPrediction, int numClasses) Combines kNN prediction with global model prediction per the kNN-Per formula: p_final = lambda * p_kNN + (1 - lambda) * p_global.
T[] KNNPredict(T[] queryFeatures, int numClasses) Performs the kNN-Per lookup: exp(-distance / temperature) weighted voting over the k nearest cached points.
void BuildCache(T[][] features, int[] labels) Builds the local feature cache from training data.