# HG changeset patch # User Jeff Hammel # Date 1504551203 25200 # Node ID 77f68c241b37d01618e22d6ab545f287a6d6f385 # Parent 22218d90d33f1f4994fc0a474131a70335ba622f [logistic regression] propagate diff -r 22218d90d33f -r 77f68c241b37 tests/test_logistic_regression.py --- a/tests/test_logistic_regression.py Mon Sep 04 11:38:46 2017 -0700 +++ b/tests/test_logistic_regression.py Mon Sep 04 11:53:23 2017 -0700 @@ -9,6 +9,7 @@ import unittest from tvii import logistic_regression + class LogisticRegresionTests(unittest.TestCase): def test_cost(self): @@ -23,5 +24,23 @@ cost = logistic_regression.cost_function(w, b, X, Y) assert abs(cost - expected_cost) < 1e-6 + def test_propagate(self): + """test canned logistic regression example""" + + # sample variables + w = np.array([[1],[2]]) + b = 2 + X = np.array([[1,2],[3,4]]) + Y = np.array([[1,0]]) + + # calculate gradient and cost + grads, cost = logistic_regression.propagate(w, b, X, Y) + + # compare to expected, + dw_expected = [[ 0.99993216], [ 1.99980262]] + db_expected = 0.499935230625 + cost_expected = 6.000064773192205 + + if __name__ == '__main__': unittest.main() diff -r 22218d90d33f -r 77f68c241b37 tvii/logistic_regression.py --- a/tvii/logistic_regression.py Mon Sep 04 11:38:46 2017 -0700 +++ b/tvii/logistic_regression.py Mon Sep 04 11:53:23 2017 -0700 @@ -43,12 +43,28 @@ - Write your code step by step for the propagation. np.log(), np.dot() """ - m = X.shape[1] + + # FORWARD PROPAGATION (FROM X TO COST) cost = cost_function(w, b, X, Y) # compute cost - A = sigmoid(w.T*X + b) # compute activation - raise NotImplementedError('TODO') + # BACKWARD PROPAGATION (TO FIND GRADIENT) + m = X.shape[1] + A = sigmoid(np.dot(w.T, X) + b) # compute activation + dw = (1./m)*np.dot(X, (A - Y).T) + db = (1./m)*np.sum(A - Y) + + # sanity check + assert(A.shape[1] == m) + assert(dw.shape == w.shape), "dw.shape is {}; w.shape is {}".format(dw.shape, w.shape) + assert(db.dtype == float) + cost = np.squeeze(cost) + assert(cost.shape == ()) + + # return gradients + grads = {"dw": dw, + "db": db} + return grads, cost def cost_function(w, b, X, Y):