class TransformationStorer: def __init__(self, X, y): # Initialize a fake NCA and variables needed to call the loss # function: self.fake_nca = NeighborhoodComponentsAnalysis() self.fake_nca.n_iter_ = np.inf self.X, y, _ = self.fake_nca._validate_params(X, y) self.same_class_mask = y[:, np.newaxis] == y[np.newaxis, :] def callback(self, transformation, n_iter): """Stores the last value of the transformation taken as input by the optimizer""" self.transformation = transformation
class LossStorer: def __init__(self, X, y): self.loss = np.inf # initialize the loss to very high # Initialize a fake NCA and variables needed to compute the loss: self.fake_nca = NeighborhoodComponentsAnalysis() self.fake_nca.n_iter_ = np.inf self.X, y, _ = self.fake_nca._validate_params(X, y) self.same_class_mask = y[:, np.newaxis] == y[np.newaxis, :] def callback(self, transformation, n_iter): """Stores the last value of the loss function""" self.loss, _ = self.fake_nca._loss_grad_lbfgs(transformation, self.X, self.same_class_mask, -1.0)