# HG changeset patch # User Jeff Hammel # Date 1506292215 25200 # Node ID 0908b6cd3217d73c2c5010f33551a3670f4279f1 # Parent 0807ac8992ba1f30d3a2c61f8ba664d4474e70d6 [regression] add better cost function for sigmoids diff -r 0807ac8992ba -r 0908b6cd3217 tvii/logistic_regression.py --- a/tvii/logistic_regression.py Sun Sep 24 15:25:49 2017 -0700 +++ b/tvii/logistic_regression.py Sun Sep 24 15:30:15 2017 -0700 @@ -98,8 +98,20 @@ def compute_costs(Yhat, Y): """ Computes the cross-entropy cost given: + + J = -(1/m)*sum_{i=0..m} (y(i)log(yhat(i)) + (1 - y(i))log(1 - yhat(i))) + + Yhat -- The sigmoid output of the network + Y -- "true" label vector """ - raise NotImplementedError('TODO') + + # compute the cross-entropy cost + logprops = np.multiply(np.log(Yhat, Y)) + np.multiply(np.log(1-Yhat), (1-Y)) + cost = - np.sum(logprobs)/m + + cost = np.squeeze(cost) # make sure cost is the dimension we expect + assert (isinstance(cost, float)) + return cost def optimize(w, b, X, Y, num_iterations, learning_rate, print_cost = False):