-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdigit.py
172 lines (130 loc) · 5.07 KB
/
digit.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# -*- coding: utf-8 -*-
"""
Created on Mon Sep 10 21:39:36 2012
@author: Magnus Ericmats
"""
from matplotlib.pyplot import imshow, draw, show
from matplotlib.cm import get_cmap
from scipy.io import loadmat
from pylab import sqrt, floor, ceil, zeros, divide, remainder, permutation
from pylab import concatenate, ones
from numpy.core.fromnumeric import reshape
from numpy import dot, exp, log, mat, square
def displayData(X):
print "Visualizing"
m, n = X.shape
width = round(sqrt(n))
height = width
display_rows = int(floor(sqrt(m)))
display_cols = int(ceil(m/display_rows))
print "Cell width:", width
print "Cell height:", height
print "Display rows:", display_rows
print "Display columns:", display_cols
display = zeros((display_rows*height,display_cols*width))
# Iterate through the training sets, reshape each one and populate
# the display matrix with the letter matrixes.
for xrow in range(0, m):
rowindex = divide(xrow, display_cols)
columnindex = remainder(xrow, display_cols)
rowstart = int(rowindex*height)
rowend = int((rowindex+1)*height)
colstart = int(columnindex*width)
colend = int((columnindex+1)*width)
display[rowstart:rowend, colstart:colend] = X[xrow,:].reshape(height,width).transpose()
imshow(display, cmap=get_cmap('binary'), interpolation='none')
# Show plot without blocking
draw()
def loadMatlabData(filename):
"""
Load data from csv file and divide it into parts for
training, cross validation and test.
"""
# Load the training data
print "Loading training data..."
data = loadmat(filename)
X = data['X']
y = data['y']
# Randomly select 100 datapoints to display
sel = permutation(X.shape[0])
random_columns = sel[0:100]
displayData(X[random_columns,:])
return X,y
def loadWeights(filename):
print "Loading saved Neural Network parameters..."
data = loadmat(filename)
theta1 = data['Theta1']
theta2 = data['Theta2']
return theta1, theta2
def sigmoid(z):
return 1 / (1 + exp(-z))
def sigmoidGradient(z):
return sigmoid(z) * (1.0-sigmoid(z))
def nnCostFunction(nn_params, input_layer_size, hidden_layer_size,
num_labels, X, y, Lambda):
print nn_params[0:hidden_layer_size * input_layer_size].shape
# Reshape the unrolled parameter vector. Remember the bias nodes.
theta1 = reshape(nn_params[0:hidden_layer_size * (input_layer_size + 1)],
(hidden_layer_size, input_layer_size + 1))
theta2 = reshape(nn_params[hidden_layer_size * (input_layer_size + 1):],
(num_labels, hidden_layer_size + 1))
# Number of training sets
m = X.shape[0]
# Return this
J = 0
theta1_grad = zeros(theta1.shape)
theta2_grad = zeros(theta2.shape)
# Substitute all ys as the data presumes indexing starts at 1
y = y - 1
# From y, craete a matrix with zeros and ones
Y = zeros((y.shape[0], num_labels))
for i in range(0, Y.shape[0]):
Y[i,y[i]] = 1.0
# Calculate the hypothesis, h (or a3, the activation in layer 3)
a1 = concatenate((ones((X.shape[0],1)),X), axis=1)
z2 = dot(a1, theta1.transpose())
a2 = concatenate((ones((X.shape[0],1)),sigmoid(z2)), axis=1)
z3 = dot(a2, theta2.transpose())
h = sigmoid(z3) # or a3
# s - sum term of J
s = (-Y*log(h) - (1.0-Y)*log(1.0-h)).sum()
# Don't regularize the bias terms (on index 0)
t1 = theta1[:,1:]
t2 = theta2[:,1:]
# reg - Regularization term of J
reg = Lambda/(2*m) * (square(t1).sum() + square(t2).sum()).sum()
# J - Cost
J = 1.0/m*s + reg
# Calculate gradients
d3 = h - Y
d2 = dot(d3, t2) * sigmoidGradient(z2)
grad2 = 1.0/m * dot(d3.transpose(), a2)
grad1 = 1.0/m * dot(d2.transpose(), a1)
# Calculate the regularization term of the gradients
theta1_grad_reg = theta1
theta2_grad_reg = theta2
theta1_grad_reg[:,0] = 0
theta2_grad_reg[:,0] = 0
theta1_grad = grad1 + Lambda/m*theta1_grad_reg
theta2_grad = grad2 + Lambda/m*theta2_grad_reg
# Unroll gradients
grad = concatenate((theta1_grad.flatten(), theta2_grad.flatten()), axis=1)
return J, grad
def main():
input_layer_size = 20 * 20
hidden_layer_size = 25
num_labels = 10
X, y = loadMatlabData('./ex4data1.mat')
theta1, theta2 = loadWeights('./ex4weights.mat')
# Unroll parameters
nn_params = concatenate((theta1.flatten(), theta2.flatten()), axis=1)
Lambda = 0
# Calculate the cost function
J, grad = nnCostFunction(nn_params, input_layer_size, hidden_layer_size,
num_labels, X, y, Lambda)
print "Cost at parameters (loaded from ex4weights.mat):", J
print "(this value should be about 0.287629)"
# Make sure the plots are not closed
show()
if __name__ == "__main__":
main()