def _build_k_prototypes(self, nprot, data): # K-Means / K-Medoids algorithm # should return a N-length array with he indices of the chosen data grouper = K_Means() grouper._centroid_mode = "index" grouper.fit(data, nprot, iters=300, mode="cuda", cuda_mem='manual', tol=1e-4, max_iters=300) centroids = grouper.centroids nclusters = centroids.shape[0] # TODO - very inefficient k_labels = np.zeros(nclusters, dtype=np.int32) for k in xrange(nclusters): dist = data - centroids[k] dist = dist**2 dist = dist.sum(axis=1) k_labels[k] = dist.argmin() return k_labels
from sklearn.cluster import KMeans from numbapro import cuda import sys n_samples = 1e4 n_features = 2 centers = 6 n_samples = np.int(n_samples) data, gt = make_blobs(n_samples = n_samples, n_features = n_features, centers = centers) data = data.astype(np.float32) n_samples_sqrt = np.int(np.sqrt(n_samples)) n_clusters = [n_samples_sqrt / 2, n_samples_sqrt] n_clusters = map(int,n_clusters) generator = K_Means(cuda_mem="manual") generator.n_clusters = 176 generator.max_iter = 1 generator.N = n_samples generator.D = n_features generator.centroids = generator._init_centroids(data) generator.labels = cuda.pinned_array(shape=generator.N, dtype = np.int32) generator._dists = cuda.pinned_array(shape=generator.N, dtype = np.float32) generator._compute_cuda_dims(data) gridDim = generator._gridDim blockDim = generator._blockDim
n_samples = 1e5 n_features = 100 n_samples = np.int(n_samples) data, gt = make_blobs(n_samples = n_samples, n_features = n_features, centers = centers) data = data.astype(np.float32) n_samples_sqrt = np.int(np.sqrt(n_samples)) n_clusters = [n_samples_sqrt / 2, n_samples_sqrt] n_clusters = map(int,n_clusters) # generator = K_Means(cuda_mem="manual") # generator.n_clusters = n_clusters[-1] # generator.max_iter = 3 # generator.fit(data) generator = K_Means(cuda_mem="manual") generator.N = n_samples generator.D = n_features generator.n_clusters = n_clusters[-1] generator.centroids = generator._init_centroids(data) generator.labels = np.empty(shape=generator.N, dtype = np.int32) generator._dists = np.empty(shape=generator.N, dtype = np.float32) generator._compute_cuda_dims(data) gridDim = generator._gridDim blockDim = generator._blockDim dData = cuda.to_device(data) dCentroids = cuda.to_device(generator.centroids)
n_features = 100 n_samples = np.int(n_samples) data, gt = make_blobs(n_samples=n_samples, n_features=n_features, centers=centers) data = data.astype(np.float32) n_samples_sqrt = np.int(np.sqrt(n_samples)) n_clusters = [n_samples_sqrt / 2, n_samples_sqrt] n_clusters = map(int, n_clusters) # generator = K_Means(cuda_mem="manual") # generator.n_clusters = n_clusters[-1] # generator.max_iter = 3 # generator.fit(data) generator = K_Means(cuda_mem="manual") generator.N = n_samples generator.D = n_features generator.n_clusters = n_clusters[-1] generator.centroids = generator._init_centroids(data) generator.labels = np.empty(shape=generator.N, dtype=np.int32) generator._dists = np.empty(shape=generator.N, dtype=np.float32) generator._compute_cuda_dims(data) gridDim = generator._gridDim blockDim = generator._blockDim dData = cuda.to_device(data) dCentroids = cuda.to_device(generator.centroids)