forked from lazyprogrammer/machine_learning_examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbenchmark.py
146 lines (115 loc) · 4.51 KB
/
benchmark.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# Vanilla deep network
# https://deeplearningcourses.com/c/deep-learning-convolutional-neural-networks-theano-tensorflow
# https://udemy.com/deep-learning-convolutional-neural-networks-theano-tensorflow
# get the data: http://ufldl.stanford.edu/housenumbers/
from __future__ import print_function, division
from builtins import range
# Note: you may need to update your version of future
# sudo pip install -U future
import os
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
from scipy.io import loadmat
from sklearn.utils import shuffle
from datetime import datetime
def error_rate(p, t):
return np.mean(p != t)
def flatten(X):
# input will be (32, 32, 3, N)
# output will be (N, 3072)
N = X.shape[-1]
flat = np.zeros((N, 3072))
for i in range(N):
flat[i] = X[:,:,:,i].reshape(3072)
return flat
# In [6]: train['X'].shape
# Out[6]: (32, 32, 3, 73257)
# In [7]: train['y'].shape
# Out[7]: (73257, 1)
# In [8]: set(train['y'].flatten().tolist())
# Out[8]: {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
# We will change these to 0..9 to be 0-indexed
# In [12]: test['X'].shape
# Out[12]: (32, 32, 3, 26032)
# In [13]: test['y'].shape
# Out[13]: (26032, 1)
def get_data():
if not os.path.exists('../large_files/train_32x32.mat'):
print('Looking for ../large_files/train_32x32.mat')
print('You have not downloaded the data and/or not placed the files in the correct location.')
print('Please get the data from: http://ufldl.stanford.edu/housenumbers')
print('Place train_32x32.mat and test_32x32.mat in the folder large_files adjacent to the class folder')
exit()
train = loadmat('../large_files/train_32x32.mat')
test = loadmat('../large_files/test_32x32.mat')
return train, test
def main():
train, test = get_data()
# Need to scale! don't leave as 0..255
# Y is a N x 1 matrix with values 1..10 (MATLAB indexes by 1)
# So flatten it and make it 0..9
# Also need indicator matrix for cost calculation
Xtrain = flatten(train['X'].astype(np.float32) / 255.)
Ytrain = train['y'].flatten() - 1
Xtrain, Ytrain = shuffle(Xtrain, Ytrain)
Xtest = flatten(test['X'].astype(np.float32) / 255.)
Ytest = test['y'].flatten() - 1
# gradient descent params
max_iter = 20
print_period = 10
N, D = Xtrain.shape
batch_sz = 500
n_batches = N // batch_sz
# initial weights
M1 = 1000 # hidden layer size
M2 = 500
K = 10
W1_init = np.random.randn(D, M1) / np.sqrt(D + M1)
b1_init = np.zeros(M1)
W2_init = np.random.randn(M1, M2) / np.sqrt(M1 + M2)
b2_init = np.zeros(M2)
W3_init = np.random.randn(M2, K) / np.sqrt(M2 + K)
b3_init = np.zeros(K)
# define variables and expressions
X = tf.placeholder(tf.float32, shape=(None, D), name='X')
T = tf.placeholder(tf.int32, shape=(None,), name='T')
W1 = tf.Variable(W1_init.astype(np.float32))
b1 = tf.Variable(b1_init.astype(np.float32))
W2 = tf.Variable(W2_init.astype(np.float32))
b2 = tf.Variable(b2_init.astype(np.float32))
W3 = tf.Variable(W3_init.astype(np.float32))
b3 = tf.Variable(b3_init.astype(np.float32))
Z1 = tf.nn.relu( tf.matmul(X, W1) + b1 )
Z2 = tf.nn.relu( tf.matmul(Z1, W2) + b2 )
logits = tf.matmul(Z2, W3) + b3
cost = tf.reduce_sum(
tf.nn.sparse_softmax_cross_entropy_with_logits(
logits=logits,
labels=T
)
)
train_op = tf.train.RMSPropOptimizer(0.0001, decay=0.99, momentum=0.9).minimize(cost)
# we'll use this to calculate the error rate
predict_op = tf.argmax(logits, 1)
t0 = datetime.now()
LL = []
init = tf.global_variables_initializer()
with tf.Session() as session:
session.run(init)
for i in range(max_iter):
for j in range(n_batches):
Xbatch = Xtrain[j*batch_sz:(j*batch_sz + batch_sz),]
Ybatch = Ytrain[j*batch_sz:(j*batch_sz + batch_sz),]
session.run(train_op, feed_dict={X: Xbatch, T: Ybatch})
if j % print_period == 0:
test_cost = session.run(cost, feed_dict={X: Xtest, T: Ytest})
prediction = session.run(predict_op, feed_dict={X: Xtest})
err = error_rate(prediction, Ytest)
print("Cost / err at iteration i=%d, j=%d: %.3f / %.3f" % (i, j, test_cost, err))
LL.append(test_cost)
print("Elapsed time:", (datetime.now() - t0))
plt.plot(LL)
plt.show()
if __name__ == '__main__':
main()