hankcs / HanLP

Natural Language Processing for the next decade. Tokenization, Part-of-Speech Tagging, Named Entity Recognition, Syntactic & Semantic Dependency Parsing, Document Classification
https://hanlp.hankcs.com/en/
Apache License 2.0
33.72k stars 10.07k forks source link

UD 2.10 数据集依存句法分析模型训练报错 #1749

Closed MacGuffin230 closed 2 years ago

MacGuffin230 commented 2 years ago

Describe the bug A clear and concise description of what the bug is. UD 2.10 数据集训练报错

Code to reproduce the issue Provide a reproducible test case that is the bare minimum necessary to generate the problem.

ud = UniversalDependenciesParser()
ud.fit(
    trn_data=UD_210_MULTILINGUAL_TRAIN,
    dev_data=UD_210_MULTILINGUAL_DEV,
    save_dir=save_dir,
    transformer=ContextualWordEmbedding('token',
                            "nreimers/mMiniLMv2-L6-H384-distilled-from-XLMR-Large",
                            average_subwords=True,
                            max_sequence_length=256,
                            word_dropout=0.2),
    max_seq_len=256,
    sampler_builder=SortingSamplerBuilder(batch_size=16),
    epochs=10,
    seed=42,
    tree=True,
    dependencies='tok',
    mix_embedding=0,
    transform=NormalizeToken(dst="token",
                             src="token",
                             mapper={
                              "-LRB-": "(",
                              "-RRB-": ")",
                              "-LCB-": "{",
                              "-RCB-": "}",
                              "-LSB-": "[",
                              "-RSB-": "]",
                              "``": "\"",
                              "''": "\"",
                              "`": "'",
                              "«": "\"",
                              "»": "\"",
                              "‘": "'",
                              "’": "'",
                              "“": "\"",
                              "”": "\"",
                              "„": "\"",
                              "‹": "'",
                              "›": "'",
                              "–": "--",
                              "—": "--"}),
)

Describe the current behavior A clear and concise description of what happened.

RuntimeError Traceback (most recent call last)

in 37 "›": "'", 38 "–": "--", ---> 39 "—": "--"}), 40 ) ~/.local/lib/python3.6/site-packages/hanlp/components/parsers/ud/ud_parser.py in fit(self, trn_data, dev_data, save_dir, transformer, sampler_builder, mix_embedding, layer_dropout, n_mlp_arc, n_mlp_rel, mlp_dropout, lr, transformer_lr, patience, batch_size, epochs, gradient_accumulation, adam_epsilon, weight_decay, warmup_steps, grad_norm, tree, proj, punct, logger, verbose, devices, **kwargs) 210 verbose=True, 211 devices: Union[float, int, List[int]] = None, **kwargs): --> 212 return super().fit(**merge_locals_kwargs(locals(), kwargs)) 213 214 def execute_training_loop(self, trn: DataLoader, dev: DataLoader, epochs, criterion, optimizer, metric, save_dir, ~/.local/lib/python3.6/site-packages/hanlp/common/torch_component.py in fit(self, trn_data, dev_data, save_dir, batch_size, epochs, devices, logger, seed, finetune, eval_trn, _device_placeholder, **kwargs) 293 dev_data=dev_data, 294 eval_trn=eval_trn, --> 295 overwrite=True)) 296 297 def build_logger(self, name, save_dir): ~/.local/lib/python3.6/site-packages/hanlp/components/parsers/ud/ud_parser.py in execute_training_loop(self, trn, dev, epochs, criterion, optimizer, metric, save_dir, logger, devices, ratio_width, patience, eval_trn, **kwargs) 222 logger.info(f"[yellow]Epoch {epoch} / {epochs}:[/yellow]") 223 self.fit_dataloader(trn, criterion, optimizer, metric, logger, history=history, ratio_width=ratio_width, --> 224 eval_trn=eval_trn, **self.config) 225 loss, dev_metric = self.evaluate_dataloader(dev, criterion, metric, logger=logger, ratio_width=ratio_width) 226 timer.update() ~/.local/lib/python3.6/site-packages/hanlp/components/parsers/ud/ud_parser.py in fit_dataloader(self, trn, criterion, optimizer, metric, logger, history, gradient_accumulation, grad_norm, ratio_width, eval_trn, **kwargs) 271 total_loss += loss.item() 272 if eval_trn: --> 273 self.decode_output(out, mask, batch) 274 self.update_metrics(metric, batch, out, mask) 275 if history.step(gradient_accumulation): ~/.local/lib/python3.6/site-packages/hanlp/components/parsers/ud/ud_parser.py in decode_output(self, outputs, mask, batch) 285 arc_scores, rel_scores = outputs['class_probabilities']['deps']['s_arc'], \ 286 outputs['class_probabilities']['deps']['s_rel'] --> 287 arc_preds, rel_preds = BiaffineDependencyParser.decode(self, arc_scores, rel_scores, mask, batch) 288 outputs['arc_preds'], outputs['rel_preds'] = arc_preds, rel_preds 289 return outputs ~/.local/lib/python3.6/site-packages/hanlp/components/parsers/biaffine/biaffine_dep.py in decode(self, arc_scores, rel_scores, mask, batch) 544 tree, proj = self.config.tree, self.config.get('proj', False) 545 if tree: --> 546 arc_preds = decode_dep(arc_scores, mask, tree, proj) 547 else: 548 arc_preds = arc_scores.argmax(-1) ~/.local/lib/python3.6/site-packages/hanlp/components/parsers/alg.py in decode_dep(s_arc, mask, tree, proj) 757 alg = mst 758 s_arc.diagonal(0, 1, 2)[1:].fill_(float('-inf')) --> 759 arc_preds[bad] = alg(s_arc[bad], mask[bad]) 760 761 return arc_preds ~/.local/lib/python3.6/site-packages/hanlp/components/parsers/alg.py in mst(scores, mask, multiroot) 555 s[:, 0] = float('-inf') 556 s[root, 0] = s_root[root] --> 557 t = chuliu_edmonds(s) 558 s_tree = s[1:].gather(1, t[1:].unsqueeze(-1)).sum() 559 if s_tree > s_best: ~/.local/lib/python3.6/site-packages/hanlp/components/parsers/alg.py in chuliu_edmonds(s) 484 485 # y is the contracted tree --> 486 y = chuliu_edmonds(s) 487 # exclude head of cycle from y 488 y, cycle_head = y[:-1], y[-1] ~/.local/lib/python3.6/site-packages/hanlp/components/parsers/alg.py in chuliu_edmonds(s) 484 485 # y is the contracted tree --> 486 y = chuliu_edmonds(s) 487 # exclude head of cycle from y 488 y, cycle_head = y[:-1], y[-1] ~/.local/lib/python3.6/site-packages/hanlp/components/parsers/alg.py in chuliu_edmonds(s) 484 485 # y is the contracted tree --> 486 y = chuliu_edmonds(s) 487 # exclude head of cycle from y 488 y, cycle_head = y[:-1], y[-1] ~/.local/lib/python3.6/site-packages/hanlp/components/parsers/alg.py in chuliu_edmonds(s) 484 485 # y is the contracted tree --> 486 y = chuliu_edmonds(s) 487 # exclude head of cycle from y 488 y, cycle_head = y[:-1], y[-1] ~/.local/lib/python3.6/site-packages/hanlp/components/parsers/alg.py in chuliu_edmonds(s) 484 485 # y is the contracted tree --> 486 y = chuliu_edmonds(s) 487 # exclude head of cycle from y 488 y, cycle_head = y[:-1], y[-1] ~/.local/lib/python3.6/site-packages/hanlp/components/parsers/alg.py in chuliu_edmonds(s) 484 485 # y is the contracted tree --> 486 y = chuliu_edmonds(s) 487 # exclude head of cycle from y 488 y, cycle_head = y[:-1], y[-1] ~/.local/lib/python3.6/site-packages/hanlp/components/parsers/alg.py in chuliu_edmonds(s) 484 485 # y is the contracted tree --> 486 y = chuliu_edmonds(s) 487 # exclude head of cycle from y 488 y, cycle_head = y[:-1], y[-1] ~/.local/lib/python3.6/site-packages/hanlp/components/parsers/alg.py in chuliu_edmonds(s) 484 485 # y is the contracted tree --> 486 y = chuliu_edmonds(s) 487 # exclude head of cycle from y 488 y, cycle_head = y[:-1], y[-1] ~/.local/lib/python3.6/site-packages/hanlp/components/parsers/alg.py in chuliu_edmonds(s) 484 485 # y is the contracted tree --> 486 y = chuliu_edmonds(s) 487 # exclude head of cycle from y 488 y, cycle_head = y[:-1], y[-1] ~/.local/lib/python3.6/site-packages/hanlp/components/parsers/alg.py in chuliu_edmonds(s) 484 485 # y is the contracted tree --> 486 y = chuliu_edmonds(s) 487 # exclude head of cycle from y 488 y, cycle_head = y[:-1], y[-1] ~/.local/lib/python3.6/site-packages/hanlp/components/parsers/alg.py in chuliu_edmonds(s) 484 485 # y is the contracted tree --> 486 y = chuliu_edmonds(s) 487 # exclude head of cycle from y 488 y, cycle_head = y[:-1], y[-1] ~/.local/lib/python3.6/site-packages/hanlp/components/parsers/alg.py in chuliu_edmonds(s) 484 485 # y is the contracted tree --> 486 y = chuliu_edmonds(s) 487 # exclude head of cycle from y 488 y, cycle_head = y[:-1], y[-1] ~/.local/lib/python3.6/site-packages/hanlp/components/parsers/alg.py in chuliu_edmonds(s) 484 485 # y is the contracted tree --> 486 y = chuliu_edmonds(s) 487 # exclude head of cycle from y 488 y, cycle_head = y[:-1], y[-1] ~/.local/lib/python3.6/site-packages/hanlp/components/parsers/alg.py in chuliu_edmonds(s) 484 485 # y is the contracted tree --> 486 y = chuliu_edmonds(s) 487 # exclude head of cycle from y 488 y, cycle_head = y[:-1], y[-1] ~/.local/lib/python3.6/site-packages/hanlp/components/parsers/alg.py in chuliu_edmonds(s) 484 485 # y is the contracted tree --> 486 y = chuliu_edmonds(s) 487 # exclude head of cycle from y 488 y, cycle_head = y[:-1], y[-1] ~/.local/lib/python3.6/site-packages/hanlp/components/parsers/alg.py in chuliu_edmonds(s) 484 485 # y is the contracted tree --> 486 y = chuliu_edmonds(s) 487 # exclude head of cycle from y 488 y, cycle_head = y[:-1], y[-1] ~/.local/lib/python3.6/site-packages/hanlp/components/parsers/alg.py in chuliu_edmonds(s) 484 485 # y is the contracted tree --> 486 y = chuliu_edmonds(s) 487 # exclude head of cycle from y 488 y, cycle_head = y[:-1], y[-1] ~/.local/lib/python3.6/site-packages/hanlp/components/parsers/alg.py in chuliu_edmonds(s) 484 485 # y is the contracted tree --> 486 y = chuliu_edmonds(s) 487 # exclude head of cycle from y 488 y, cycle_head = y[:-1], y[-1] ~/.local/lib/python3.6/site-packages/hanlp/components/parsers/alg.py in chuliu_edmonds(s) 484 485 # y is the contracted tree --> 486 y = chuliu_edmonds(s) 487 # exclude head of cycle from y 488 y, cycle_head = y[:-1], y[-1] ~/.local/lib/python3.6/site-packages/hanlp/components/parsers/alg.py in chuliu_edmonds(s) 484 485 # y is the contracted tree --> 486 y = chuliu_edmonds(s) 487 # exclude head of cycle from y 488 y, cycle_head = y[:-1], y[-1] ~/.local/lib/python3.6/site-packages/hanlp/components/parsers/alg.py in chuliu_edmonds(s) 484 485 # y is the contracted tree --> 486 y = chuliu_edmonds(s) 487 # exclude head of cycle from y 488 y, cycle_head = y[:-1], y[-1] ~/.local/lib/python3.6/site-packages/hanlp/components/parsers/alg.py in chuliu_edmonds(s) 484 485 # y is the contracted tree --> 486 y = chuliu_edmonds(s) 487 # exclude head of cycle from y 488 y, cycle_head = y[:-1], y[-1] ~/.local/lib/python3.6/site-packages/hanlp/components/parsers/alg.py in chuliu_edmonds(s) 484 485 # y is the contracted tree --> 486 y = chuliu_edmonds(s) 487 # exclude head of cycle from y 488 y, cycle_head = y[:-1], y[-1] ~/.local/lib/python3.6/site-packages/hanlp/components/parsers/alg.py in chuliu_edmonds(s) 484 485 # y is the contracted tree --> 486 y = chuliu_edmonds(s) 487 # exclude head of cycle from y 488 y, cycle_head = y[:-1], y[-1] ~/.local/lib/python3.6/site-packages/hanlp/components/parsers/alg.py in chuliu_edmonds(s) 448 cycle = torch.tensor(cycle) 449 # indices of noncycle in the original tree --> 450 noncycle = torch.ones(len(s)).index_fill_(0, cycle, 0) 451 noncycle = torch.where(noncycle.gt(0))[0] 452 RuntimeError: unknown parameter type **Expected behavior** A clear and concise description of what you expected to happen. **System information** - OS Platform and Distribution (e.g., Linux Ubuntu 16.04): Ubuntu 18.04.6 LTS - Python version: 3.6.5 - HanLP version: 2.1.0b32 **Other info / logs** Include any logs or source code that would be helpful to diagnose the problem. If including tracebacks, please include the full traceback. Large logs and files should be attached. * [x] I've completed this form and searched the web for solutions.
hankcs commented 2 years ago

在dev上训练了1个epoch,一切正常,无法复现:https://colab.research.google.com/drive/1rbTYJzwtGp5NB5Z65P5Isx5cvqK3zgy3?usp=sharing

可能是你的torch版本问题,试试最新的LTS版?

MacGuffin230 commented 2 years ago

使用UD_210_MULTILINGUAL_DEV做训练集是正常的,使用UD_210_MULTILINGUAL_TRAIN就会报那个错误

hankcs commented 2 years ago

如果你能把chuliu_edmonds的参数dump下来,我可能可以帮到你。

MacGuffin230 commented 2 years ago

能提供一个使用UD2.10数据集进行训练的demo吗?我是不是参数或者模型选的不对?