k_mean.py
def KMeans(x, K=10, Niter=10):
    """Implements Lloyd's algorithm for the Euclidean metric."""

    start = time.time()
    N, D = x.shape  # Number of samples, dimension of the ambient space

    c = x[:K, :].clone()  # Simplistic initialization for the centroids

    x_i = LazyTensor(x.view(N, 1, D))  # (N, 1, D) samples
    c_j = LazyTensor(c.view(1, K, D))  # (1, K, D) centroids

    # K-means loop:
    # - x  is the (N, D) point cloud,
    # - cl is the (N,) vector of class labels
    # - c  is the (K, D) cloud of cluster centroids
    for i in range(Niter):

        # E step: assign points to the closest cluster -------------------------
        D_ij = ((x_i - c_j) ** 2).sum(-1)  # (N, K) symbolic squared distances
        cl = D_ij.argmin(dim=1).long().view(-1)  # Points -> Nearest cluster

        # M step: update the centroids to the normalized cluster average: ------
        # Compute the sum of points per cluster:
        c.zero_()
        c.scatter_add_(0, cl[:, None].repeat(1, D), x)

        # Divide by the number of points per cluster:
        Ncl = torch.bincount(cl, minlength=K).type_as(c).view(K, 1)
        c /= Ncl  # in-place division to compute the average

   

    return cl, c
Implements Lloyd's algorithm for the Euclidean metric.
Implements Lloyd's algorithm for the Euclidean metric.
Implements Lloyd ' s algorithm for the Euclidean metric .
def KMeans(x, K=10, Niter=10):
    start = time.time()
    N, D = x.shape  
    c = x[:K, :].clone()  
    x_i = LazyTensor(x.view(N, 1, D))  
    c_j = LazyTensor(c.view(1, K, D))  
    for i in range(Niter):
        D_ij = ((x_i - c_j) ** 2).sum(-1)  
        cl = D_ij.argmin(dim=1).long().view(-1)  
        c.zero_()
        c.scatter_add_(0, cl[:, None].repeat(1, D), x)
        Ncl = torch.bincount(cl, minlength=K).type_as(c).view(K, 1)
        c /= Ncl  
    return cl, c
def KMeans ( x , K = 10 , Niter = 10 ) : start = time . time ( ) N , D = x . shape c = x [ : K , : ] . clone ( ) x_i = LazyTensor ( x . view ( N , 1 , D ) ) c_j = LazyTensor ( c . view ( 1 , K , D ) ) for i in range ( Niter ) : D_ij = ( ( x_i - c_j ) ** 2 ) . sum ( - 1 ) cl = D_ij . argmin ( dim = 1 ) . long ( ) . view ( - 1 ) c . zero_ ( ) c . scatter_add_ ( 0 , cl [ : , None ] . repeat ( 1 , D ) , x ) Ncl = torch . bincount ( cl , minlength = K ) . type_as ( c ) . view ( K , 1 ) c /= Ncl return cl , c
Implements Lloyd's algorithm for the Euclidean metric.
Implements Lloyd ' s algorithm for the Euclidean metric .
"""Implements Lloyd's algorithm for the Euclidean metric.""" # Number of samples, dimension of the ambient space # Simplistic initialization for the centroids # (N, 1, D) samples # (1, K, D) centroids # K-means loop: # - x  is the (N, D) point cloud, # - cl is the (N,) vector of class labels # - c  is the (K, D) cloud of cluster centroids # E step: assign points to the closest cluster ------------------------- # (N, K) symbolic squared distances # Points -> Nearest cluster # M step: update the centroids to the normalized cluster average: ------ # Compute the sum of points per cluster: # Divide by the number of points per cluster: # in-place division to compute the average
x
K
Niter