Repository navigation
Expand file tree
/
Copy pathlambda_optimization.py
More file actions
153 lines (123 loc) · 6.5 KB
/
Copy pathlambda_optimization.py
File metadata and controls
153 lines (123 loc) · 6.5 KB
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
import numpy as np
from scipy.optimize import minimize
from scipy.io import loadmat
from math import sqrt
from func import *
import time
def preprocess():
""" Input:
Although this function doesn't have any input, you are required to load
the MNIST data set from file 'mnist_all.mat'.
Output:
train_data: matrix of training set. Each row of train_data contains
feature vector of a image
train_label: vector of label corresponding to each image in the training
set
validation_data: matrix of training set. Each row of validation_data
contains feature vector of a image
validation_label: vector of label corresponding to each image in the
training set
test_data: matrix of training set. Each row of test_data contains
feature vector of a image
test_label: vector of label corresponding to each image in the testing
set
Some suggestions for preprocessing step:
- feature selection"""
mat = loadmat('mnist_all.mat') # loads the MAT object as a Dictionary
# Pick a reasonable size for validation data
# ------------Initialize preprocess arrays----------------------#
train_preprocess = np.zeros(shape=(50000, 784))
validation_preprocess = np.zeros(shape=(10000, 784))
test_preprocess = np.zeros(shape=(10000, 784))
train_label_preprocess = np.zeros(shape=(50000,))
validation_label_preprocess = np.zeros(shape=(10000,))
test_label_preprocess = np.zeros(shape=(10000,))
# ------------Initialize flag variables----------------------#
train_len = 0
validation_len = 0
test_len = 0
train_label_len = 0
validation_label_len = 0
# ------------Start to split the data set into 6 arrays-----------#
for key in mat:
# -----------when the set is training set--------------------#
if "train" in key:
label = key[-1] # record the corresponding label
tup = mat.get(key)
sap = range(tup.shape[0])
tup_perm = np.random.permutation(sap)
tup_len = len(tup) # get the length of current training set
tag_len = tup_len - 1000 # defines the number of examples which will be added into the training set
# ---------------------adding data to training set-------------------------#
train_preprocess[train_len:train_len + tag_len] = tup[tup_perm[1000:], :]
train_len += tag_len
train_label_preprocess[train_label_len:train_label_len + tag_len] = label
train_label_len += tag_len
# ---------------------adding data to validation set-------------------------#
validation_preprocess[validation_len:validation_len + 1000] = tup[tup_perm[0:1000], :]
validation_len += 1000
validation_label_preprocess[validation_label_len:validation_label_len + 1000] = label
validation_label_len += 1000
# ---------------------adding data to test set-------------------------#
elif "test" in key:
label = key[-1]
tup = mat.get(key)
sap = range(tup.shape[0])
tup_perm = np.random.permutation(sap)
tup_len = len(tup)
test_label_preprocess[test_len:test_len + tup_len] = label
test_preprocess[test_len:test_len + tup_len] = tup[tup_perm]
test_len += tup_len
# ---------------------Shuffle,double and normalize-------------------------#
train_size = range(train_preprocess.shape[0])
train_perm = np.random.permutation(train_size)
train_data = train_preprocess[train_perm]
train_data = np.double(train_data)
train_data = train_data / 255.0
train_label = train_label_preprocess[train_perm]
validation_size = range(validation_preprocess.shape[0])
vali_perm = np.random.permutation(validation_size)
validation_data = validation_preprocess[vali_perm]
validation_data = np.double(validation_data)
validation_data = validation_data / 255.0
validation_label = validation_label_preprocess[vali_perm]
test_size = range(test_preprocess.shape[0])
test_perm = np.random.permutation(test_size)
test_data = test_preprocess[test_perm]
test_data = np.double(test_data)
test_data = test_data / 255.0
test_label = test_label_preprocess[test_perm]
#-------------------------- feature selection ----------------------------------------------------#
selected_features = [i for i in range(train_data.shape[1]) if len(np.unique(train_data[:,i])) > 1]
train_data = train_data[:, selected_features]
validation_data = validation_data[:, selected_features]
test_data = test_data[:, selected_features]
print('preprocess done')
return train_data, train_label, validation_data, validation_label, test_data, test_label, selected_features
lambdas = np.arange(0.0, 60.0, 5.0)
for lambdaval in lambdas:
train_data, train_label, validation_data, validation_label, test_data, test_label, selected_features = preprocess()
print('\n############## Running lambda: ' + str(lambdaval) + '....')
# N = 1000
# train_data = train_data[:N]
# train_label = train_label[:N]
starttime = time.time()
n_input = train_data.shape[1] # not including bias unit
n_hidden = 20
n_class = 10
# initialize the weights into some random matrices
initial_w1 = initializeWeights(n_input, n_hidden)
initial_w2 = initializeWeights(n_hidden, n_class)
initialWeights = np.concatenate((initial_w1.flatten(), initial_w2.flatten()), 0)
args = (n_input, n_hidden, n_class, train_data, train_label, lambdaval)
opts = {'maxiter': 150, 'disp': True} # Preferred value.
nn_params = minimize(nnObjFunction, initialWeights, jac=True, args=args, method='CG', options=opts)
# Reshape nnParams from 1D vector into w1 and w2 matrices
w1 = nn_params.x[0:n_hidden * (n_input + 1)].reshape((n_hidden, (n_input + 1)))
w2 = nn_params.x[(n_hidden * (n_input + 1)):].reshape((n_class, (n_hidden + 1)))
predicted_label = nnPredict(w1, w2, train_data)
print('Training set Accuracy:' + str(100 * np.mean((predicted_label == train_label).astype(float))) + '%')
predicted_label = nnPredict(w1, w2, validation_data)
print('Validation set Accuracy:' + str(100 * np.mean((predicted_label == validation_label).astype(float))) + '%')
runtime = time.time() - starttime
print('Runtime: ' + str(runtime) + ' sec\n')