-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFigure1.py
232 lines (187 loc) · 7.56 KB
/
Figure1.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
'''
Modified version of https://github.com/EigenPro/EigenPro-tensorflow
in particular run_expr.py
'''
from __future__ import print_function
import argparse
import collections
import keras
import numpy as np
import time
import warnings
from distutils.version import StrictVersion
from keras.layers import Dense, Input
from keras.models import Model
from keras import backend as K
from eigenpro import kernels
from eigenpro import mnist
from eigenpro import ciphar
from eigenpro import synthetic
from eigenpro import utils
from eigenpro import training
from eigenpro.backend_extra import hasGPU
from eigenpro.layers import KernelEmbedding, RFF
from eigenpro.optimizers import PSGD, SGD
assert StrictVersion(keras.__version__) >= StrictVersion('2.0.8'), \
"Requires Keras (>=2.0.8)."
if StrictVersion(keras.__version__) > StrictVersion('2.0.8'):
warnings.warn('\n\nEigenPro-tensorflow has been tested with Keras 2.0.8. '
'If the\ncurrent version (%s) fails, '
'switch to 2.0.8 by command,\n\n'
'\tpip install Keras==2.0.8\n\n' %(keras.__version__), Warning)
assert keras.backend.backend() == u'tensorflow', \
"Requires Tensorflow (>=1.2.1)."
assert hasGPU(), "Requires GPU."
# Set the hyper-parameters.
bs = 256 # size of the mini-batch
M = 5000 # (EigenPro) subsample size
k = 160 # (EigenPro) top-k eigensystem
# for dataset in ['Synthetic1', 'Synthetic2']:
for dataset in ['CIPHAR']:
if dataset is 'MNIST':
num_classes = 10
(x_train, y_train), (x_test, y_test) = mnist.load()
elif dataset is 'CIPHAR':
num_classes = 10
(x_train, y_train), (x_test, y_test) = ciphar.load(grey=False)
elif dataset is 'Synthetic1':
num_classes = 2
(x_train, y_train), (x_test, y_test) = synthetic.load(1)
elif dataset is 'Synthetic2':
num_classes = 2
(x_train, y_train), (x_test, y_test) = synthetic.load(2)
size = 40000
print(size)
x_train = x_train[:size]
y_train = y_train[:size]
n, D = x_train.shape # (n_sample, n_feature)
# convert class vectors to binary class matrices
y_train = keras.utils.to_categorical(y_train, num_classes)
y_test = keras.utils.to_categorical(y_test, num_classes)
trainers = collections.OrderedDict()
Trainer = collections.namedtuple('Trainer', ['model', 'x_train', 'x_test', 'tr_scores', 'te_scores'])
input_shape = (D+1,) # n_feature, (sample) index
ix = Input(shape=input_shape, dtype='float32', name='indexed-feat')
x, index = utils.separate_index(ix) # features, sample_id
## Gauss
# Calculate step size and (Primal) EigenPro preconditioner.
s = 5 # kernel bandwidth
kernel = lambda x,y: kernels.Gaussian(x, y, s)
kfeat = KernelEmbedding(kernel, x_train, input_shape=(D,))(x)
kf, scale, s0 = utils.asm_eigenpro_f(
x_train, kernel, M, k, 1, in_rkhs=True)
eta = np.float32(1.5 / s0) # 1.5 / s0
eta = eta * num_classes # correction due to mse loss
# Assemble kernel EigenPro trainer.
y = Dense(num_classes, input_shape=(n,),
activation='linear',
kernel_initializer='zeros',
use_bias=False)(kfeat)
model = Model(ix, y)
#model.compile(loss='mse',
# optimizer=PSGD(pred_t=y,
# index_t=index,
# eta=scale*eta,
# eigenpro_f=lambda g: kf(g, kfeat)),
# metrics=['accuracy'])
model.compile(loss='mse',
optimizer=PSGD(pred_t=y, index_t=index, eta=eta),
metrics=['accuracy'])
trainers['Gauss'] = Trainer(model=model,
x_train = utils.add_index(x_train),
x_test=utils.add_index(x_test),
tr_scores={},
te_scores={})
## Laplace
# Calculate step size and (Primal) EigenPro preconditioner.
s = np.float32(10)
kernel = lambda x,y: kernels.Laplace(x, y, s)
kfeat = KernelEmbedding(kernel, x_train, input_shape=(D,))(x)
kf, scale, s0 = utils.asm_eigenpro_f(
x_train, kernel, M, k, 1, in_rkhs=True)
eta = np.float32(1.5 / s0) # 1.5 / s0
eta = eta * num_classes # correction due to mse loss
# Assemble kernel EigenPro trainer.
y = Dense(num_classes, input_shape=(n,),
activation='linear',
kernel_initializer='zeros',
use_bias=False)(kfeat)
model = Model(ix, y)
model.compile(loss='mse',
optimizer=PSGD(pred_t=y,
index_t=index,
eta=scale*eta,
eigenpro_f=lambda g: kf(g, kfeat)),
metrics=['accuracy'])
trainers['Laplace'] = Trainer(model=model,
x_train = utils.add_index(x_train),
x_test=utils.add_index(x_test),
tr_scores={},
te_scores={})
# Start training.
for name, trainer in trainers.items():
print("")
initial_epoch=0
np.random.seed(1) # Keras uses numpy random number generator
train_ts = 0 # training time in seconds
for epoch in [1, 2, 5, 10, 20, 30, 40, 50, 70, 100]:
start = time.time()
trainer.model.fit(
trainer.x_train, y_train,
batch_size=bs, epochs=epoch, verbose=0,
validation_data=(trainer.x_test, y_test),
initial_epoch=initial_epoch)
train_ts += time.time() - start
tr_score = trainer.model.evaluate(trainer.x_train, y_train, verbose=0)
te_score = trainer.model.evaluate(trainer.x_test, y_test, verbose=0)
trainer.tr_scores[epoch] = tr_score
trainer.te_scores[epoch] = te_score
print("%s\t\ttrain error: %.2f%%\ttest error: %.2f%% (%d epochs, %.2f seconds)" %
(name, (1 - tr_score[1]) * 100, (1 - te_score[1]) * 100, epoch, train_ts))
initial_epoch = epoch
utils.reset()
trainers_dict = {}
for name, trainer in trainers.items():
trainers_dict[name] = {'tr_scores': trainer.tr_scores, 'te_scores': trainer.te_scores}
if name is 'Gauss':
s = 5
kernel = lambda x,y: training.Gaussian(x, y, s)
else:
s = 10
kernel = lambda x,y: training.Laplace(x, y, s)
K = kernel(x_train, x_train)
alpha_lin = np.linalg.solve(K, y_train)
pred_train = K.T.dot(alpha_lin)
y = y_train
mse = (np.square(pred_train - y)).mean(axis=None)
miss = np.count_nonzero(np.argmax(pred_train, axis=1) - np.argmax(y, axis=1)) / y.shape[0]
trainers_dict[name]['lin_train_mse'] = mse
trainers_dict[name]['lin_train_ce'] = miss
print('train')
print('mse')
print(mse)
print('miss')
print(miss)
K = None
utils.reset()
testK = kernel(x_train, x_test)
pred_test = testK.T.dot(alpha_lin)
y = y_test
mse = (np.square(pred_test - y)).mean(axis=None)
miss = np.count_nonzero(np.argmax(pred_test, axis=1) - np.argmax(y, axis=1)) / y.shape[0]
trainers_dict[name]['lin_test_mse'] = mse
trainers_dict[name]['lin_test_ce'] = miss
print('test')
print('mse')
print(mse)
print('miss')
print(miss)
testK = None
utils.reset()
#trainers_dict
with open('output/figure1' + dataset + '-' + time.strftime("%Y%m%d-%H%M%S") + '.txt', 'w') as f:
print(trainers_dict, file=f)
print()
print()
print()
#print(trainers)