5
5
# https://deeplearningcourses.com/c/data-science-deep-learning-in-python
6
6
# https://www.udemy.com/data-science-deep-learning-in-python
7
7
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
+
8
14
import numpy as np
9
15
import matplotlib .pyplot as plt
10
16
@@ -53,14 +59,7 @@ def derivative_b1(Z, T, Y, W2):
53
59
return dZ .sum (axis = 0 )
54
60
55
61
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 ):
64
63
return np .sum (T * np .log (Y ) + (1 - T )* np .log (1 - Y ))
65
64
66
65
@@ -72,32 +71,25 @@ def test_xor():
72
71
b1 = np .zeros (5 )
73
72
W2 = np .random .randn (5 )
74
73
b2 = 0
75
- LL = [] # keep track of likelihoods
74
+ LL = [] # keep track of log- likelihoods
76
75
learning_rate = 1e-2
77
76
regularization = 0.
78
77
last_error_rate = None
79
- for i in xrange (30000 ):
78
+ for i in range (30000 ):
80
79
pY , Z = forward (X , W1 , b1 , W2 , b2 )
81
- ll = cost (Y , pY )
80
+ ll = get_log_likelihood (Y , pY )
82
81
prediction = predict (X , W1 , b1 , W2 , b2 )
83
82
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
+
92
84
LL .append (ll )
93
85
W2 += learning_rate * (derivative_w2 (Z , Y , pY ) - regularization * W2 )
94
86
b2 += learning_rate * (derivative_b2 (Y , pY ) - regularization * b2 )
95
87
W1 += learning_rate * (derivative_w1 (X , Z , Y , pY , W2 ) - regularization * W1 )
96
88
b1 += learning_rate * (derivative_b1 (Z , Y , pY , W2 ) - regularization * b1 )
97
89
if i % 1000 == 0 :
98
- print ll
90
+ print ( ll )
99
91
100
- print "final classification rate:" , np .mean (prediction == Y )
92
+ print ( "final classification rate:" , np .mean (prediction == Y ) )
101
93
plt .plot (LL )
102
94
plt .show ()
103
95
@@ -110,45 +102,45 @@ def test_donut():
110
102
111
103
# distance from origin is radius + random normal
112
104
# 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 )
115
107
X_inner = np .concatenate ([[R1 * np .cos (theta )], [R1 * np .sin (theta )]]).T
116
108
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 )
119
111
X_outer = np .concatenate ([[R2 * np .cos (theta )], [R2 * np .sin (theta )]]).T
120
112
121
113
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 ))
123
115
124
116
n_hidden = 8
125
117
W1 = np .random .randn (2 , n_hidden )
126
118
b1 = np .random .randn (n_hidden )
127
119
W2 = np .random .randn (n_hidden )
128
120
b2 = np .random .randn (1 )
129
- LL = [] # keep track of likelihoods
121
+ LL = [] # keep track of log- likelihoods
130
122
learning_rate = 0.00005
131
123
regularization = 0.2
132
124
last_error_rate = None
133
- for i in xrange ( 160000 ):
125
+ for i in range ( 3000 ):
134
126
pY , Z = forward (X , W1 , b1 , W2 , b2 )
135
- ll = cost (Y , pY )
127
+ ll = get_log_likelihood (Y , pY )
136
128
prediction = predict (X , W1 , b1 , W2 , b2 )
137
129
er = np .abs (prediction - Y ).mean ()
138
130
LL .append (ll )
139
131
W2 += learning_rate * (derivative_w2 (Z , Y , pY ) - regularization * W2 )
140
132
b2 += learning_rate * (derivative_b2 (Y , pY ) - regularization * b2 )
141
133
W1 += learning_rate * (derivative_w1 (X , Z , Y , pY , W2 ) - regularization * W1 )
142
134
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 )
145
137
plt .plot (LL )
146
138
plt .show ()
147
139
148
140
149
141
if __name__ == '__main__' :
150
- test_xor ()
151
- # test_donut()
142
+ # test_xor()
143
+ test_donut ()
152
144
153
145
154
146
0 commit comments