Mercurial > hg > tvii
comparison tvii/logistic_regression.py @ 28:77f68c241b37
[logistic regression] propagate
author | Jeff Hammel <k0scist@gmail.com> |
---|---|
date | Mon, 04 Sep 2017 11:53:23 -0700 |
parents | c52d8173b056 |
children | ae0c345ea09d |
comparison
equal
deleted
inserted
replaced
27:22218d90d33f | 28:77f68c241b37 |
---|---|
41 | 41 |
42 Tips: | 42 Tips: |
43 - Write your code step by step for the propagation. np.log(), np.dot() | 43 - Write your code step by step for the propagation. np.log(), np.dot() |
44 """ | 44 """ |
45 | 45 |
46 m = X.shape[1] | |
47 | 46 |
47 | |
48 # FORWARD PROPAGATION (FROM X TO COST) | |
48 cost = cost_function(w, b, X, Y) # compute cost | 49 cost = cost_function(w, b, X, Y) # compute cost |
49 | 50 |
50 A = sigmoid(w.T*X + b) # compute activation | 51 # BACKWARD PROPAGATION (TO FIND GRADIENT) |
51 raise NotImplementedError('TODO') | 52 m = X.shape[1] |
53 A = sigmoid(np.dot(w.T, X) + b) # compute activation | |
54 dw = (1./m)*np.dot(X, (A - Y).T) | |
55 db = (1./m)*np.sum(A - Y) | |
56 | |
57 # sanity check | |
58 assert(A.shape[1] == m) | |
59 assert(dw.shape == w.shape), "dw.shape is {}; w.shape is {}".format(dw.shape, w.shape) | |
60 assert(db.dtype == float) | |
61 cost = np.squeeze(cost) | |
62 assert(cost.shape == ()) | |
63 | |
64 # return gradients | |
65 grads = {"dw": dw, | |
66 "db": db} | |
67 return grads, cost | |
52 | 68 |
53 | 69 |
54 def cost_function(w, b, X, Y): | 70 def cost_function(w, b, X, Y): |
55 """ | 71 """ |
56 Cost function for binary classification | 72 Cost function for binary classification |