Skip to content

Instantly share code, notes, and snippets.

@kenorb
Forked from mayankgrwl97/backward_pass.py
Created December 4, 2020 18:04
Show Gist options
  • Save kenorb/5503fc6d0a9d3c0795cd5902f6528bcc to your computer and use it in GitHub Desktop.
Save kenorb/5503fc6d0a9d3c0795cd5902f6528bcc to your computer and use it in GitHub Desktop.

Revisions

  1. @mayankgrwl97 mayankgrwl97 revised this gist Feb 20, 2018. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions backward_pass.py
    Original file line number Diff line number Diff line change
    @@ -30,7 +30,7 @@ def conv_backward(dH, cache):
    # Looping over vertical(h) and horizontal(w) axis of the output
    for h in range(n_H):
    for w in range(n_W):
    dX[h:h+f, w:w+f] += W * dZ(h,w)
    dW += X[h:h+f, w:w+f] * dZ(h,w)
    dX[h:h+f, w:w+f] += W * dH(h,w)
    dW += X[h:h+f, w:w+f] * dH(h,w)

    return dX, dW
  2. @mayankgrwl97 mayankgrwl97 created this gist Dec 14, 2017.
    36 changes: 36 additions & 0 deletions backward_pass.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,36 @@
    def conv_backward(dH, cache):
    '''
    The backward computation for a convolution function
    Arguments:
    dH -- gradient of the cost with respect to output of the conv layer (H), numpy array of shape (n_H, n_W) assuming channels = 1
    cache -- cache of values needed for the conv_backward(), output of conv_forward()
    Returns:
    dX -- gradient of the cost with respect to input of the conv layer (X), numpy array of shape (n_H_prev, n_W_prev) assuming channels = 1
    dW -- gradient of the cost with respect to the weights of the conv layer (W), numpy array of shape (f,f) assuming single filter
    '''

    # Retrieving information from the "cache"
    (X, W) = cache

    # Retrieving dimensions from X's shape
    (n_H_prev, n_W_prev) = X.shape

    # Retrieving dimensions from W's shape
    (f, f) = W.shape

    # Retrieving dimensions from dH's shape
    (n_H, n_W) = dH.shape

    # Initializing dX, dW with the correct shapes
    dX = np.zeros(X.shape)
    dW = np.zeros(W.shape)

    # Looping over vertical(h) and horizontal(w) axis of the output
    for h in range(n_H):
    for w in range(n_W):
    dX[h:h+f, w:w+f] += W * dZ(h,w)
    dW += X[h:h+f, w:w+f] * dZ(h,w)

    return dX, dW