Skip to content

Instantly share code, notes, and snippets.

@RafayAK
Created November 10, 2019 09:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RafayAK/23c8d0f53c4a379fe229f130ed2d46dc to your computer and use it in GitHub Desktop.
Save RafayAK/23c8d0f53c4a379fe229f130ed2d46dc to your computer and use it in GitHub Desktop.
This function computes the stable version of BCE cost
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