changeset 55:0908b6cd3217

[regression] add better cost function for sigmoids
author Jeff Hammel <k0scist@gmail.com>
date Sun, 24 Sep 2017 15:30:15 -0700
parents 0807ac8992ba
children 5867e4a10fae
files tvii/logistic_regression.py
diffstat 1 files changed, 13 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- 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):