def _get_kmeans_lookup_table_and_weight(nbits, w, init='k-means++', tol=1e-2, n_init=1, rand_seed=0):
    """
    Generate K-Means lookup table given a weight parameter field

    :param nbits:
        Number of bits for quantization

    :param w:
        Weight as numpy array

    Returns
    -------
    lut: numpy.array
        Lookup table, numpy array of shape (1 << nbits, );
    wq: numpy.array
        Quantized weight of type numpy.uint8
    """
    if _HAS_SKLEARN:
        from sklearn.cluster import KMeans
    else:
        raise Exception('sklearn package required for k-means quantization')
    units = _np.prod(w.shape)
    lut_len = 1 << nbits
    n_clusters = units if (units < lut_len) else lut_len
    wf = w.reshape(-1, 1)
    kmeans = KMeans(n_clusters=n_clusters, init=init, tol=tol, n_init=n_init, random_state=rand_seed).fit(wf)
    wq = kmeans.labels_[:units]
    lut = _np.zeros(lut_len)
    lut[:n_clusters] = kmeans.cluster_centers_.flatten()
    return lut, wq
def _get_kmeans_lookup_table_and_weight ( nbits , w , init = 'k-means++' , tol = 1e-2 , n_init = 1 , rand_seed = 0 ) : if _HAS_SKLEARN : from sklearn . cluster import KMeans else : raise Exception ( 'sklearn package required for k-means quantization' ) units = _np . prod ( w . shape ) lut_len = 1 << nbits n_clusters = units if ( units < lut_len ) else lut_len wf = w . reshape ( - 1 , 1 ) kmeans = KMeans ( n_clusters = n_clusters , init = init , tol = tol , n_init = n_init , random_state = rand_seed ) . fit ( wf ) wq = kmeans . labels_ [ : units ] lut = _np . zeros ( lut_len ) lut [ : n_clusters ] = kmeans . cluster_centers_ . flatten ( ) return lut , wq
Generate K-Means lookup table given a weight parameter field

    :param nbits:
        Number of bits for quantization

    :param w:
        Weight as numpy array

    Returns
    -------
    lut: numpy.array
        Lookup table, numpy array of shape (1 << nbits, );
    wq: numpy.array
        Quantized weight of type numpy.uint8