Created
November 8, 2019 08:36
-
-
Save RafayAK/87e4197331784e1c6e5c820f02a72fc1 to your computer and use it in GitHub Desktop.
The gist contains the code for computing the "unstable" BCE cost and it's derivative
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def compute_bce_cost(Y, P_hat): | |
""" | |
This function computes Binary Cross-Entropy(bce) Cost and returns the Cost and its | |
derivative. | |
This function uses the following Binary Cross-Entropy Cost defined as: | |
=> (1/m) * np.sum(-Y*np.log(P_hat) - (1-Y)*np.log(1-P_hat)) | |
Args: | |
Y: labels of data | |
P_hat: Estimated output probabilities from the last layer, the output layer | |
Returns: | |
cost: The Binary Cross-Entropy Cost result | |
dP_hat: gradient of Cost w.r.t P_hat | |
""" | |
m = Y.shape[1] # m -> number of examples in the batch | |
cost = (1/m) * np.sum(-Y*np.log(P_hat) - (1-Y)*np.log(1-P_hat)) | |
cost = np.squeeze(cost) # remove extraneous dimensions to give just a scalar (e.g. this turns [[17]] into 17) | |
dP_hat = (1/m) * (-(Y/P_hat) + ((1-Y)/(1-P_hat))) | |
return cost, dP_hat |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment