-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfig.template.jsonnet
392 lines (386 loc) · 15.2 KB
/
config.template.jsonnet
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
########################################################################################
# BASIC configuration #
########################################################################################
# Training data path, str
# Must be in CONNLU format (or it's extended version with semantic relation field).
# Can accepted multiple paths when concatenated with ',', "path1,path2"
local training_data_path = std.extVar("training_data_path");
# Validation data path, str
# Can accepted multiple paths when concatenated with ',', "path1,path2"
local validation_data_path = if std.length(std.extVar("validation_data_path")) > 0 then std.extVar("validation_data_path");
# Path to pretrained tokens, str or null
local pretrained_tokens = if std.length(std.extVar("pretrained_tokens")) > 0 then std.extVar("pretrained_tokens");
# Name of pretrained transformer model, str or null
local pretrained_transformer_name = if std.length(std.extVar("pretrained_transformer_name")) > 0 then std.extVar("pretrained_transformer_name");
# Learning rate value, float
local learning_rate = 0.002;
# Number of epochs, int
local num_epochs = std.parseInt(std.extVar("num_epochs"));
# Cuda device id, -1 for cpu, int
local cuda_device = std.parseInt(std.extVar("cuda_device"));
# Minimum number of words in batch, int
local word_batch_size = std.parseInt(std.extVar("word_batch_size"));
# Features used as input, list of str
# Choice "upostag", "xpostag", "lemma"
# Required "token", "char"
local features = std.split(std.extVar("features"), " ");
# Targets of the model, list of str
# Choice "feats", "lemma", "upostag", "xpostag", "semrel". "sent"
# Required "deprel", "head"
local targets = std.split(std.extVar("targets"), " ");
# Word embedding dimension, int
# If pretrained_tokens is not null must much provided dimensionality
local embedding_dim = std.parseInt(std.extVar("embedding_dim"));
# Dropout rate on predictors, float
# All of the models on top of the encoder uses this dropout
local predictors_dropout = 0.25;
# Xpostag embedding dimension, int
# (discarded if xpostag not in features)
local xpostag_dim = 32;
# Upostag embedding dimension, int
# (discarded if upostag not in features)
local upostag_dim = 32;
# Feats embedding dimension, int
# (discarded if feats not in featres)
local feats_dim = 32;
# Lemma embedding dimension, int
# (discarded if lemma not in features)
local lemma_char_dim = 64;
# Character embedding dim, int
local char_dim = 64;
# Word embedding projection dim, int
local projected_embedding_dim = 100;
# Loss weights, dict[str, int]
local loss_weights = {
xpostag: 0.05,
upostag: 0.05,
lemma: 0.05,
feats: 0.2,
deprel: 0.8,
head: 0.2,
semrel: 0.05,
};
# Encoder hidden size, int
local hidden_size = 512;
# Number of layers in the encoder, int
local num_layers = 2;
# Cycle loss iterations, int
local cycle_loss_n = 0;
# Maximum length of the word, int
# Shorter words are padded, longer - truncated
local word_length = 30;
# Whether to use tensorboard, bool
local use_tensorboard = if std.extVar("use_tensorboard") == "True" then true else false;
# Path for tensorboard metrics, str
local metrics_dir = "./runs";
# Helper functions
local in_features(name) = !(std.length(std.find(name, features)) == 0);
local in_targets(name) = !(std.length(std.find(name, targets)) == 0);
local use_transformer = pretrained_transformer_name != null;
# Verify some configuration requirements
assert in_features("token"): "Key 'token' must be in features!";
assert in_features("char"): "Key 'char' must be in features!";
assert in_targets("deprel"): "Key 'deprel' must be in targets!";
assert in_targets("head"): "Key 'head' must be in targets!";
assert pretrained_tokens == null || pretrained_transformer_name == null: "Can't use pretrained tokens and pretrained transformer at the same time!";
########################################################################################
# ADVANCED configuration #
########################################################################################
# Detailed dataset, training, vocabulary and model configuration.
{
# Configuration type (default or finetuning), str
type: std.extVar('type'),
# Datasets used for vocab creation, list of str
# Choice "train", "valid"
datasets_for_vocab_creation: ['train'],
# Path to training data, str
train_data_path: training_data_path,
# Path to validation data, str
validation_data_path: validation_data_path,
# Dataset reader configuration (conllu format)
dataset_reader: {
type: "conllu",
features: features,
targets: targets,
# Whether data contains semantic relation field, bool
use_sem: if in_targets("semrel") then true else false,
token_indexers: {
token: if use_transformer then {
type: "pretrained_transformer_mismatched_fixed",
model_name: pretrained_transformer_name,
tokenizer_kwargs: if std.startsWith(pretrained_transformer_name, "allegro/herbert")
then {use_fast: false} else {},
} else {
# SingleIdTokenIndexer, token as single int
type: "single_id",
},
upostag: {
type: "single_id",
namespace: "upostag",
feature_name: "pos_",
},
xpostag: {
type: "single_id",
namespace: "xpostag",
feature_name: "tag_",
},
lemma: {
type: "characters_const_padding",
character_tokenizer: {
start_tokens: ["__START__"],
end_tokens: ["__END__"],
},
# +2 for start and end token
min_padding_length: word_length + 2,
},
char: {
type: "characters_const_padding",
character_tokenizer: {
start_tokens: ["__START__"],
end_tokens: ["__END__"],
},
# +2 for start and end token
min_padding_length: word_length + 2,
},
feats: {
type: "feats_indexer",
},
},
lemma_indexers: {
char: {
type: "characters_const_padding",
namespace: "lemma_characters",
character_tokenizer: {
start_tokens: ["__START__"],
end_tokens: ["__END__"],
},
# +2 for start and end token
min_padding_length: word_length + 2,
},
},
},
# Data loader configuration
data_loader: {
batch_sampler: {
type: "token_count",
word_batch_size: word_batch_size,
},
},
# Vocabulary configuration
vocabulary: std.prune({
type: "from_instances_extended",
only_include_pretrained_words: true,
pretrained_files: {
tokens: pretrained_tokens,
},
oov_token: "_",
padding_token: "__PAD__",
non_padded_namespaces: ["head_labels"],
}),
model: std.prune({
type: "semantic_multitask",
text_field_embedder: {
type: "basic",
token_embedders: {
xpostag: if in_features("xpostag") then {
type: "embedding",
padding_index: 0,
embedding_dim: xpostag_dim,
vocab_namespace: "xpostag",
},
upostag: if in_features("upostag") then {
type: "embedding",
padding_index: 0,
embedding_dim: upostag_dim,
vocab_namespace: "upostag",
},
token: if use_transformer then {
type: "transformers_word_embeddings",
model_name: pretrained_transformer_name,
projection_dim: projected_embedding_dim,
tokenizer_kwargs: if std.startsWith(pretrained_transformer_name, "allegro/herbert")
then {use_fast: false} else {},
} else {
type: "embeddings_projected",
embedding_dim: embedding_dim,
projection_layer: {
in_features: embedding_dim,
out_features: projected_embedding_dim,
dropout_rate: 0.25,
activation: "tanh"
},
vocab_namespace: "tokens",
pretrained_file: pretrained_tokens,
trainable: if pretrained_tokens == null then true else false,
},
char: {
type: "char_embeddings_from_config",
embedding_dim: char_dim,
dilated_cnn_encoder: {
input_dim: char_dim,
filters: [512, 256, char_dim],
kernel_size: [3, 3, 3],
stride: [1, 1, 1],
padding: [1, 2, 4],
dilation: [1, 2, 4],
activations: ["relu", "relu", "linear"],
},
},
lemma: if in_features("lemma") then {
type: "char_embeddings_from_config",
embedding_dim: lemma_char_dim,
dilated_cnn_encoder: {
input_dim: lemma_char_dim,
filters: [512, 256, lemma_char_dim],
kernel_size: [3, 3, 3],
stride: [1, 1, 1],
padding: [1, 2, 4],
dilation: [1, 2, 4],
activations: ["relu", "relu", "linear"],
},
},
feats: if in_features("feats") then {
type: "feats_embedding",
padding_index: 0,
embedding_dim: feats_dim,
vocab_namespace: "feats",
},
},
},
loss_weights: loss_weights,
seq_encoder: {
type: "combo_encoder",
layer_dropout_probability: 0.33,
stacked_bilstm: {
input_size:
(char_dim + projected_embedding_dim +
(if in_features('xpostag') then xpostag_dim else 0) +
(if in_features('lemma') then lemma_char_dim else 0) +
(if in_features('upostag') then upostag_dim else 0) +
(if in_features('feats') then feats_dim else 0)),
hidden_size: hidden_size,
num_layers: num_layers,
recurrent_dropout_probability: 0.33,
layer_dropout_probability: 0.33
},
},
dependency_relation: {
type: "combo_dependency_parsing_from_vocab",
vocab_namespace: 'deprel_labels',
head_predictor: {
local projection_dim = 512,
cycle_loss_n: cycle_loss_n,
head_projection_layer: {
in_features: hidden_size * 2,
out_features: projection_dim,
activation: "tanh",
},
dependency_projection_layer: {
in_features: hidden_size * 2,
out_features: projection_dim,
activation: "tanh",
},
},
local projection_dim = 128,
head_projection_layer: {
in_features: hidden_size * 2,
out_features: projection_dim,
dropout_rate: predictors_dropout,
activation: "tanh"
},
dependency_projection_layer: {
in_features: hidden_size * 2,
out_features: projection_dim,
dropout_rate: predictors_dropout,
activation: "tanh"
},
},
morphological_feat: if in_targets("feats") then {
type: "combo_morpho_from_vocab",
vocab_namespace: "feats_labels",
input_dim: hidden_size * 2,
hidden_dims: [128],
activations: ["tanh", "linear"],
dropout: [predictors_dropout, 0.0],
num_layers: 2,
},
lemmatizer: if in_targets("lemma") then {
type: "combo_lemma_predictor_from_vocab",
char_vocab_namespace: "token_characters",
lemma_vocab_namespace: "lemma_characters",
embedding_dim: 256,
input_projection_layer: {
in_features: hidden_size * 2,
out_features: 32,
dropout_rate: predictors_dropout,
activation: "tanh"
},
filters: [256, 256, 256],
kernel_size: [3, 3, 3, 1],
stride: [1, 1, 1, 1],
padding: [1, 2, 4, 0],
dilation: [1, 2, 4, 1],
activations: ["relu", "relu", "relu", "linear"],
},
upos_tagger: if in_targets("upostag") then {
input_dim: hidden_size * 2,
hidden_dims: [64],
activations: ["tanh", "linear"],
dropout: [predictors_dropout, 0.0],
num_layers: 2,
vocab_namespace: "upostag_labels"
},
xpos_tagger: if in_targets("xpostag") then {
input_dim: hidden_size * 2,
hidden_dims: [128],
activations: ["tanh", "linear"],
dropout: [predictors_dropout, 0.0],
num_layers: 2,
vocab_namespace: "xpostag_labels"
},
semantic_relation: if in_targets("semrel") then {
input_dim: hidden_size * 2,
hidden_dims: [64],
activations: ["tanh", "linear"],
dropout: [predictors_dropout, 0.0],
num_layers: 2,
vocab_namespace: "semrel_labels"
},
regularizer: {
regexes: [
[".*conv1d.*", {type: "l2", alpha: 1e-6}],
[".*forward.*", {type: "l2", alpha: 1e-6}],
[".*backward.*", {type: "l2", alpha: 1e-6}],
[".*char_embed.*", {type: "l2", alpha: 1e-5}],
],
},
}),
trainer: std.prune({
checkpointer: {
type: "finishing_only_checkpointer",
},
type: "gradient_descent_validate_n",
cuda_device: cuda_device,
grad_clipping: 5.0,
num_epochs: num_epochs,
optimizer: {
type: "adam",
lr: learning_rate,
betas: [0.9, 0.9],
},
patience: 1, # it will be overwriten by callback
epoch_callbacks: [
{ type: "transfer_patience" },
],
learning_rate_scheduler: {
type: "combo_scheduler",
},
tensorboard_writer: if use_tensorboard then {
serialization_dir: metrics_dir,
should_log_learning_rate: false,
should_log_parameter_statistics: false,
summary_interval: 100,
},
validation_metric: "+EM",
}),
}