3

I have a for loop that looks like this in Python:

def layerOneBackprop(l, a):
    for x in range(len(l.weights)):
        for y in range(len(l.weights[x])):
            l.weightDelta[x][y] = a.weightDelta[0][x] * a.weights[0][x] * dSigmoid(l.layerOut[x])
            dW = l.inVals[y] * l.weightDelta[x][y]  
            l.weights[x][y] = l.weights[x][y] - (learningRate * dW)

My current problem is that this loop (the entire function actually) is quite slow as it takes nearly 30-40 seconds to run per call. I am hoping that parallelizing this function will help it run faster on my server. How would this be done in Python?

I have seen thread implementations but I have also read that the GIL for Python may cause my code to become essentially single threaded. How could this be accomplished without such problems?

Tanishq dubey
  • 1,522
  • 7
  • 19
  • 42
  • This sounds a lot like: http://stackoverflow.com/questions/20548628/how-to-do-parallel-programming-in-python – lsamayoa Apr 27 '16 at 04:46
  • 1
    It looks like you're writing some functionality for deep neural networks. Honestly you'll probably gain more performance by switching to `numpy` or `theano` and using their native matrix operations instead of using python's built-in lists. – John Apr 27 '16 at 04:49
  • Thanks for the tip! I am using `numpy` right now, this isn't my most optimized code for `numpy` as well. I wanted to see if it was possible in my old implementation. As `theano` goes, I can't use that because of the environment this code will run in. – Tanishq dubey Apr 27 '16 at 04:50
  • Multiprocessing is the answer you're looking for, but I agree with John it is not the *right* answer. Unless you have a 16 core machine, multiprocessing really isn't going to help a whole lot. +1 to `numpy`, +1 to `pypy`, +1 to `cython` – gnicholas Apr 27 '16 at 04:51
  • While blocking IO operations release Python's GIL, intensive computations don't, so multithreading in Python simply doesn't work. If you look at the provided link (your question is effectively a duplicate of it) you will find multiprocessing proposed for that reason. – Ulrich Eckhardt Apr 27 '16 at 06:11
  • Dude, take a look to PyCuda or PyOpenCl, nothing would be faster than that, hope you a have a cool graphic cards. – Netwave Apr 27 '16 at 07:20

0 Answers0