55# https://deeplearningcourses.com/c/data-science-deep-learning-in-python
66# https://www.udemy.com/data-science-deep-learning-in-python
77
8+ from __future__ import print_function , division
9+ from builtins import range
10+ # Note: you may need to update your version of future
11+ # sudo pip install -U future
12+
13+
814import numpy as np
915import matplotlib .pyplot as plt
1016
@@ -53,14 +59,7 @@ def derivative_b1(Z, T, Y, W2):
5359 return dZ .sum (axis = 0 )
5460
5561
56- def cost (T , Y ):
57- # tot = 0
58- # for n in xrange(len(T)):
59- # if T[n] == 1:
60- # tot += np.log(Y[n])
61- # else:
62- # tot += np.log(1 - Y[n])
63- # return tot
62+ def get_log_likelihood (T , Y ):
6463 return np .sum (T * np .log (Y ) + (1 - T )* np .log (1 - Y ))
6564
6665
@@ -72,32 +71,25 @@ def test_xor():
7271 b1 = np .zeros (5 )
7372 W2 = np .random .randn (5 )
7473 b2 = 0
75- LL = [] # keep track of likelihoods
74+ LL = [] # keep track of log- likelihoods
7675 learning_rate = 1e-2
7776 regularization = 0.
7877 last_error_rate = None
79- for i in xrange (30000 ):
78+ for i in range (30000 ):
8079 pY , Z = forward (X , W1 , b1 , W2 , b2 )
81- ll = cost (Y , pY )
80+ ll = get_log_likelihood (Y , pY )
8281 prediction = predict (X , W1 , b1 , W2 , b2 )
8382 er = np .mean (prediction != Y )
84- if er != last_error_rate :
85- last_error_rate = er
86- print "error rate:" , er
87- print "true:" , Y
88- print "pred:" , prediction
89- # if LL and ll < LL[-1]:
90- # print "early exit"
91- # break
83+
9284 LL .append (ll )
9385 W2 += learning_rate * (derivative_w2 (Z , Y , pY ) - regularization * W2 )
9486 b2 += learning_rate * (derivative_b2 (Y , pY ) - regularization * b2 )
9587 W1 += learning_rate * (derivative_w1 (X , Z , Y , pY , W2 ) - regularization * W1 )
9688 b1 += learning_rate * (derivative_b1 (Z , Y , pY , W2 ) - regularization * b1 )
9789 if i % 1000 == 0 :
98- print ll
90+ print ( ll )
9991
100- print "final classification rate:" , np .mean (prediction == Y )
92+ print ( "final classification rate:" , np .mean (prediction == Y ) )
10193 plt .plot (LL )
10294 plt .show ()
10395
@@ -110,45 +102,45 @@ def test_donut():
110102
111103 # distance from origin is radius + random normal
112104 # angle theta is uniformly distributed between (0, 2pi)
113- R1 = np .random .randn (N / 2 ) + R_inner
114- theta = 2 * np .pi * np .random .random (N / 2 )
105+ R1 = np .random .randn (N // 2 ) + R_inner
106+ theta = 2 * np .pi * np .random .random (N // 2 )
115107 X_inner = np .concatenate ([[R1 * np .cos (theta )], [R1 * np .sin (theta )]]).T
116108
117- R2 = np .random .randn (N / 2 ) + R_outer
118- theta = 2 * np .pi * np .random .random (N / 2 )
109+ R2 = np .random .randn (N // 2 ) + R_outer
110+ theta = 2 * np .pi * np .random .random (N // 2 )
119111 X_outer = np .concatenate ([[R2 * np .cos (theta )], [R2 * np .sin (theta )]]).T
120112
121113 X = np .concatenate ([ X_inner , X_outer ])
122- Y = np .array ([0 ]* (N / 2 ) + [1 ]* (N / 2 ))
114+ Y = np .array ([0 ]* (N // 2 ) + [1 ]* (N / /2 ))
123115
124116 n_hidden = 8
125117 W1 = np .random .randn (2 , n_hidden )
126118 b1 = np .random .randn (n_hidden )
127119 W2 = np .random .randn (n_hidden )
128120 b2 = np .random .randn (1 )
129- LL = [] # keep track of likelihoods
121+ LL = [] # keep track of log- likelihoods
130122 learning_rate = 0.00005
131123 regularization = 0.2
132124 last_error_rate = None
133- for i in xrange ( 160000 ):
125+ for i in range ( 3000 ):
134126 pY , Z = forward (X , W1 , b1 , W2 , b2 )
135- ll = cost (Y , pY )
127+ ll = get_log_likelihood (Y , pY )
136128 prediction = predict (X , W1 , b1 , W2 , b2 )
137129 er = np .abs (prediction - Y ).mean ()
138130 LL .append (ll )
139131 W2 += learning_rate * (derivative_w2 (Z , Y , pY ) - regularization * W2 )
140132 b2 += learning_rate * (derivative_b2 (Y , pY ) - regularization * b2 )
141133 W1 += learning_rate * (derivative_w1 (X , Z , Y , pY , W2 ) - regularization * W1 )
142134 b1 += learning_rate * (derivative_b1 (Z , Y , pY , W2 ) - regularization * b1 )
143- if i % 100 == 0 :
144- print "i:" , i , "ll:" , ll , "classification rate:" , 1 - er
135+ if i % 300 == 0 :
136+ print ( "i:" , i , "ll:" , ll , "classification rate:" , 1 - er )
145137 plt .plot (LL )
146138 plt .show ()
147139
148140
149141if __name__ == '__main__' :
150- test_xor ()
151- # test_donut()
142+ # test_xor()
143+ test_donut ()
152144
153145
154146
0 commit comments