python - Differences in numba outputs -
i implemented basic nearest-neighbors search in study work. fact basic numpy implementation working well, adding '@jit' decorator (compiling in numba), outputs differents (it duplicates neighbors in end unknown reason...)
here basic algorithm:
import numpy np numba import jit @jit(nopython=true) def knn(p, points, k): '''find k nearest neighbors (brute force) of point p in list points (each row point)''' n = p.size # lenght of points m = points.shape[0] # number of points neighbors = np.zeros((k,n)) distances = 1e6*np.ones(k) in xrange(m): d = 0 pt = points[i, :] # point compare r in xrange(n): # each coordinate aux = p[r] - pt[r] d += aux * aux if d < distances[k-1]: # find new neighbor pos = k-1 while pos>0 , d<distances[pos-1]: # find position pos -= 1 pt = points[i, :] # insert neighbor , distance: neighbors[pos+1:, :] = neighbors[pos:-1, :] neighbors[pos, :] = pt distances[pos+1:] = distances[pos:-1] distances[pos] = d return neighbors, distances
for testing:
p = np.random.rand(10) points = np.random.rand(250, 10) k = 5 neighbors = knn(p, points, k)
without @jit decorator, 1 gets correct answer:
in [1]: distances out[1]: array([ 0.3933974 , 0.44754336, 0.54548715, 0.55619749, 0.5657846 ])
but numba compilation gives weird outputs:
out[2]: distances out[2]: array([ 0.3933974 , 0.44754336, 0.54548715, 0.54548715, 0.54548715])
somebody can help? don't realize why happens...
thanks you.
i believe issue numba handling writing 1 slice differently when slices overlapping when running without. i'm not familiar internals of numpy, perhaps there special logic handle dealing volatile memory operations this, aren't there in numba. change following lines , results jit decorator become consistent plain python version:
neighbors[pos+1:, :] = neighbors[pos:-1, :].copy() ... distances[pos+1:] = distances[pos:-1].copy()
Comments
Post a Comment