Neighbourhood components analysis (NCA) learns a linear map (shape , so ) for nearest-neighbour classification. Each point picks a neighbour at random with the softmax probability
and the objective is the expected number of points whose random neighbour has the same label:
so is a smooth leave-one-out error.
Implement:
nca_objective(W, X, y) returning (J, grad) where J is a float and grad is the exact gradient with the shape of W. X has shape (N, D). The softmax must not overflow or underflow to when the projected points are far apart (for example when W has a large scale).nca_fit(X, y, W0, lr, n_iter) running exactly n_iter steps of gradient ascent from W0 (do not modify W0). Return (W, history) where history lists before the first step and after every step (n_iter + 1 floats).A point with no other member of its class contributes to .