Skip to content

Commit 3e7005a

Browse files
python3
1 parent 6564b53 commit 3e7005a

File tree

4 files changed

+41
-148
lines changed

4 files changed

+41
-148
lines changed

ann_class/batch_donut.py

-112
This file was deleted.

ann_class/forwardprop.py

+9-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@
44
# https://deeplearningcourses.com/c/data-science-deep-learning-in-python
55
# https://www.udemy.com/data-science-deep-learning-in-python
66

7+
from __future__ import print_function, division
8+
from builtins import range
9+
# Note: you may need to update your version of future
10+
# sudo pip install -U future
11+
12+
13+
714
import numpy as np
815
import matplotlib.pyplot as plt
916

@@ -44,7 +51,7 @@ def forward(X, W1, b1, W2, b2):
4451
def classification_rate(Y, P):
4552
n_correct = 0
4653
n_total = 0
47-
for i in xrange(len(Y)):
54+
for i in range(len(Y)):
4855
n_total += 1
4956
if Y[i] == P[i]:
5057
n_correct += 1
@@ -56,5 +63,5 @@ def classification_rate(Y, P):
5663
# verify we chose the correct axis
5764
assert(len(P) == len(Y))
5865

59-
print "Classification rate for randomly chosen weights:", classification_rate(Y, P)
66+
print("Classification rate for randomly chosen weights:", classification_rate(Y, P))
6067

ann_class/sklearn_ann.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@
33
# the notes for this class can be found at:
44
# https://deeplearningcourses.com/c/data-science-deep-learning-in-python
55
# https://www.udemy.com/data-science-deep-learning-in-python
6+
from __future__ import print_function, division
7+
from builtins import range
8+
# Note: you may need to update your version of future
9+
# sudo pip install -U future
10+
11+
612
import sys
713
sys.path.append('../ann_logistic_extra')
814
from process import get_data
@@ -28,4 +34,4 @@
2834
# print the train and test accuracy
2935
train_accuracy = model.score(Xtrain, Ytrain)
3036
test_accuracy = model.score(Xtest, Ytest)
31-
print "train accuracy:", train_accuracy, "test accuracy:", test_accuracy
37+
print("train accuracy:", train_accuracy, "test accuracy:", test_accuracy)

ann_class/xor_donut.py

+25-33
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@
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+
814
import numpy as np
915
import 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

149141
if __name__ == '__main__':
150-
test_xor()
151-
# test_donut()
142+
# test_xor()
143+
test_donut()
152144

153145

154146

0 commit comments

Comments
 (0)