Python Forum
please help making a loop faster
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
please help making a loop faster
#1
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
Reply


Messages In This Thread
please help making a loop faster - by carla_highlander - Nov-02-2019, 02:39 PM
RE: please help making a loop faster - by paul18fr - Nov-04-2019, 11:23 AM
RE: please help making a loop faster - by ThomasL - Nov-04-2019, 02:33 PM
RE: please help making a loop faster - by mrnapoli - Nov-04-2019, 02:57 PM
RE: please help making a loop faster - by ThomasL - Nov-04-2019, 03:05 PM
RE: please help making a loop faster - by mrnapoli - Nov-04-2019, 03:29 PM
RE: please help making a loop faster - by ThomasL - Nov-04-2019, 06:34 PM
RE: please help making a loop faster - by mrnapoli - Nov-04-2019, 06:35 PM
RE: please help making a loop faster - by ThomasL - Nov-04-2019, 06:58 PM
RE: please help making a loop faster - by mrnapoli - Nov-04-2019, 07:00 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020