0

I am interested in this course and also new to python. I try the first NN program but it is quite slow (mostly in the following loop).

# loop over all test rows
for i in xrange(num_test):
  distances = np.sum(np.abs(self.Xtr - X[i,:]), axis = 1)
  min_index = np.argmin(distances)
  Ypred[i] = self.ytr[min_index]

Is there way to accelerate it?

Thanks.

user180574
  • 5,681
  • 13
  • 53
  • 94
  • Loops are slow in python. If you want efficiency - avoid them. – lejlot Feb 21 '17 at 00:09
  • Since this is about working code, isn't it a better candidate for [codereview.se]? – David Rawson Feb 21 '17 at 00:12
  • @DavidRawson, thanks. I ask here because I see people post similar questions. If they run the same code, they may have similar issue. – user180574 Feb 21 '17 at 00:35
  • @lejlot, could you elaborate a bit how to? Regarding this particular code, I don't see where you can avoid loop. I am not sure how python would do it different way. Thanks. – user180574 Feb 21 '17 at 00:38
  • Loops are fundamental in many programming language... Otherwise, there's recursive functions, pointer referring (eg labels)... – Joe DF Feb 21 '17 at 06:30
  • Add tag of numpy as it could be related how to use it more skillfully to accelerate computing. – user180574 Feb 21 '17 at 17:01
  • There is nothing really to elaborate. Python has extremely slow loops. You have to **vectorize** your code to make it decently fast. It is not a trivial operation and requires some tricks to make it work. The other way around is to use Cython, which can make your loops faster http://nealhughes.net/cython1/ – lejlot Feb 21 '17 at 23:52
  • @lejlot, I really appreciate your comments. Just to contribute my own observation, I try Cython via the introduction in the suggested link. It doesn't accelerate any significantly. – user180574 Feb 23 '17 at 18:52
  • But why it is a loop problem? It is only one loop and within it the major chunk of computation is vector operation where numpy should do a good job, right? – user180574 Feb 23 '17 at 18:55

1 Answers1

0

Answer myself: The parallel approach introduced in this link (Parallelise python loop with numpy arrays and shared-memory) seems to work, basically cython, prange, gil, openmp and other tweaks.

Community
  • 1
  • 1
user180574
  • 5,681
  • 13
  • 53
  • 94