#!/usr/bin/env python from yael import yael import time n = 20000 # number of vectors nq = 1000 d = 128 # dimensionality of the vectors nt = 2 # number of threads to use k = 1 # number of nn returned v = yael.fvec_new_rand(d * n) # random set of vectors q = yael.fvec_new_rand(d * nq) idx = yael.ivec_new(nq * k) dis = yael.fvec_new(nq * k) t1 = time.time() yael.knn_thread(nq, n, d, k, v, q, idx, nt) t2 = time.time() idx = yael.IntArray.acquirepointer(idx) print([idx[i] for i in xrange(nq * k)]) print('kmeans performed in %.3fs' % (t2 - t1))
from yael import yael m = 3 n = 4 k = 5 a = yael.FloatArray.acquirepointer(yael.fvec_new_rand(m * k)) b = yael.FloatArray.acquirepointer(yael.fvec_new_rand(k * n)) for transp in ['NN', 'NT', 'TN', 'TT']: print "=====================", transp print "a=", if transp[0] == 'N': yael.fmat_print(a, m, k) else: yael.fmat_print_tranposed(a, m, k) print "b=", if transp[1] == 'N': yael.fmat_print(b, k, n) else: yael.fmat_print_tranposed(b, k, n) res = yael.FloatArray.acquirepointer( yael.fmat_new_mul_full(a, b, m, n, k, transp)) print "a*b=", yael.fmat_print(res, m, n)
from yael import yael import time n = 20000 # number of vectors nq = 1000 d = 128 # dimensionality of the vectors nt = 2 # number of threads to use k = 1 # number of nn returned v = yael.fvec_new_rand (d * n) # random set of vectors q = yael.fvec_new_rand (d * nq) idx = yael.ivec_new (nq * k) dis = yael.fvec_new (nq * k) t1 = time.time() yael.knn_thread (nq, n, d, k, v, q, idx, nt) t2 = time.time() idx = yael.IntArray.acquirepointer (idx) print [idx[i] for i in xrange (nq * k)] print 'kmeans performed in %.3fs' % (t2 - t1)
import time from yael import yael yael.common_srandom(12345) d = 16 n = 1024 k = 4 pts = yael.fvec_new_rand(d * n) pts = yael.fvec.acquirepointer(pts) cents = yael.fvec(d * k) nt = 1 assign = yael.ivec(n) def d_chi2(a, b): return (a - b)**2 / (a + b) print "clustering %d uniform %dD pts in %d centroids" % (n, d, k) for name, flags in ("L2", 0), ("L1", yael.KMEANS_L1), ("Chi2", yael.KMEANS_CHI2): print "%s clustering" % name t0 = time.time()
import time from yael import yael yael.common_srandom(12345) d=16 n=1024 k=4 pts = yael.fvec_new_rand(d * n) pts = yael.fvec.acquirepointer(pts) cents = yael.fvec(d * k) nt = 1 assign = yael.ivec(n) def d_chi2(a, b): return (a - b) ** 2 / (a + b) print "clustering %d uniform %dD pts in %d centroids" % (n, d, k) for name, flags in ("L2", 0), ("L1", yael.KMEANS_L1), ("Chi2", yael.KMEANS_CHI2): print "%s clustering" % name t0 = time.time()
from yael import yael m=3 n=4 k=5 a=yael.FloatArray.acquirepointer(yael.fvec_new_rand(m*k)) b=yael.FloatArray.acquirepointer(yael.fvec_new_rand(k*n)) for transp in ['NN','NT','TN','TT']: print "=====================",transp print "a=", if transp[0]=='N': yael.fmat_print(a,m,k) else: yael.fmat_print_tranposed(a,m,k) print "b=", if transp[1]=='N': yael.fmat_print(b,k,n) else: yael.fmat_print_tranposed(b,k,n) res=yael.FloatArray.acquirepointer(yael.fmat_new_mul_full(a,b,m,n,k,transp)) print "a*b=", yael.fmat_print(res,m,n)