Skip to content

Instantly share code, notes, and snippets.

@ImadDabbura
Created September 20, 2018 15:15
Show Gist options
  • Save ImadDabbura/1a82e17d6a3ba9c554e07b808c78fc0c to your computer and use it in GitHub Desktop.
Save ImadDabbura/1a82e17d6a3ba9c554e07b808c78fc0c to your computer and use it in GitHub Desktop.
def compute_cost_reg(AL, y, parameters, lambd=0):
# number of examples
m = y.shape[1]
# compute traditional cross entropy cost
cross_entropy_cost = compute_cost(AL, y)
# convert parameters dictionary to vector
parameters_vector = dictionary_to_vector(parameters)
# compute the regularization penalty
L2_regularization_penalty = (
lambd / (2 * m)) * np.sum(np.square(parameters_vector))
# compute the total cost
cost = cross_entropy_cost + L2_regularization_penalty
return cost
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment