-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.py
325 lines (272 loc) · 11.3 KB
/
util.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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
import numpy as np
def softmax(X, W):
"""exp(X*W) normalized."""
[n, m] = X.shape
A = X.dot(W)
A = np.exp(A - A.max(axis=1).reshape(n,1))
A = A / A.sum(axis=1).reshape(n,1)
return A
def vec_to_mat(t, K):
"""Convert a vector of prediction or labels into a 1-of-K matrix
representation."""
if isinstance(t, list):
ncases = len(t)
else: # t is a numpy array
ncases = t.size
T = np.zeros((ncases, K), dtype=np.int)
for i in range(0, ncases):
k = t[i]
if isinstance(k, np.ndarray):
T[i][k[0]] = 1
else:
T[i][k] = 1
return T
def loss_vec_mat(t, T):
"""Compute the loss of prediction vector t, given the ground truth matrix
T, in a 1-of-K representation."""
[n, K] = T.shape
loss = 0
for i in range(0, n):
if not T[i][t[i]]:
loss += 1
return loss
def square_loss(Y, T):
"""Compute element-wise square error of the prediction matrix Y and ground
truth matrix T."""
return ((Y - T) * (Y - T)).sum() / 2
class ActivationTypeError(Exception):
"""Not supported activation function type."""
def __init__(self, msg):
self.msg = msg
def __str__(self):
if isinstance(self.msg, str):
return self.msg
else:
return repr(self.msg)
class OutputTypeError(Exception):
"""Not supported output function type."""
def __init__(self, msg):
self.msg = msg
def __str__(self):
if isinstance(self.msg, str):
return self.msg
else:
return repr(self.msg)
class ConfigFormatError(Exception):
"""Configuration file format error."""
def __init__(self, msg):
self.msg = msg
def __str__(self):
return self.msg
class LayerSpec:
"""A class for the type of activation function of a layer"""
ACT_SIGMOID = 'sigmoid'
ACT_TANH = 'tanh'
ACT_RELU = 'relu'
TYPE_SIGMOID = 0
TYPE_TANH = 1
TYPE_RELU = 2
def __init__(self, out_dim, activation):
self.lookup = { LayerSpec.ACT_SIGMOID : LayerSpec.TYPE_SIGMOID,
LayerSpec.ACT_TANH : LayerSpec.TYPE_TANH,
LayerSpec.ACT_RELU : LayerSpec.TYPE_RELU }
if activation.lower() not in self.lookup:
raise ActivationTypeError(activation)
self.act_type = self.lookup[activation]
self.out_dim = out_dim
class OutputSpec:
"""A class for the type of activation of the output layer"""
ACT_LINEAR = 'linear'
ACT_SOFTMAX = 'softmax'
TYPE_LINEAR = 0
TYPE_SOFTMAX = 1
def __init__(self, out_dim, output_type):
self.lookup = { OutputSpec.ACT_LINEAR : OutputSpec.TYPE_LINEAR,
OutputSpec.ACT_SOFTMAX : OutputSpec.TYPE_SOFTMAX }
if output_type.lower() not in self.lookup:
raise OutputTypeError(output_type)
self.output_type = self.lookup[output_type]
self.out_dim = out_dim
class Config:
"""A class for configuration of the neural net."""
# section names
SEC_DATA = 'data'
SEC_PARAMETERS = 'parameters'
SEC_LAYER = 'layer'
SEC_OUTPUT = 'output'
# attribute names
DATA_TRAIN_FILE = 'train_data_file'
DATA_VAL_FILE = 'val_data_file'
DATA_TEST_FILE = 'test_data_file'
DATA_OUTPUT_DIR = 'output_dir'
PAR_LEARN_RATE = 'learn_rate'
PAR_INIT_SCALE = 'init_scale'
PAR_MOMENTUM = 'momentum'
PAR_WEIGHT_DECAY = 'weight_decay'
PAR_MINIBATCH_SIZE = 'minibatch_size'
PAR_NUM_EPOCHS = 'num_epochs'
PAR_EPOCH_TO_DISPLAY = 'epoch_to_display'
PAR_EPOCH_TO_SAVE = 'epoch_to_save'
PAR_RANDOM_SEED = 'random_seed'
LAYER_TYPE = 'type'
LAYER_OUT_DIM = 'out_dim'
OUTPUT_TYPE = 'type'
OUTPUT_OUT_DIM = 'out_dim'
def __init__(self, file_name):
"""Set default option values. Read configurations from a text file."""
# data files
self.train_data_file = ''
self.val_data_file = ''
self.test_data_file = ''
self.is_val = False
self.is_test = False
self.output_dir = ''
self.output_filename_pattern = 'm%d'
self.is_output = False
# parameters for training
self.learn_rate = 0.001
self.init_scale = 0.001
self.momentum = 0.9
self.weight_decay = 0
self.minibatch_size = 100
self.epoch_to_display = 10
self.epoch_to_save = 100
self.num_epochs = 1000
self.random_seed = None
# information for each layer, read from the file
self.num_layers = 0
# default output layer
self.output = OutputSpec(10, OutputSpec.ACT_LINEAR)
self._parse_cfg_file(file_name)
def _parse_cfg_file(self, file_name):
"""Parse a configuration file."""
import ConfigParser
cfg = ConfigParser.ConfigParser()
cfg.read(file_name)
# make sure the config file is in the right format
self._check_cfg_sections(cfg)
# starts parsing
self.num_layers = len(cfg.sections()) - 3
self.layer = []
# parse data section
if cfg.has_option(Config.SEC_DATA, Config.DATA_TRAIN_FILE):
self.train_data_file = cfg.get(Config.SEC_DATA, Config.DATA_TRAIN_FILE)
else:
raise ConfigFormatError('No training data.')
if cfg.has_option(Config.SEC_DATA, Config.DATA_VAL_FILE):
self.val_data_file = cfg.get(Config.SEC_DATA, Config.DATA_VAL_FILE)
self.is_val = True
if cfg.has_option(Config.SEC_DATA, Config.DATA_TEST_FILE):
self.test_data_file = cfg.get(Config.SEC_DATA, Config.DATA_TEST_FILE)
self.is_test = True
if cfg.has_option(Config.SEC_DATA, Config.DATA_OUTPUT_DIR):
self.output_dir = cfg.get(Config.SEC_DATA, Config.DATA_OUTPUT_DIR)
self.is_output = True
# parse parameter section
if cfg.has_option(Config.SEC_PARAMETERS, Config.PAR_LEARN_RATE):
self.learn_rate = float(cfg.get(Config.SEC_PARAMETERS,
Config.PAR_LEARN_RATE))
if cfg.has_option(Config.SEC_PARAMETERS, Config.PAR_INIT_SCALE):
self.init_scale = float(cfg.get(Config.SEC_PARAMETERS,
Config.PAR_INIT_SCALE))
if cfg.has_option(Config.SEC_PARAMETERS, Config.PAR_MOMENTUM):
self.momentum = float(cfg.get(Config.SEC_PARAMETERS,
Config.PAR_MOMENTUM))
if cfg.has_option(Config.SEC_PARAMETERS, Config.PAR_WEIGHT_DECAY):
self.weight_decay = float(cfg.get(Config.SEC_PARAMETERS,
Config.PAR_WEIGHT_DECAY))
if cfg.has_option(Config.SEC_PARAMETERS, Config.PAR_MINIBATCH_SIZE):
self.minibatch_size = int(cfg.get(Config.SEC_PARAMETERS,
Config.PAR_MINIBATCH_SIZE))
if cfg.has_option(Config.SEC_PARAMETERS, Config.PAR_NUM_EPOCHS):
self.num_epochs = int(cfg.get(Config.SEC_PARAMETERS,
Config.PAR_NUM_EPOCHS))
if cfg.has_option(Config.SEC_PARAMETERS, Config.PAR_EPOCH_TO_DISPLAY):
self.epoch_to_display = int(cfg.get(Config.SEC_PARAMETERS,
Config.PAR_EPOCH_TO_DISPLAY))
if cfg.has_option(Config.SEC_PARAMETERS, Config.PAR_EPOCH_TO_SAVE):
self.epoch_to_save = int(cfg.get(Config.SEC_PARAMETERS,
Config.PAR_EPOCH_TO_SAVE))
if cfg.has_option(Config.SEC_PARAMETERS, Config.PAR_RANDOM_SEED):
self.random_seed = int(cfg.get(Config.SEC_PARAMETERS,
Config.PAR_RANDOM_SEED))
# parse layer specifications
for i in range(1, self.num_layers + 1):
layer_name = Config.SEC_LAYER + str(i)
if cfg.has_option(layer_name, Config.LAYER_TYPE) and \
cfg.has_option(layer_name, Config.LAYER_OUT_DIM):
self.layer.append(LayerSpec(
int(cfg.get(layer_name, Config.LAYER_OUT_DIM)),
cfg.get(layer_name, Config.LAYER_TYPE)))
else:
raise ConfigFormatError('Incomplete layer: ' + layer_name)
# parse output section
if cfg.has_option(Config.SEC_OUTPUT, Config.OUTPUT_TYPE) and \
cfg.has_option(Config.SEC_OUTPUT, Config.OUTPUT_OUT_DIM):
self.output = OutputSpec(
int(cfg.get(Config.SEC_OUTPUT, Config.OUTPUT_OUT_DIM)),
cfg.get(Config.SEC_OUTPUT, Config.OUTPUT_TYPE))
else:
raise ConfigFormatError('Incomplete output layer.')
def _check_cfg_sections(self, cfg):
"""The config parser cfg should already have the config file read in.
"""
secs = cfg.sections()
n_data_sec = 0
n_par_sec = 0
n_output_sec = 0
current_layer = 0
for section in secs:
if section == Config.SEC_DATA:
n_data_sec += 1
if n_data_sec > 1:
raise ConfigFormatError('Multiple data sections.')
elif section == Config.SEC_PARAMETERS:
n_par_sec += 1
if n_par_sec > 1:
raise ConfigFormatError('Multiple parameter sections.')
elif section == Config.SEC_OUTPUT:
n_output_sec += 1
if n_output_sec > 1:
raise ConfigFormatError('Multiple output sections.')
elif section.startswith(Config.SEC_LAYER):
n_layer = int(section[5:])
if not n_layer == current_layer + 1:
raise ConfigFormatError('Repeated/skiped layer definition.')
current_layer += 1
else:
raise ConfigFormatError('Unknown section name.')
return True
def display(self):
"""For debug use only."""
# data section
print '[' + Config.SEC_DATA + ']'
print Config.DATA_TRAIN_FILE + '=' + self.train_data_file
print Config.DATA_VAL_FILE + '=' + self.val_data_file
print 'is_val=' + str(self.is_val)
print Config.DATA_TEST_FILE + '=' + self.test_data_file
print 'is_test=' + str(self.is_test)
print Config.DATA_OUTPUT_DIR + '=' + self.output_dir
print 'is_output=' + str(self.is_output)
print '\n'
# parameter section
print '[' + Config.SEC_PARAMETERS + ']'
print Config.PAR_LEARN_RATE + '=' + str(self.learn_rate)
print Config.PAR_INIT_SCALE + '=' + str(self.init_scale)
print Config.PAR_MOMENTUM + '=' + str(self.momentum)
print Config.PAR_WEIGHT_DECAY + '=' + str(self.weight_decay)
print Config.PAR_MINIBATCH_SIZE + '=' + str(self.minibatch_size)
print Config.PAR_NUM_EPOCHS + '=' + str(self.num_epochs)
print Config.PAR_EPOCH_TO_DISPLAY + '=' + str(self.epoch_to_display)
print Config.PAR_EPOCH_TO_SAVE + '=' + str(self.epoch_to_save)
print '\n'
# layer sections
for i in range(0, self.num_layers):
print '[' + Config.SEC_LAYER + str(i+1) + ']'
print Config.LAYER_TYPE + '=' + str(self.layer[i].act_type)
print Config.LAYER_OUT_DIM + '=' + str(self.layer[i].out_dim)
print '\n'
# output section
print '[' + Config.SEC_OUTPUT + ']'
print Config.OUTPUT_TYPE + '=' + str(self.output.output_type)
print Config.OUTPUT_OUT_DIM + '=' + str(self.output.out_dim)