Created
November 10, 2019 09:12
-
-
Save RafayAK/23c8d0f53c4a379fe229f130ed2d46dc to your computer and use it in GitHub Desktop.
This function computes the stable version of BCE cost
This file contains hidden or 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_stable_bce_cost(Y, Z): | |
""" | |
This function computes the "Stable" Binary Cross-Entropy(stable_bce) Cost and returns the Cost and its | |
derivative w.r.t Z_last(the last linear node) . | |
The Stable Binary Cross-Entropy Cost is defined as: | |
=> (1/m) * np.sum(max(Z,0) - ZY + log(1+exp(-|Z|))) | |
Args: | |
Y: labels of data | |
Z: Values from the last linear node | |
Returns: | |
cost: The "Stable" Binary Cross-Entropy Cost result | |
dZ_last: gradient of Cost w.r.t Z_last | |
""" | |
m = Y.shape[1] | |
cost = (1/m) * np.sum(np.maximum(Z, 0) - Z*Y + np.log(1+ np.exp(- np.abs(Z)))) | |
dZ_last = (1/m) * ((1/(1+np.exp(- Z))) - Y) # from Z computes the Sigmoid so P_hat - Y, where P_hat = sigma(Z) | |
return cost, dZ_last |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment