changeset 22:3713c6733990

[logistic regression] introduce illustrative test
author Jeff Hammel <k0scist@gmail.com>
date Mon, 04 Sep 2017 08:59:52 -0700
parents 0149be5a984c
children f34110e28a0a
files tests/test_logistic_regression.py tvii/logistic_regression.py
diffstat 2 files changed, 56 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/tests/test_logistic_regression.py	Mon Sep 04 08:37:06 2017 -0700
+++ b/tests/test_logistic_regression.py	Mon Sep 04 08:59:52 2017 -0700
@@ -4,13 +4,24 @@
 test logistic regression
 """
 
+import numpy as np
 import os
 import unittest
 from tvii import logistic_regression
 
 class LogisticRegresionTests(unittest.TestCase):
-    def test_basic(self):
-        """placeholder"""
+
+    def test_cost(self):
+        """test cost function"""
+
+        w, b, X, Y = (np.array([[1],[2]]),
+                      2,
+                      np.array([[1,2],[3,4]]),
+                      np.array([[1,0]]))
+
+        expected_cost = 6.000064773192205
+        cost = logistic_regression.cost_function(w, b, X, Y)
+        print cost
 
 if __name__ == '__main__':
     unittest.main()
--- a/tvii/logistic_regression.py	Mon Sep 04 08:37:06 2017 -0700
+++ b/tvii/logistic_regression.py	Mon Sep 04 08:59:52 2017 -0700
@@ -14,16 +14,54 @@
 import numpy as np
 from .sigmoid import sigmoid
 
-def cost_function(_):
+
+def propagate(w, b, X, Y):
+    """
+    Implement the cost function and its gradient for the propagation:
+    Forward Propagation:
+    - You get X
+    - You compute $A = \sigma(w^T X + b) = (a^{(0)}, a^{(1)}, ..., a^{(m-1)}, a^{(m)})$
+    - You calculate the cost function: $J = -\frac{1}{m}\sum_{i=1}^{m}y^{(i)}\log(a^{(i)})+(1-y^{(i)})\log(1-a^{(i)})$
+
+    Here are the two formulas you will be using:
+
+    $$ \frac{\partial J}{\partial w} = \frac{1}{m}X(A-Y)^T\tag{7}$$
+    $$ \frac{\partial J}{\partial b} = \frac{1}{m} \sum_{i=1}^m (a^{(i)}-y^{(i)})\tag{8}$$
+
+    Arguments:
+    w -- weights, a numpy array of size (num_px * num_px * 3, 1)
+    b -- bias, a scalar
+    X -- data of size (num_px * num_px * 3, number of examples)
+    Y -- true "label" vector (containing 0 if non-cat, 1 if cat) of size (1, number of examples)
+
+    Return:
+    cost -- negative log-likelihood cost for logistic regression
+    dw -- gradient of the loss with respect to w, thus same shape as w
+    db -- gradient of the loss with respect to b, thus same shape as b
+
+    Tips:
+    - Write your code step by step for the propagation. np.log(), np.dot()
+    """
+
+    m = X.shape[1]
+    A = w.T*X  # compute activation
+
+
+def cost_function(w, b, X, Y):
     """
     Cost function for binary classification
     yhat = sigmoid(W.T*x + b)
     interpret yhat thhe probably that y=1
 
     Loss function:
-    y log(yhat) + (1- {UNFINISHED})
+    y log(yhat) + (1 - y) log(1 - yhat)
     """
-    raise NotImplementedError('TODO')
+
+    m = X.shape[1]
+    A = w.T*X
+    cost = Y*np.log(A) + (1 - Y)*np.log(1 - A)
+    return (1./m)*cost
+
 
 def logistic_regression(_):
     """the slow way"""
@@ -31,6 +69,7 @@
     dw1 =0
     dw2=0
     db=0
+    raise NotImplementedError('TODO')
 
 def logistic_regression(nx):
     dw = np.zeros(nx)
@@ -38,7 +77,7 @@
     # z = np.dot(wT, x) + b  # "boradcasting
     raise NotImplementedError('TODO')
 
-# derivativs:
+# derivatives:
 # dz1 = a1 - y1 ; dz2 = a2 - y2 ; ....
 # dZ = [ dz1 dz2 ... dzm ]
 # Z = w'X + b = np.dot(w', X) + b