cs231n:如何让程序运行速度更快?

问题描述:

我对这门课程很感兴趣,也是python的新手。我尝试了第一个NN程序,但是它非常慢(主要在以下循环中)。cs231n:如何让程序运行速度更快?

# 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] 

有没有办法加速它呢?

谢谢。

+0

循环在Python中很慢。如果你想要效率 - 避免它们。 – lejlot

+0

由于这是关于工作代码,是不是[codereview.se]更好的候选人? –

+0

@DavidRawson,谢谢。我在这里问,因为我看到人们发表类似的问题。如果他们运行相同的代码,他们可能会有类似的问题。 – user180574

回答我自己:在这个链接(Parallelise python loop with numpy arrays and shared-memory)中引入的并行方法似乎工作,基本上是cython,prange,gil,openmp和其他调整。