Skip to content

Commit 3947c16

Browse files
committed
working model for digits classification
1 parent 1caf059 commit 3947c16

File tree

2 files changed

+167
-73
lines changed

2 files changed

+167
-73
lines changed
+107-54
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#additional libraries
21
import numpy as np
32
import theano
43
from theano import tensor as T
@@ -10,17 +9,14 @@
109

1110
#random number generator
1211
rng = np.random
13-
14-
#details regarding training set
1512
#number of training examples
16-
N = 5000
13+
M = 5000
1714
#number of features
18-
features = 784
15+
N = 784
1916
#number of output classes
2017
out_class = 10
2118
#learning rate
22-
alpha = 0.00001
23-
19+
alpha = 0.1
2420
training_steps = 1000
2521

2622
#read the data
@@ -33,99 +29,156 @@
3329
Y_train = train['label']
3430
Y_train = Y_train[0:5000]
3531

32+
#convert the values to numpy arrays
33+
X_train = X_train.as_matrix()
34+
X_train = X_train.astype(theano.config.floatX)
35+
Y_train = Y_train.astype(int)
36+
37+
#do the feature scaling of parameters...belong to [0, 1]
38+
max_pixel_val = 255
39+
min_pixel_val = 0
40+
X_train = X_train / (max_pixel_val - min_pixel_val)
41+
42+
3643
#declare variables
37-
# x- (1x785) and y- (1x10)
38-
# 'x' is matrix of 'Nxfeatures' values
3944
x = T.dmatrix('x')
40-
# 'y' is vector of 10 values (output) corresponding to each digit
41-
y = T.ivector('y')
45+
y = T.dvector('y')
4246

4347
#declaring weights and bias term for all the classes
44-
W = np.zeros((features, out_class)).astype(theano.config.floatX)
45-
B = np.zeros((1, out_class)).astype(theano.config.floatX)
46-
w = shared(np.zeros((features), dtype=theano.config.floatX), name='w')
47-
b = shared(0.0, name='b')
48+
W = np.zeros((N, out_class))
49+
B = np.zeros((1, out_class))
50+
w = shared(value=rng.randn((N)), name='w')
51+
b = shared(value=rng.randn(), name='b')
4852

4953
#hypothesis function is basically thus
50-
h = 1 / (1 + T.exp(-T.dot(x, w) - b))
51-
#predict true if hypothesis is greater than 0.5
52-
H = 1 / (1 + T.exp(-T.dot(x, W) - B))
54+
h = 1.0 / (1.0 + T.exp(-T.dot(x, w) - b))
55+
pred = h > 0.5
5356

54-
#had to change this from theano.tensor to numpy?
55-
pred = T.argmax(H, axis=1)
56-
#for i in xrange(N):
57-
# pred[i] = T.argmax(H[i])
57+
# H = 1.0 / (1.0 + T.exp(-T.dot(x, W) - B))
58+
# pred_vec = T.argmax(H, axis=1)
5859

59-
60-
#cost function
60+
#cost function with regularization
6161
J = -y * T.log(h) - (1-y) * T.log(1-h)
62-
cost = J.mean()
63-
#add reguralization term to the cost function
6462
reg = 0.01 * (w ** 2).sum()
65-
cost = cost + reg
63+
cost = J.mean() + reg
64+
6665
#calculate the gradients of parameters
6766
grad_w, grad_b = T.grad(cost, [w, b])
6867

6968
#function for training and predicting
70-
train = function(inputs=[x, y], outputs=[pred, cost],\
71-
updates=[(w, w-alpha * grad_w), (b, b-alpha*grad_b)])
72-
73-
predict = function(inputs=[x], outputs=pred)
69+
train = function(inputs=[x, y], outputs=[pred, cost], \
70+
updates=[(w, w - alpha * grad_w), (b, b - alpha*grad_b)])
71+
# predict = function(inputs=[x], outputs=[H, pred_vec])
7472

73+
#new predict function for a given class DEBUGGING
74+
predict_class = function(inputs=[x], outputs=[h, pred])
7575

76+
"""
77+
TRAIN THE MODEL
78+
"""
7679
#training the model separately for each class
7780
for i in xrange(out_class):
78-
Y_vec_train = np.zeros((N,))
79-
Y_dummy = Y_train == i
8081

81-
#create vector output consisting of zeros and ones
82-
for k in xrange(N):
82+
#create a M-dimensional vector of 0s and 1s for that class
83+
Y_vec_train = np.zeros((M, ))
84+
Y_dummy = (Y_train == i)
85+
for k in xrange(M):
8386
if Y_dummy[k] == True:
8487
Y_vec_train[k] = 1
85-
86-
#declare the weights and bias term
87-
w = shared(rng.randn((features)), name='w')
88-
#why didn't bias term update when initialised with 'zero'
89-
b = shared(rng.randn(), name='b')
9088

91-
print '\n\nTraining the parameters for class ', i
92-
print 'Iterating over entire training set\n'
89+
# if i == 0:
90+
# print 'random b: ', b.get_value()
91+
# print 'random w: ', w.get_value()
92+
93+
#set the random values to the weights and bias terms
94+
w.set_value(rng.randn((N)))
95+
b.set_value(rng.randn())
96+
97+
print '\nTraining the parameters for class ', i
98+
# print 'Iterating over entire training set...\n'
9399
#train the parameters for this particular class
94100
cost_vec = np.zeros((training_steps, ))
95101
for j in xrange(training_steps):
96102
pred, cost = train(X_train, Y_vec_train)
97-
print 'Iteration: ', j+1, '\tCost: ', cost
103+
# print 'updated b: ', b.get_value()
104+
# print 'updated w: ', w.get_value()[0:10]
105+
# time.sleep(0.5)
106+
print '\n'
107+
print 'Iter: ', j+1, '\tJ: ', cost, \
108+
'\tb: ', b.get_value(), '\tw: ', w.get_value()[0:3]
98109
cost_vec[j] = cost
99110

111+
#predictions made by the model at the end of training for class i
112+
# print '\n\nPredicting the output after the training for class ', i
113+
pred_count = 0
114+
for predicted, target in zip(pred, Y_vec_train):
115+
if predicted == target:
116+
pred_count += 1
117+
# print 'predicted: ', predicted, ' Target: ', target
118+
print 'Accuracy in classification for class ', i, ' is: ', (pred_count/5000.0) * 100
119+
100120
#plot cost as a function of weights
101121
x_vals = [idx for idx in range(training_steps)]
102122
y_vals = cost_vec
103123
plt.plot(x_vals, y_vals, 'r')
104-
plt.savefig("./cost{i}.png".format(i=i))
124+
plt.savefig("./cost_for_class_{i}.png".format(i=i))
105125
plt.show()
106126
plt.cla()
107127
plt.clf()
108128

109129
#store the weights of parameters for that particular class
110-
print 'Done with training for class ', i
130+
# print 'Done with training for class ', i
131+
132+
# print 'weights for class: ', i, w.get_value()
133+
# print 'bias term for class: ', i, b.get_value()
134+
135+
#predict the hypothesis for the classes
136+
hypo, pr = predict_class(X_train)
137+
# if i == 0:
138+
# # print 'Xtrain[0, :] : \n', [idx2 for idx2 in X_train[0, :]]
139+
140+
# print hypo
141+
# print pr
142+
# # print '\ntrained b: ', b.get_value()
143+
# # print 'trained w: ', w.get_value()
144+
# # print 'weights:\n', w.get_value()
145+
# # print '\n\nbias: \n', b.get_value()
146+
147+
# # print w.get_value()[0:20]
148+
# # print b.get_value()
111149

112-
print 'weights for class: ', i, w.get_value()
113-
print 'bias term for class: ', i, b.get_value()
114150

115151
W[:, i] = w.get_value()
116152
B[0, i] = b.get_value()
117-
118-
#debug here...are these values stored correctly??
119-
#print 'Weights are as follows: \n'
120-
#print 'Unbiased weights: \n', W[:, i]
121-
#print '\nBiased weight: \n', B[0, i]
122-
#print 'Stored the weights for class ', i
123153
time.sleep(2)
124154

125155
print 'Done with training the model!'
126156

127157
#save parameters for later use
128-
#ANALYSIS DONE...SAVED CORRECTLY
129158
np.savez('./weights.npz', w=W)
130159
np.savez('./bias.npz', b=B)
131160
print 'Saved the parameters...'
161+
162+
163+
"""
164+
PREDICT THE ACCURACY OVER TRAINING SET
165+
"""
166+
H = 1.0 / (1.0 + T.exp(-T.dot(x, W) - B))
167+
pred_vec = T.argmax(H, axis=1)
168+
predict = function(inputs=[x], outputs=[H, pred_vec])
169+
170+
171+
print 'Predicting accuracy over the training set: '
172+
Hypo, predicted = predict(X_train)
173+
print 'hypo shape: ', Hypo.shape
174+
print 'predicted shape: ', predicted.shape
175+
print '\n\nHypo: ', Hypo[0:20, :]
176+
print '\n\npredicted: ', predicted[0:20]
177+
print '\n\ntarget: ', Y_train[0:20]
178+
179+
pred_count = 0
180+
for target, _pred in zip(Y_train, predicted):
181+
# print 'predicted: ', _pred, ' Target: ', target
182+
if target == _pred:
183+
pred_count += 1
184+
print 'Accuracy over entire set of data is: ', (pred_count/5000.0) * 100

NaiveImplementation/predict.py

+60-19
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,76 @@
11
import numpy as np
22
import pandas as pd
33
import theano
4+
from theano import function
5+
import time
6+
from theano import tensor as T
47

58
#read weights and bias term
69
weights = np.load('./weights.npz')
710
bias = np.load('./bias.npz')
8-
weights = weights['w']
9-
bias = bias['b']
11+
w = weights['w']
12+
b = bias['b']
13+
14+
# print 'weights shape: ', w.shape
15+
# print 'bias shape: ', b.shape
16+
17+
#constants
18+
M = 5000
19+
20+
#read the data
1021
train = pd.read_csv('./../Data/train.csv')
11-
X = train.iloc[0:5000, 1:]
12-
Y = train['label']
13-
Y = Y[0:5000]
1422

15-
X = X.as_matrix()
16-
X = X.astype(theano.config.floatX)
23+
#split data into input and output values
24+
X_train = train.iloc[0:5000, 1:]
25+
Y_train = train['label']
26+
Y_train = Y_train[0:5000]
27+
28+
#convert the values to numpy arrays
29+
X_train = X_train.as_matrix()
30+
X_train = X_train.astype(theano.config.floatX)
31+
Y_train = Y_train.astype(int)
32+
33+
#do the feature scaling of parameters...belong to [0, 1]
34+
max_pixel_val = 255
35+
min_pixel_val = 0
36+
X_train = X_train / (max_pixel_val - min_pixel_val)
37+
38+
39+
#declare variables
40+
x = T.dmatrix('x')
1741

1842
#hypothesis function
19-
h = 1 / (1 + np.exp(-np.dot(X, weights) - bias))
20-
count = 0
21-
N = 5000
22-
pred = np.zeros((N, 1))
23-
print h[0:10]
43+
h = 1.0 / (1.0 + T.exp(-T.dot(x, w) - b))
44+
pred_vec = T.argmax(h, axis=1)
45+
# print 'w[:, 0] :', [idx1 for idx1 in w[:, 0]]
46+
# print 'b[0][0]: ', b[0][0]
47+
48+
#predict the hypothesis only for the class i
49+
# h_0 = 1.0 / (1.0 + T.exp(-T.dot(x, w[:, 0]) - b[0][0]))
2450

25-
print 'Shape of hypothesis over training set: ', h.shape
26-
for i in xrange(N):
27-
pred[i] = np.argmax(h[i])
51+
#compile
52+
predict = function(inputs=[x], outputs=[h, pred_vec])
53+
# predict_0 = function(inputs=[x], outputs=h_0)
2854

29-
for i in xrange(N):
30-
if pred[i] == Y[i]:
55+
# debugging function
56+
print 'reading hypothesis...'
57+
#predict values
58+
hypo, predicted = predict(X_train)
59+
# predicted_0 = predict_0(X_train)
60+
# print 'hypo shape: ', hypo.shape, ' predicted shape: ', predicted.shape
61+
# print 'hypothesis: ', hypo[0:20, :]
62+
# print 'corresponding class: ', predicted[0:20]
63+
# print 'X_train[0, :] : \n', [idx2 for idx2 in X_train[0, :]]
64+
# print '\npredictions for class 0: \n', predicted_0
65+
66+
print 'Predicting over training set: \n'
67+
count = 0
68+
for i in xrange(M):
69+
# print 'Iteration: ', i , '\tpredicted: ', predicted[i], '\ttarget: ', Y[i]
70+
# time.sleep(0.1)
71+
if predicted[i] == Y_train[i]:
3172
count += 1
3273

3374
#print accuracy over the given set of examples
34-
print 'Accuracy : ', (count/42000.0) * 100
35-
print 'Done!!'
75+
print 'Accuracy : ', (count/5000.0) * 100, '%'
76+
print 'Done!!'

0 commit comments

Comments
 (0)