Code 1
def feedforward(self,d):
out = []
for neuron in self.layer:
out.append(neuron.feedforward(d))
return np.array(out)
This the original code I've written for performing a feedforward. I wanted to improve the speed of execution using multithreading so i edited the code to use ThreadPoolExecutor
from concurrent.futures
module
Code 2
def parallel_feedforward(self,func,param):
return func.feedforward(param)
def feedforward(self,d):
out = []
with ThreadPoolExecutor(max_workers = 4) as executor:
new_d = np.tile(d,(len(self.layer),1))
for o in executor.map(self.parallel_feedforward,self.layer,new_d):
out.append(o)
return np.array(out)
variable d
is a vector, i used np.tile()
so that the executor.map
takes the input properly
After timing the the speed of execution of both. I found out that the Code 1 is significantly faster than the Code 2 (Code 2 is almost 8-10 times slower). But wouldn't the code using multithreading be faster than it's loop counterpart. Is it because the code I've written is wrong or is it because of something else. If it is because of some mistake in my code, can someone tell me what have i did wrong?.
Thanks for your help in advance.