Skip to content

Commit 9159f64

Browse files
author
Lazy Programmer
committed
python 3
1 parent 4c6ffb1 commit 9159f64

12 files changed

+216
-132
lines changed

Diff for: unsupervised_class2/__init__.py

Whitespace-only changes.

Diff for: unsupervised_class2/autoencoder.py

+56-31
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,42 @@
1414
from util import relu, error_rate, getKaggleMNIST, init_weights
1515

1616

17+
def T_shared_zeros_like32(p):
18+
# p is a Theano shared itself
19+
return theano.shared(np.zeros_like(p.get_value(), dtype=np.float32))
20+
21+
def momentum_updates(cost, params, mu, learning_rate):
22+
# momentum changes
23+
dparams = [T_shared_zeros_like32(p) for p in params]
24+
25+
updates = []
26+
grads = T.grad(cost, params)
27+
for p, dp, g in zip(params, dparams, grads):
28+
dp_update = mu*dp - learning_rate*g
29+
p_update = p + dp_update
30+
31+
updates.append((dp, dp_update))
32+
updates.append((p, p_update))
33+
return updates
34+
35+
1736
class AutoEncoder(object):
1837
def __init__(self, M, an_id):
1938
self.M = M
2039
self.id = an_id
2140

2241
def fit(self, X, learning_rate=0.5, mu=0.99, epochs=1, batch_sz=100, show_fig=False):
42+
# cast to float
43+
mu = np.float32(mu)
44+
learning_rate = np.float32(learning_rate)
45+
2346
N, D = X.shape
2447
n_batches = N // batch_sz
2548

2649
W0 = init_weights((D, self.M))
2750
self.W = theano.shared(W0, 'W_%s' % self.id)
28-
self.bh = theano.shared(np.zeros(self.M), 'bh_%s' % self.id)
29-
self.bo = theano.shared(np.zeros(D), 'bo_%s' % self.id)
51+
self.bh = theano.shared(np.zeros(self.M, dtype=np.float32), 'bh_%s' % self.id)
52+
self.bo = theano.shared(np.zeros(D, dtype=np.float32), 'bo_%s' % self.id)
3053
self.params = [self.W, self.bh, self.bo]
3154
self.forward_params = [self.W, self.bh]
3255

@@ -61,18 +84,17 @@ def fit(self, X, learning_rate=0.5, mu=0.99, epochs=1, batch_sz=100, show_fig=Fa
6184
outputs=cost,
6285
)
6386

64-
updates = [
65-
(p, p + mu*dp - learning_rate*T.grad(cost, p)) for p, dp in zip(self.params, self.dparams)
66-
] + [
67-
(dp, mu*dp - learning_rate*T.grad(cost, p)) for p, dp in zip(self.params, self.dparams)
68-
]
87+
88+
89+
updates = momentum_updates(cost, self.params, mu, learning_rate)
6990
train_op = theano.function(
7091
inputs=[X_in],
7192
updates=updates,
7293
)
7394

7495
costs = []
7596
print("training autoencoder: %s" % self.id)
97+
print("epochs to do:", epochs)
7698
for i in range(epochs):
7799
print("epoch:", i)
78100
X = shuffle(X)
@@ -117,9 +139,22 @@ def __init__(self, hidden_layer_sizes, UnsupervisedModel=AutoEncoder):
117139
count += 1
118140

119141

120-
def fit(self, X, Y, Xtest, Ytest, pretrain=True, learning_rate=0.01, mu=0.99, reg=0.1, epochs=1, batch_sz=100):
142+
def fit(self, X, Y, Xtest, Ytest,
143+
pretrain=True,
144+
train_head_only=False,
145+
learning_rate=0.1,
146+
mu=0.99,
147+
reg=0.0,
148+
epochs=1,
149+
batch_sz=100):
150+
151+
# cast to float32
152+
learning_rate = np.float32(learning_rate)
153+
mu = np.float32(mu)
154+
reg = np.float32(reg)
155+
121156
# greedy layer-wise training of autoencoders
122-
pretrain_epochs = 1
157+
pretrain_epochs = 2
123158
if not pretrain:
124159
pretrain_epochs = 0
125160

@@ -135,38 +170,27 @@ def fit(self, X, Y, Xtest, Ytest, pretrain=True, learning_rate=0.01, mu=0.99, re
135170
K = len(set(Y))
136171
W0 = init_weights((self.hidden_layers[-1].M, K))
137172
self.W = theano.shared(W0, "W_logreg")
138-
self.b = theano.shared(np.zeros(K), "b_logreg")
173+
self.b = theano.shared(np.zeros(K, dtype=np.float32), "b_logreg")
139174

140175
self.params = [self.W, self.b]
141-
for ae in self.hidden_layers:
142-
self.params += ae.forward_params
143-
144-
# for momentum
145-
self.dW = theano.shared(np.zeros(W0.shape), "dW_logreg")
146-
self.db = theano.shared(np.zeros(K), "db_logreg")
147-
self.dparams = [self.dW, self.db]
148-
for ae in self.hidden_layers:
149-
self.dparams += ae.forward_dparams
176+
if not train_head_only:
177+
for ae in self.hidden_layers:
178+
self.params += ae.forward_params
150179

151180
X_in = T.matrix('X_in')
152181
targets = T.ivector('Targets')
153182
pY = self.forward(X_in)
154183

155-
# squared_magnitude = [(p*p).sum() for p in self.params]
156-
# reg_cost = T.sum(squared_magnitude)
157-
cost = -T.mean( T.log(pY[T.arange(pY.shape[0]), targets]) ) #+ reg*reg_cost
184+
squared_magnitude = [(p*p).sum() for p in self.params]
185+
reg_cost = T.sum(squared_magnitude)
186+
cost = -T.mean( T.log(pY[T.arange(pY.shape[0]), targets]) ) + reg*reg_cost
158187
prediction = self.predict(X_in)
159188
cost_predict_op = theano.function(
160189
inputs=[X_in, targets],
161190
outputs=[cost, prediction],
162191
)
163192

164-
updates = [
165-
(p, p + mu*dp - learning_rate*T.grad(cost, p)) for p, dp in zip(self.params, self.dparams)
166-
] + [
167-
(dp, mu*dp - learning_rate*T.grad(cost, p)) for p, dp in zip(self.params, self.dparams)
168-
]
169-
# updates = [(p, p - learning_rate*T.grad(cost, p)) for p in self.params]
193+
updates = momentum_updates(cost, self.params, mu, learning_rate)
170194
train_op = theano.function(
171195
inputs=[X_in, targets],
172196
updates=updates,
@@ -209,7 +233,8 @@ def main():
209233
# dnn.fit(Xtrain, Ytrain, Xtest, Ytest, epochs=3)
210234
# vs
211235
dnn = DNN([1000, 750, 500])
212-
dnn.fit(Xtrain, Ytrain, Xtest, Ytest, pretrain=False, epochs=10)
236+
dnn.fit(Xtrain, Ytrain, Xtest, Ytest, pretrain=True, train_head_only=False, epochs=3)
237+
# note: try training the head only too! what does that mean?
213238

214239

215240
def test_single_autoencoder():
@@ -239,5 +264,5 @@ def test_single_autoencoder():
239264

240265

241266
if __name__ == '__main__':
242-
# main()
243-
test_single_autoencoder()
267+
main()
268+
# test_single_autoencoder()

Diff for: unsupervised_class2/rbm.py

+16-15
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# https://deeplearningcourses.com/c/unsupervised-deep-learning-in-python
22
# https://www.udemy.com/unsupervised-deep-learning-in-python
3+
from __future__ import print_function, division
4+
from builtins import range, input
5+
# Note: you may need to update your version of future
6+
# sudo pip install -U future
7+
38
import numpy as np
49
import theano
510
import theano.tensor as T
@@ -18,8 +23,12 @@ def __init__(self, M, an_id):
1823
self.rng = RandomStreams()
1924

2025
def fit(self, X, learning_rate=0.1, epochs=1, batch_sz=100, show_fig=False):
26+
# cast to float32
27+
learning_rate = np.float32(learning_rate)
28+
29+
2130
N, D = X.shape
22-
n_batches = N / batch_sz
31+
n_batches = N // batch_sz
2332

2433
W0 = init_weights((D, self.M))
2534
self.W = theano.shared(W0, 'W_%s' % self.id)
@@ -28,14 +37,6 @@ def fit(self, X, learning_rate=0.1, epochs=1, batch_sz=100, show_fig=False):
2837
self.params = [self.W, self.c, self.b]
2938
self.forward_params = [self.W, self.c]
3039

31-
# we won't use this to fit the RBM but we will use these for backpropagation later
32-
# TODO: technically they should be reset before doing backprop
33-
self.dW = theano.shared(np.zeros(W0.shape), 'dW_%s' % self.id)
34-
self.dc = theano.shared(np.zeros(self.M), 'dbh_%s' % self.id)
35-
self.db = theano.shared(np.zeros(D), 'dbo_%s' % self.id)
36-
self.dparams = [self.dW, self.dc, self.db]
37-
self.forward_dparams = [self.dW, self.dc]
38-
3940
X_in = T.matrix('X_%s' % self.id)
4041

4142
# attach it to the object so it can be used later
@@ -50,7 +51,7 @@ def fit(self, X, learning_rate=0.1, epochs=1, batch_sz=100, show_fig=False):
5051
# but we would like to see how this cost function changes
5152
# as we do contrastive divergence
5253
X_hat = self.forward_output(X_in)
53-
cost = -(X_in * T.log(X_hat) + (1 - X_in) * T.log(1 - X_hat)).sum() / (batch_sz * D)
54+
cost = -(X_in * T.log(X_hat) + (1 - X_in) * T.log(1 - X_hat)).mean()
5455
cost_op = theano.function(
5556
inputs=[X_in],
5657
outputs=cost,
@@ -71,15 +72,15 @@ def fit(self, X, learning_rate=0.1, epochs=1, batch_sz=100, show_fig=False):
7172
)
7273

7374
costs = []
74-
print "training rbm: %s" % self.id
75-
for i in xrange(epochs):
76-
print "epoch:", i
75+
print("training rbm: %s" % self.id)
76+
for i in range(epochs):
77+
print("epoch:", i)
7778
X = shuffle(X)
78-
for j in xrange(n_batches):
79+
for j in range(n_batches):
7980
batch = X[j*batch_sz:(j*batch_sz + batch_sz)]
8081
train_op(batch)
8182
the_cost = cost_op(X) # technically we could also get the cost for Xtest here
82-
print "j / n_batches:", j, "/", n_batches, "cost:", the_cost
83+
print("j / n_batches:", j, "/", n_batches, "cost:", the_cost)
8384
costs.append(the_cost)
8485
if show_fig:
8586
plt.plot(costs)

Diff for: unsupervised_class2/tsne_books.py

+10-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# https://deeplearningcourses.com/c/unsupervised-deep-learning-in-python
22
# https://www.udemy.com/unsupervised-deep-learning-in-python
3+
from __future__ import print_function, division
4+
from builtins import range
5+
# Note: you may need to update your version of future
6+
# sudo pip install -U future
7+
38
import nltk
49
import numpy as np
510
import matplotlib.pyplot as plt
@@ -39,6 +44,7 @@ def my_tokenizer(s):
3944
for title in titles:
4045
try:
4146
title = title.encode('ascii', 'ignore') # this will throw exception if bad characters
47+
title = title.decode('utf-8')
4248
all_titles.append(title)
4349
tokens = my_tokenizer(title)
4450
all_tokens.append(tokens)
@@ -47,8 +53,8 @@ def my_tokenizer(s):
4753
word_index_map[token] = current_index
4854
current_index += 1
4955
index_word_map.append(token)
50-
except:
51-
pass
56+
except Exception as e:
57+
print(e)
5258

5359

5460

@@ -67,13 +73,13 @@ def tokens_to_vector(tokens):
6773
for tokens in all_tokens:
6874
X[:,i] = tokens_to_vector(tokens)
6975
i += 1
70-
print "X.shape:", X.shape
76+
print("X.shape:", X.shape)
7177

7278
def main():
7379
tsne = TSNE(perplexity=40)
7480
Z = tsne.fit_transform(X)
7581
plt.scatter(Z[:,0], Z[:,1])
76-
for i in xrange(D):
82+
for i in range(D):
7783
plt.annotate(s=index_word_map[i], xy=(Z[i,0], Z[i,1]))
7884
plt.show()
7985

Diff for: unsupervised_class2/tsne_donut.py

+10-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# https://deeplearningcourses.com/c/unsupervised-deep-learning-in-python
22
# https://www.udemy.com/unsupervised-deep-learning-in-python
3+
from __future__ import print_function, division
4+
from builtins import range
5+
# Note: you may need to update your version of future
6+
# sudo pip install -U future
7+
38
import numpy as np
49
import matplotlib.pyplot as plt
510

@@ -13,16 +18,16 @@ def get_donut_data():
1318

1419
# distance from origin is radius + random normal
1520
# angle theta is uniformly distributed between (0, 2pi)
16-
R1 = np.random.randn(N/2) + R_inner
17-
theta = 2*np.pi*np.random.random(N/2)
21+
R1 = np.random.randn(N//2) + R_inner
22+
theta = 2*np.pi*np.random.random(N//2)
1823
X_inner = np.concatenate([[R1 * np.cos(theta)], [R1 * np.sin(theta)]]).T
1924

20-
R2 = np.random.randn(N/2) + R_outer
21-
theta = 2*np.pi*np.random.random(N/2)
25+
R2 = np.random.randn(N//2) + R_outer
26+
theta = 2*np.pi*np.random.random(N//2)
2227
X_outer = np.concatenate([[R2 * np.cos(theta)], [R2 * np.sin(theta)]]).T
2328

2429
X = np.concatenate([ X_inner, X_outer ])
25-
Y = np.array([0]*(N/2) + [1]*(N/2))
30+
Y = np.array([0]*(N//2) + [1]*(N//2))
2631
return X, Y
2732

2833

Diff for: unsupervised_class2/tsne_mnist.py

+17-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# https://deeplearningcourses.com/c/unsupervised-deep-learning-in-python
22
# https://www.udemy.com/unsupervised-deep-learning-in-python
3+
from __future__ import print_function, division
4+
from builtins import range
5+
# Note: you may need to update your version of future
6+
# sudo pip install -U future
7+
38
import numpy as np
49
import matplotlib.pyplot as plt
510

@@ -10,7 +15,7 @@
1015
import sys
1116
sys.path.append(os.path.abspath('..'))
1217
from unsupervised_class.kmeans_mnist import purity
13-
from unsupervised_class.gmm import gmm
18+
from sklearn.mixture import GaussianMixture
1419

1520

1621
def main():
@@ -26,10 +31,17 @@ def main():
2631
plt.show()
2732

2833
# purity measure from unsupervised machine learning pt 1
29-
_, Rfull = gmm(X, 10, max_iter=30, smoothing=10e-1)
30-
print "full purity:", purity(Y, Rfull)
31-
_, Rreduced = gmm(Z, 10, max_iter=30, smoothing=10e-1)
32-
print "reduced purity:", purity(Y, Rreduced)
34+
# maximum purity is 1, higher is better
35+
gmm = GaussianMixture(n_components=10)
36+
gmm.fit(X)
37+
Rfull = gmm.predict_proba(X)
38+
print("Rfull.shape:", Rfull.shape)
39+
print("full purity:", purity(Y, Rfull))
40+
41+
# now try the same thing on the reduced data
42+
gmm.fit(Z)
43+
Rreduced = gmm.predict_proba(Z)
44+
print("reduced purity:", purity(Y, Rreduced))
3345

3446
if __name__ == '__main__':
3547
main()

Diff for: unsupervised_class2/tsne_xor.py

+5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# https://deeplearningcourses.com/c/unsupervised-deep-learning-in-python
22
# https://www.udemy.com/unsupervised-deep-learning-in-python
3+
from __future__ import print_function, division
4+
from builtins import range
5+
# Note: you may need to update your version of future
6+
# sudo pip install -U future
7+
38
import numpy as np
49
import matplotlib.pyplot as plt
510

0 commit comments

Comments
 (0)