代码1
def feedforward(self,d):
out = []
for neuron in self.layer:
out.append(neuron.feedforward(d))
return np.array(out)
这是我为执行前馈编写的原始代码。我想提高使用多线程的执行速度,因此我编辑了代码以使用ThreadPoolExecutor
模块中的concurrent.futures
代码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)
变量d
是一个向量,我使用了np.tile()
,以便executor.map
正确地接受输入
计时后两者的执行速度。我发现代码1比代码2快很多(代码2慢了8-10倍)。但是使用多线程的代码难道不会比循环代码更快。是因为我编写的代码是错误的还是因为其他原因。如果是由于我的代码中的某些错误,有人可以告诉我我做错了什么吗?。
谢谢您的帮助。