To evaluate a pre-trained model quickly without fine-tuning, the paper freezes it, takes the representations of a few labelled training images per class, and solves a regularised least-squares problem that maps them to target vectors in . The solution is in closed form. Implement this evaluation.
Implement three functions.
few_shot_subset(labels, n_shot, num_classes) returns the indices (in increasing order) of the training examples used: for every class, its first n_shot examples in dataset order. A class with fewer examples contributes all of them.
fit_few_shot(X, y, num_classes, l2) takes features X of shape (n, d) and integer labels y. Build targets of shape (n, K) with if and otherwise, and return the weights
as a NumPy array of shape (d, K), where = l2 . There is no bias term.
few_shot_accuracy(X_train, y_train, X_test, y_test, n_shot, num_classes, l2) selects the subset, fits on it, predicts the class of each test row as the index of the largest entry of (the lowest index on a tie), and returns the fraction of correct predictions as a float.