Nov-02-2019, 02:39 PM
I'm working on a forward pass for a neural network. I have written loop within loop within loops. I know there's a way to do this in numpy that is much faster and simpler.
def forward_p(x, w, b): """ Inputs: - x: A numpy array of images of shape (N, H, W) - w: A numpy array of weights of shape (M, H, W) - b: A numpy vector of biases of size M Outputs: - cout: a numpy array of shape (N, M) """ N, H, W = x.shape M, _, _ = w.shape cout = np.zeros((N,M)) for ni in range(N): for mi in range(M): cout[ni,mi] = b[mi] for d1 in range(H): for d2 in range(W): cout[ni,mi] += x[ni, d1, d2] * w[mi, d1, d2] return cout