我从头制作了一个神经网络,希望它运行得更快一点。我想知道矢量化我的前进道具是否会使它更快。 我当前的前进道具代码是:
def forwardProp(self, inputs):
for i in range (self.dimensions[1]):
self.secondLayerNeurons[i] = self.relu(np.dot(self.firstLayerWeights[i], inputs)+self.firstLayerBiases[i])
for i in range (self.dimensions[2]):
self.outputNeurons[i] = self.sigmoid(np.dot(self.secondLayerWeights[i], self.secondLayerNeurons)+self.secondLayerBiases[i])
如果矢量化可以加快速度,我将如何进行矢量化?提前谢谢
我想知道将我的前进道具矢量化是否会使它更快
对!
如何将其矢量化?
你想消除循环,找出向量代数,也能做同样的事情
让我们说} 。假设您在一个名为
self.firstLayerWeights.shape
是(N, D)
。你想要calculate the row-wise dot product of this matrix with ^{rowwise_dot
的函数中实现了这个逻辑现在有了
rowwise_dot
函数,可以添加整个self.firstLayerBiases
向量,而无需循环接下来,确保
self.relu
和self.sigmoid
可以获取向量,并为向量的每个元素返回所需的内容。这可能涉及类似的把戏,如矢量化逐行点积因此,最后你有:
矢量化前进道具使MLP跑得更快。我用了@operator
相关问题 更多 >
编程相关推荐