Skip to content

Commit f35c47c

Browse files
add new examples
1 parent 0beb827 commit f35c47c

File tree

5 files changed

+244
-38
lines changed

5 files changed

+244
-38
lines changed

ann_class2/dropout_tensorflow.py

+12-7
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class HiddenLayer(object):
1313
def __init__(self, M1, M2):
1414
self.M1 = M1
1515
self.M2 = M2
16-
W = np.random.randn(M1, M2) / np.sqrt(M1 + M2)
16+
W = np.random.randn(M1, M2) / np.sqrt(M1)
1717
b = np.zeros(M2)
1818
self.W = tf.Variable(W.astype(np.float32))
1919
self.b = tf.Variable(b.astype(np.float32))
@@ -28,13 +28,16 @@ def __init__(self, hidden_layer_sizes, p_keep):
2828
self.hidden_layer_sizes = hidden_layer_sizes
2929
self.dropout_rates = p_keep
3030

31-
def fit(self, X, Y, lr=10e-7, mu=0.99, decay=0.999, epochs=300, batch_sz=100):
31+
def fit(self, X, Y, lr=1e-3, mu=0.99, decay=0.999, epochs=300, batch_sz=100, split=True, print_every=20):
3232
# make a validation set
3333
X, Y = shuffle(X, Y)
3434
X = X.astype(np.float32)
3535
Y = Y.astype(np.int64)
36-
Xvalid, Yvalid = X[-1000:], Y[-1000:]
37-
X, Y = X[:-1000], Y[:-1000]
36+
if split:
37+
Xvalid, Yvalid = X[-1000:], Y[-1000:]
38+
X, Y = X[:-1000], Y[:-1000]
39+
else:
40+
Xvalid, Yvalid = X, Y
3841

3942
# initialize hidden layers
4043
N, D = X.shape
@@ -45,7 +48,7 @@ def fit(self, X, Y, lr=10e-7, mu=0.99, decay=0.999, epochs=300, batch_sz=100):
4548
h = HiddenLayer(M1, M2)
4649
self.hidden_layers.append(h)
4750
M1 = M2
48-
W = np.random.randn(M1, K) / np.sqrt(M1 + K)
51+
W = np.random.randn(M1, K) / np.sqrt(M1)
4952
b = np.zeros(K)
5053
self.W = tf.Variable(W.astype(np.float32))
5154
self.b = tf.Variable(b.astype(np.float32))
@@ -66,7 +69,8 @@ def fit(self, X, Y, lr=10e-7, mu=0.99, decay=0.999, epochs=300, batch_sz=100):
6669
labels=labels
6770
)
6871
)
69-
train_op = tf.train.RMSPropOptimizer(lr, decay=decay, momentum=mu).minimize(cost)
72+
# train_op = tf.train.RMSPropOptimizer(lr, decay=decay, momentum=mu).minimize(cost)
73+
train_op = tf.train.MomentumOptimizer(lr, momentum=mu).minimize(cost)
7074
prediction = self.predict(inputs)
7175

7276
n_batches = N / batch_sz
@@ -75,14 +79,15 @@ def fit(self, X, Y, lr=10e-7, mu=0.99, decay=0.999, epochs=300, batch_sz=100):
7579
with tf.Session() as session:
7680
session.run(init)
7781
for i in xrange(epochs):
82+
print "epoch:", i, "n_batches:", n_batches
7883
X, Y = shuffle(X, Y)
7984
for j in xrange(n_batches):
8085
Xbatch = X[j*batch_sz:(j*batch_sz+batch_sz)]
8186
Ybatch = Y[j*batch_sz:(j*batch_sz+batch_sz)]
8287

8388
session.run(train_op, feed_dict={inputs: Xbatch, labels: Ybatch})
8489

85-
if j % 20 == 0:
90+
if j % print_every == 0:
8691
c = session.run(cost, feed_dict={inputs: Xvalid, labels: Yvalid})
8792
p = session.run(prediction, feed_dict={inputs: Xvalid})
8893
costs.append(c)

ann_class2/dropout_theano.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def __init__(self, M1, M2, an_id):
2020
self.id = an_id
2121
self.M1 = M1
2222
self.M2 = M2
23-
W = np.random.randn(M1, M2) / np.sqrt(M1 + M2)
23+
W = np.random.randn(M1, M2) / np.sqrt(M1)
2424
b = np.zeros(M2)
2525
self.W = theano.shared(W, 'W_%s' % self.id)
2626
self.b = theano.shared(b, 'b_%s' % self.id)
@@ -35,7 +35,7 @@ def __init__(self, hidden_layer_sizes, p_keep):
3535
self.hidden_layer_sizes = hidden_layer_sizes
3636
self.dropout_rates = p_keep
3737

38-
def fit(self, X, Y, learning_rate=10e-7, mu=0.99, decay=0.999, epochs=300, batch_sz=100, show_fig=False):
38+
def fit(self, X, Y, learning_rate=1e-6, mu=0.99, decay=0.999, epochs=300, batch_sz=100, show_fig=False):
3939
# make a validation set
4040
X, Y = shuffle(X, Y)
4141
X = X.astype(np.float32)
@@ -56,7 +56,7 @@ def fit(self, X, Y, learning_rate=10e-7, mu=0.99, decay=0.999, epochs=300, batch
5656
self.hidden_layers.append(h)
5757
M1 = M2
5858
count += 1
59-
W = np.random.randn(M1, K) / np.sqrt(M1 + K)
59+
W = np.random.randn(M1, K) / np.sqrt(M1)
6060
b = np.zeros(K)
6161
self.W = theano.shared(W, 'W_logreg')
6262
self.b = theano.shared(b, 'b_logreg')
@@ -83,9 +83,9 @@ def fit(self, X, Y, learning_rate=10e-7, mu=0.99, decay=0.999, epochs=300, batch
8383
updates = [
8484
(c, decay*c + (1-decay)*T.grad(cost, p)*T.grad(cost, p)) for p, c in zip(self.params, cache)
8585
] + [
86-
(p, p + mu*dp - learning_rate*T.grad(cost, p)/T.sqrt(c + 10e-10)) for p, c, dp in zip(self.params, cache, dparams)
86+
(p, p + mu*dp - learning_rate*T.grad(cost, p)/T.sqrt(c + 1e-10)) for p, c, dp in zip(self.params, cache, dparams)
8787
] + [
88-
(dp, mu*dp - learning_rate*T.grad(cost, p)/T.sqrt(c + 10e-10)) for p, c, dp in zip(self.params, cache, dparams)
88+
(dp, mu*dp - learning_rate*T.grad(cost, p)/T.sqrt(c + 1e-10)) for p, c, dp in zip(self.params, cache, dparams)
8989
]
9090

9191
# momentum only

ann_class2/grid_search.py

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# For the class Data Science: Practical Deep Learning Concepts in Theano and TensorFlow
2+
# https://deeplearningcourses.com/c/data-science-deep-learning-in-theano-tensorflow
3+
# https://www.udemy.com/data-science-deep-learning-in-theano-tensorflow
4+
from __future__ import print_function, division
5+
from builtins import range
6+
# Note: you may need to update your version of future
7+
# sudo pip install -U future
8+
9+
10+
import theano.tensor as T
11+
from theano_ann import ANN
12+
from util import get_spiral, get_clouds
13+
from sklearn.utils import shuffle
14+
import matplotlib.pyplot as plt
15+
import numpy as np
16+
17+
18+
def grid_search():
19+
# get the data and split into train/test
20+
X, Y = get_spiral()
21+
# X, Y = get_clouds()
22+
X, Y = shuffle(X, Y)
23+
Ntrain = int(0.7*len(X))
24+
Xtrain, Ytrain = X[:Ntrain], Y[:Ntrain]
25+
Xtest, Ytest = X[Ntrain:], Y[Ntrain:]
26+
27+
# hyperparameters to try
28+
hidden_layer_sizes = [
29+
[300],
30+
[100,100],
31+
[50,50,50],
32+
]
33+
learning_rates = [1e-4, 1e-3, 1e-2]
34+
l2_penalties = [0., 0.1, 1.0]
35+
36+
# loop through all possible hyperparameter settings
37+
best_validation_rate = 0
38+
best_hls = None
39+
best_lr = None
40+
best_l2 = None
41+
for hls in hidden_layer_sizes:
42+
for lr in learning_rates:
43+
for l2 in l2_penalties:
44+
model = ANN(hls)
45+
model.fit(Xtrain, Ytrain, learning_rate=lr, reg=l2, mu=0.99, epochs=3000, show_fig=False)
46+
validation_accuracy = model.score(Xtest, Ytest)
47+
train_accuracy = model.score(Xtrain, Ytrain)
48+
print(
49+
"validation_accuracy: %.3f, train_accuracy: %.3f, settings: %s, %s, %s" %
50+
(validation_accuracy, train_accuracy, hls, lr, l2)
51+
)
52+
if validation_accuracy > best_validation_rate:
53+
best_validation_rate = validation_accuracy
54+
best_hls = hls
55+
best_lr = lr
56+
best_l2 = l2
57+
print("Best validation_accuracy:", best_validation_rate)
58+
print("Best settings:")
59+
print("hidden_layer_sizes:", best_hls)
60+
print("learning_rate:", best_lr)
61+
print("l2:", best_l2)
62+
63+
64+
# def one():
65+
# X, Y = get_spiral()
66+
# X, Y = shuffle(X, Y)
67+
# Ntrain = int(0.7*len(X))
68+
# Xtrain, Ytrain = X[:Ntrain], Y[:Ntrain]
69+
# # plt.scatter(Xtrain[:,0], Xtrain[:,1], c=Ytrain)
70+
# # plt.show()
71+
# Xtest, Ytest = X[Ntrain:], Y[Ntrain:]
72+
73+
# model = ANN([100,100])
74+
# model.fit(Xtrain, Ytrain, activation=T.nnet.relu, learning_rate=1e-3, mu=0.99, reg=0., epochs=3000, show_fig=True)
75+
# validation_accuracy = model.score(Xtest, Ytest)
76+
# print("validation_accuracy:", validation_accuracy)
77+
# print("train accuracy:", model.score(Xtrain, Ytrain))
78+
79+
80+
if __name__ == '__main__':
81+
grid_search()
82+
# one()

ann_class2/random_search.py

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# For the class Data Science: Practical Deep Learning Concepts in Theano and TensorFlow
2+
# https://deeplearningcourses.com/c/data-science-deep-learning-in-theano-tensorflow
3+
# https://www.udemy.com/data-science-deep-learning-in-theano-tensorflow
4+
from __future__ import print_function, division
5+
from builtins import range
6+
# Note: you may need to update your version of future
7+
# sudo pip install -U future
8+
9+
10+
# import theano.tensor as T
11+
from theano_ann import ANN
12+
from util import get_spiral, get_clouds
13+
from sklearn.utils import shuffle
14+
import matplotlib.pyplot as plt
15+
import numpy as np
16+
17+
18+
def random_search():
19+
# get the data and split into train/test
20+
X, Y = get_spiral()
21+
# X, Y = get_clouds()
22+
X, Y = shuffle(X, Y)
23+
Ntrain = int(0.7*len(X))
24+
Xtrain, Ytrain = X[:Ntrain], Y[:Ntrain]
25+
Xtest, Ytest = X[Ntrain:], Y[Ntrain:]
26+
27+
# starting hyperparameters
28+
M = 20
29+
nHidden = 2
30+
log_lr = -4
31+
log_l2 = -2 # since we always want it to be positive
32+
max_tries = 30
33+
34+
35+
# loop through all possible hyperparameter settings
36+
best_validation_rate = 0
37+
best_hls = None
38+
best_lr = None
39+
best_l2 = None
40+
for _ in range(max_tries):
41+
model = ANN([M]*nHidden)
42+
model.fit(
43+
Xtrain, Ytrain,
44+
learning_rate=10**log_lr, reg=10**log_l2,
45+
mu=0.99, epochs=3000, show_fig=False
46+
)
47+
validation_accuracy = model.score(Xtest, Ytest)
48+
train_accuracy = model.score(Xtrain, Ytrain)
49+
print(
50+
"validation_accuracy: %.3f, train_accuracy: %.3f, settings: %s, %s, %s" %
51+
(validation_accuracy, train_accuracy, [M]*nHidden, log_lr, log_l2)
52+
)
53+
if validation_accuracy > best_validation_rate:
54+
best_validation_rate = validation_accuracy
55+
best_M = M
56+
best_nHidden = nHidden
57+
best_lr = log_lr
58+
best_l2 = log_l2
59+
60+
# select new hyperparams
61+
nHidden = best_nHidden + np.random.randint(-1, 2) # -1, 0, or 1
62+
nHidden = max(1, nHidden)
63+
M = best_M + np.random.randint(-1, 2)*10
64+
M = max(10, M)
65+
log_lr = best_lr + np.random.randint(-1, 2)
66+
log_l2 = best_l2 + np.random.randint(-1, 2)
67+
print("Best validation_accuracy:", best_validation_rate)
68+
print("Best settings:")
69+
print("best_M:", best_M)
70+
print("best_nHidden:", best_nHidden)
71+
print("learning_rate:", best_lr)
72+
print("l2:", best_l2)
73+
74+
75+
if __name__ == '__main__':
76+
random_search()

0 commit comments

Comments
 (0)