<commit_before>import numpy as np
from numpy import linalg as LA
from scipy import sparse, optimize
from sklearn.metrics.pairwise import euclidean_distances
from sklearn.utils import gen_batches
import logging, sys, os


class LargeMarginNearestNeighbor:
    """
    Large Margin Nearest Neighbor metric learning.

    This implementation follows closely <NAME>'s MATLAB code found at
    https://bitbucket.org/mlcircus/lmnn
    which solves the unconstrained problem, finding a linear transformation with L-BFGS instead of
    solving the constrained problem that finds the globally optimal metric.

    Copyright (c) 2017, <NAME>
    Licensed under the GPLv3 license (see LICENSE.txt)

    Args:
        L (array_like):     [d, D] initial transformation, if None identity or pca will be used based on `use_pca` (default: None)
        k (int):            number of target neighbors (default: 3)
        max_iter (int):     maximum number of iterations in the optimization (default: 200)
        use_pca (bool):     whether to use pca to warm-start the linear transformation,
                            if False, the identity is used except if `L` is not None (default: True)
        tol (float):        tolerance for the optimization  (default: 1e-5)
        verbose (bool):     whether to output information from the L-BFGS optimizer (default: False)
        dim_out (int):      preferred dimensionality of the inputs after the transformation,
                            if None it is inferred from use_pca and L (default: None)
        max_constr (int):   maximum number of constraints to enforce per iteration (default: 10 million)
        use_sparse (bool):  whether to use a sparse matrix for the impostors storage.
                            Although using this, the distance to impostors is computated twice,
                            this way is somewhat faster for larger datasets than with a dense matrix, where unique pairs
                            have to be identified explicitly. (default: True)
        load (string):      file name from which to load a linear transformation.
                            If None, initialisation is either identity or eigenvectors from pca. (default: None)
        save (string):      file name prefix to save intermediate linear transformations to.
                            Will be extended with number of function call and saved after each function call.
                            If None, nothing is saved. (default: None)
        temp_dir (string):  name of directory to save/load computed transformations to/from (default: 'temp_res')
        log_level (int):    level of logger verbosity (default: logging.INFO)

    Attributes:
        targets (array_like):    [N, k], the k target neighbors of each input
        dfG (array_like):        [d, D], the gradient component from target neighbors (computed once)
        n_funcalls (int):        counter of calls to _loss_grad
        logger (object):         logger object, responsible for printing intermediate messages/warnings/etc.
        details (dict):          statistics about the algorithm execution mainly from the L-BFGS optimizer
        obj_count (int):         class object counter
    """

    obj_count = 0

    def __init__(self, L=None, k=3, max_iter=200, use_pca=True, tol=1e-5, verbose=False,
                 dim_out=None, max_constr=int(1e7), use_sparse=True, load=None, save=None, temp_dir='temp_res',
                 log_level=logging.INFO):

        # Parameters
        self.L = L
        self.k = k
        self.max_iter = max_iter
        self.use_pca = use_pca
        self.tol = tol
        self.verbose = verbose
        self.dim_out = dim_out
<commit_msg>v1.2.0 many small fixes and improvements, cleanup<commit_after>import numpy as np
from numpy import linalg as LA
from scipy import sparse, optimize
from sklearn.metrics.pairwise import euclidean_distances
from sklearn.utils import gen_batches
import logging, sys, os


class LargeMarginNearestNeighbor:
    """
    Large Margin Nearest Neighbor metric learning.

    This implementation follows closely <NAME>'s MATLAB code found at
    https://bitbucket.org/mlcircus/lmnn
    which solves the unconstrained problem, finding a linear transformation with L-BFGS instead of
    solving the constrained problem that finds the globally optimal metric.

    Copyright (c) 2017, <NAME>
    Licensed under the GPLv3 license (see LICENSE.txt)

    Args:
        L (array_like):     [d, D] initial transformation, if None identity or pca will be used based on `use_pca` (default: None)
        k (int):            number of target neighbors (default: 3)
        max_iter (int):     maximum number of iterations in the optimization (default: 200)
        use_pca (bool):     whether to use pca to warm-start the linear transformation,
                            if False, the identity is used except if `L` is not None (default: True)
        tol (float):        tolerance for the optimization  (default: 1e-5)
        verbose (bool):     whether to output information from the L-BFGS optimizer (default: False)
        dim_out (int):      preferred dimensionality of the inputs after the transformation,
                            if None it is inferred from use_pca and L (default: None)
        max_constr (int):   maximum number of constraints to enforce per iteration (default: 10 million)
        use_sparse (bool):  whether to use a sparse matrix for the impostors storage.
                            Although using this, the distance to impostors is computed twice,
                            this way is somewhat faster for larger datasets than using a dense matrix, where unique pairs
                            have to be identified explicitly. (default: True)
        load (string):      file name from which to load a linear transformation.
                            If None, initialisation is either identity or eigenvectors from pca. (default: None)
        save (string):      file name prefix to save intermediate linear transformations to. After every function call,
                            it will be extended with number of function call and `.npy` file extension.
                            If None, nothing is saved. (default: None)
        temp_dir (string):  name of directory to save/load computed transformations to/from (default: 'temp_res')
        log_level (int):    level of logger verbosity (default: logging.INFO)

    Attributes:
        targets (array_like):    [N, k], the k target neighbors of each input
        dfG (array_like):        [d, D], the gradient component from target neighbors (computed once)
        n_funcalls (int):        counter of calls to _loss_grad
        logger (object):         logger object, responsible for printing intermediate messages/warnings/etc.
        details (dict):          statistics about the algorithm execution mainly from the L-BFGS optimizer
        obj_count (int):         class object counter
    """

    obj_count = 0

    def __init__(self, L=None, k=3, max_iter=200, use_pca=True, tol=1e-5, verbose=False,
                 dim_out=None, max_constr=int(1e7), use_sparse=True, load=None, save=None, temp_dir='temp_res',
                 log_level=logging.INFO):

        # Parameters
        self.L = L
        self.k = k
        self.max_iter = max_iter
        self.use_pca = use_pca
        self.tol = tol
        self.verbose = verbose
        self.dim_out = dim_out

{'fraction_non_alphanumeric': 0.06310289389067524, 'fraction_numerical': 0.006296891747052519, 'mean_word_length': 2.4071200365130077, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1821514', 'n_tokens_mistral': 1949, 'n_tokens_neox': 1871, 'n_words': 821}