-
Notifications
You must be signed in to change notification settings - Fork 53
/
configurable.py
362 lines (338 loc) · 11.5 KB
/
configurable.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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright 2016 Timothy Dozat
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import os
import argparse
import numpy as np
import tensorflow as tf
from ConfigParser import SafeConfigParser
#***************************************************************
class Configurable(object):
""""""
#=============================================================
def __init__(self, *args, **kwargs):
""""""
self._name = kwargs.pop('name', type(self).__name__)
if args and kwargs:
raise TypeError('Configurables must take either a config parser or keyword args')
if args:
if len(args) > 1:
raise TypeError('Configurables take at most one argument')
if args:
self._config = args[0]
else:
self._config = self._configure(**kwargs)
return
#=============================================================
def _configure(self, **kwargs):
""""""
config = SafeConfigParser()
config_files = [os.path.join('config', 'defaults.cfg'),
os.path.join('config', self.name.lower() + '.cfg'),
kwargs.pop('config_file', '')]
config.read(config_files)
for option, value in kwargs.iteritems():
assigned = False
for section in config.sections():
if option in config.options(section):
config.set(section, option, str(value))
assigned = True
break
if not assigned:
raise ValueError('%s is not a valid option.' % option)
return config
#=============================================================
argparser = argparse.ArgumentParser()
argparser.add_argument('--config_file')
argparser.add_argument('--data_dir')
argparser.add_argument('--embed_dir')
@property
def name(self):
return self._name
argparser.add_argument('--name')
#=============================================================
# [OS]
@property
def word_file(self):
return self._config.get('OS', 'word_file')
argparser.add_argument('--word_file')
@property
def tag_file(self):
return self._config.get('OS', 'tag_file')
argparser.add_argument('--tag_file')
@property
def rel_file(self):
return self._config.get('OS', 'rel_file')
argparser.add_argument('--rel_file')
@property
def embed_file(self):
return self._config.get('OS', 'embed_file')
argparser.add_argument('--embed_file')
@property
def embed_aux_file(self):
return self._config.get('OS', 'embed_aux_file')
argparser.add_argument('--embed_aux_file')
@property
def train_file(self):
return self._config.get('OS', 'train_file')
argparser.add_argument('--train_file')
@property
def valid_file(self):
return self._config.get('OS', 'valid_file')
argparser.add_argument('--valid_file')
@property
def test_file(self):
return self._config.get('OS', 'test_file')
argparser.add_argument('--test_file')
@property
def save_dir(self):
return self._config.get('OS', 'save_dir')
argparser.add_argument('--save_dir')
#=============================================================
# [Dataset]
@property
def cased(self):
return self._config.getboolean('Dataset', 'cased')
argparser.add_argument('--cased')
@property
def ensure_tree(self):
return self._config.getboolean('Dataset', 'ensure_tree')
argparser.add_argument('--ensure_tree')
@property
def root_label(self):
return self._config.get('Dataset', 'root_label')
argparser.add_argument('--root_label')
@property
def add_to_pretrained(self):
return self._config.getboolean('Dataset', 'add_to_pretrained')
argparser.add_argument('--add_to_pretrained')
@property
def min_occur_count(self):
return self._config.getint('Dataset', 'min_occur_count')
argparser.add_argument('--min_occur_count')
@property
def minimize_pads(self):
return self._config.getboolean('Dataset', 'minimize_pads')
argparser.add_argument('--minimize_pads')
@property
def n_bkts(self):
return self._config.getint('Dataset', 'n_bkts')
argparser.add_argument('--n_bkts')
@property
def n_valid_bkts(self):
return self._config.getint('Dataset', 'n_valid_bkts')
argparser.add_argument('--n_valid_bkts')
@property
def lines_per_buffer(self):
return self._config.getint('Dataset', 'lines_per_buffer')
argparser.add_argument('--lines_per_buffer')
#=============================================================
# [Layers]
@property
def n_recur(self):
return self._config.getint('Layers', 'n_recur')
argparser.add_argument('--n_recur')
@property
def recur_cell(self):
from lib import rnn_cells
return getattr(rnn_cells, self._config.get('Layers', 'recur_cell'))
argparser.add_argument('--recur_cell')
@property
def recur_bidir(self):
return self._config.getboolean('Layers', 'recur_bidir')
argparser.add_argument('--recur_bidir')
@property
def forget_bias(self):
if self._config.get('Layers', 'forget_bias') == 'None':
from lib.linalg import sig_const
return sig_const
else:
return self._config.getfloat('Layers', 'forget_bias')
argparser.add_argument('--forget_bias')
#=============================================================
# [Sizes]
@property
def embed_size(self):
return self._config.getint('Sizes', 'embed_size')
argparser.add_argument('--embed_size')
@property
def recur_size(self):
return self._config.getint('Sizes', 'recur_size')
argparser.add_argument('--recur_size')
@property
def attn_mlp_size(self):
return self._config.getint('Sizes', 'attn_mlp_size')
argparser.add_argument('--attn_mlp_size')
@property
def class_mlp_size(self):
return self._config.getint('Sizes', 'class_mlp_size')
argparser.add_argument('--class_mlp_size')
@property
def info_mlp_size(self):
return self._config.getint('Sizes', 'info_mlp_size')
argparser.add_argument('--info_mlp_size')
#=============================================================
# [Functions]
@property
def recur_func(self):
func = self._config.get('Functions', 'recur_func')
if func == 'identity':
return tf.identity
elif func == 'leaky_relu':
return lambda x: tf.maximum(.1*x, x)
else:
return getattr(tf.nn, func)
argparser.add_argument('--recur_func')
@property
def info_func(self):
func = self._config.get('Functions', 'info_func')
if func == 'identity':
return tf.identity
elif func == 'leaky_relu':
return lambda x: tf.maximum(.1*x, x)
else:
return getattr(tf.nn, func)
argparser.add_argument('--info_func')
@property
def mlp_func(self):
func = self._config.get('Functions', 'mlp_func')
if func == 'identity':
return tf.identity
elif func == 'leaky_relu':
return lambda x: tf.maximum(.1*x, x)
else:
return getattr(tf.nn, func)
argparser.add_argument('--mlp_func')
#=============================================================
# [Regularization]
@property
def word_l2_reg(self):
return self._config.getfloat('Regularization', 'word_l2_reg')
argparser.add_argument('--word_l2_reg')
#=============================================================
# [Dropout]
@property
def word_keep_prob(self):
return self._config.getfloat('Dropout', 'word_keep_prob')
argparser.add_argument('--word_keep_prob')
@property
def tag_keep_prob(self):
return self._config.getfloat('Dropout', 'tag_keep_prob')
argparser.add_argument('--tag_keep_prob')
@property
def rel_keep_prob(self):
return self._config.getfloat('Dropout', 'rel_keep_prob')
argparser.add_argument('--rel_keep_prob')
@property
def recur_keep_prob(self):
return self._config.getfloat('Dropout', 'recur_keep_prob')
argparser.add_argument('--recur_keep_prob')
@property
def cell_include_prob(self):
return self._config.getfloat('Dropout', 'cell_include_prob')
argparser.add_argument('--cell_include_prob')
@property
def hidden_include_prob(self):
return self._config.getfloat('Dropout', 'hidden_include_prob')
argparser.add_argument('--hidden_include_prob')
@property
def ff_keep_prob(self):
return self._config.getfloat('Dropout', 'ff_keep_prob')
argparser.add_argument('--ff_keep_prob')
@property
def mlp_keep_prob(self):
return self._config.getfloat('Dropout', 'mlp_keep_prob')
argparser.add_argument('--mlp_keep_prob')
@property
def info_keep_prob(self):
return self._config.getfloat('Dropout', 'info_keep_prob')
argparser.add_argument('--info_keep_prob')
#=============================================================
# [Learning rate]
@property
def learning_rate(self):
return self._config.getfloat('Learning rate', 'learning_rate')
argparser.add_argument('--learning_rate')
@property
def decay(self):
return self._config.getfloat('Learning rate', 'decay')
argparser.add_argument('--decay')
@property
def decay_steps(self):
return self._config.getfloat('Learning rate', 'decay_steps')
argparser.add_argument('--decay_steps')
@property
def clip(self):
return self._config.getfloat('Learning rate', 'clip')
argparser.add_argument('--clip')
#=============================================================
# [Radam]
@property
def mu(self):
return self._config.getfloat('Radam', 'mu')
argparser.add_argument('--mu')
@property
def nu(self):
return self._config.getfloat('Radam', 'nu')
argparser.add_argument('--nu')
@property
def gamma(self):
return self._config.getfloat('Radam', 'gamma')
argparser.add_argument('--gamma')
@property
def epsilon(self):
return self._config.getfloat('Radam', 'epsilon')
argparser.add_argument('--epsilon')
@property
def chi(self):
return self._config.getfloat('Radam', 'chi')
argparser.add_argument('--chi')
#=============================================================
# [Training]
@property
def pretrain_iters(self):
return self._config.getint('Training', 'pretrain_iters')
argparser.add_argument('--pretrain_iters')
@property
def train_iters(self):
return self._config.getint('Training', 'train_iters')
argparser.add_argument('--train_iters')
@property
def train_batch_size(self):
return self._config.getint('Training', 'train_batch_size')
argparser.add_argument('--train_batch_size')
@property
def test_batch_size(self):
return self._config.getint('Training', 'test_batch_size')
argparser.add_argument('--test_batch_size')
@property
def validate_every(self):
return self._config.getint('Training', 'validate_every')
argparser.add_argument('--validate_every')
@property
def print_every(self):
return self._config.getint('Training', 'print_every')
argparser.add_argument('--print_every')
@property
def save_every(self):
return self._config.getint('Training', 'save_every')
argparser.add_argument('--save_every')
@property
def per_process_gpu_memory_fraction(self):
return self._config.getfloat('Training', 'per_process_gpu_memory_fraction')
argparser.add_argument('--per_process_gpu_memory_fraction')