Skip to content

Instantly share code, notes, and snippets.

@marcopeix
Created February 26, 2019 18:10
Show Gist options
  • Save marcopeix/245c2be98d7badbb43034b88f52d6066 to your computer and use it in GitHub Desktop.
Save marcopeix/245c2be98d7badbb43034b88f52d6066 to your computer and use it in GitHub Desktop.
def L_model_backward(AL, Y, caches):
grads = {}
L = len(caches) # the number of layers
m = AL.shape[1]
Y = Y.reshape(AL.shape) # after this line, Y is the same shape as AL
dAL = dAL = - (np.divide(Y, AL) - np.divide(1 - Y, 1 - AL))
current_cache = caches[-1]
grads["dA" + str(L)], grads["dW" + str(L)], grads["db" + str(L)] = linear_backward(sigmoid_backward(dAL, current_cache[1]), current_cache[0])
for l in reversed(range(L-1)):
current_cache = caches[l]
dA_prev_temp, dW_temp, db_temp = linear_backward(sigmoid_backward(dAL, current_cache[1]), current_cache[0])
grads["dA" + str(l + 1)] = dA_prev_temp
grads["dW" + str(l + 1)] = dW_temp
grads["db" + str(l + 1)] = db_temp
return grads
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment