yzhangcs / parser

:rocket: State-of-the-art parsers for natural language.
https://parser.yzhang.site/
MIT License
827 stars 139 forks source link

SuPar 1.1.0 cannot load 1.0.1 models #67

Closed KoichiYasuoka closed 3 years ago

KoichiYasuoka commented 3 years ago

Hi @yzhangcs I've tried to use new SuPar 1.1.0, but it doesn't load my biaffine models:

>>> from supar import Parser
>>> nlp=Parser.load("my-biaffine-lzh")
  File "/home/yasuoka/.local/lib/python3.7/site-packages/supar/parsers/parser.py", line 167, in load
    return cls(args, model, transform)
  File "/home/yasuoka/.local/lib/python3.7/site-packages/supar/parsers/dep.py", line 32, in __init__
    self.WORD, self.TEXT, self.CHAR, self.BERT = self.transform.FORM
ValueError: not enough values to unpack (expected 4, got 2)

How do I do? In my models self.transform.FORM is a tuple of 2 values (words and bert):

((words): Field(pad=<pad>, unk=<unk>, bos=<bos>, lower=True), (bert): SubwordField(pad=[PAD], unk=[UNK], bos=[CLS]))
yzhangcs commented 3 years ago

Hi @KoichiYasuoka, some code has been changed, I think you may retrain your model from scratch, or rebuild a parser and replace transform in your previous parser with the new built one.

KoichiYasuoka commented 3 years ago

Thank you for the information @yzhangcs , I've just tried to retrain my model with supar==1.1.0 transformers==4.5.1 pytorch==1.8.1+cu101 and UD_Classical_Chinese-Kyoto:

$ python3 -m supar.cmds.biaffine_dep train -b -d 0 -p my-biaffine-lzh -f bert --bert KoichiYasuoka/roberta-classical-chinese-base-char --train train.conllu --dev dev.conllu --test test.conllu --embed=''

2021-04-28 13:45:41 INFO
---------------------+-------------------------------
Param                |             Value
---------------------+-------------------------------
tree                 |             False
proj                 |             False
partial              |             False
mode                 |             train
path                 |        my-biaffine-lzh
device               |               0
seed                 |               1
threads              |               16
batch_size           |              5000
local_rank           |               -1
feat                 |            ['bert']
build                |              True
encoder              |              lstm
punct                |             False
max_len              |              None
buckets              |               32
train                |          train.conllu
dev                  |           dev.conllu
test                 |          test.conllu
embed                |
unk                  |              unk
n_embed              |              100
bert                 | KoichiYasuoka/roberta-classical-chinese-base-char
---------------------+-------------------------------

2021-04-28 13:45:41 INFO Building the fields
Using bos_token, but it is not set yet.
2021-04-28 13:45:45 INFO CoNLL(
 (words): Field(pad=<pad>, unk=<unk>, bos=<bos>, lower=True)
 (texts): RawField()
 (bert): SubwordField(pad=[PAD], unk=[UNK], bos=[CLS])
 (arcs): Field(bos=<bos>, use_vocab=False)
 (rels): Field(bos=<bos>)
)
2021-04-28 13:45:45 INFO Building the model
2021-04-28 13:45:51 INFO BiaffineDependencyModel(
  (word_embed): Embedding(3657, 100)
  (bert_embed): TransformerEmbedding(KoichiYasuoka/roberta-classical-chinese-base-char, n_layers=4, n_out=100, stride=256, pooling=mean, pad_index=1)
  (embed_dropout): IndependentDropout(p=0.33)
  (encoder): VariationalLSTM(200, 400, num_layers=3, bidirectional=True, dropout=0.33)
  (encoder_dropout): SharedDropout(p=0.33, batch_first=True)
  (arc_mlp_d): MLP(n_in=800, n_out=500, dropout=0.33)
  (arc_mlp_h): MLP(n_in=800, n_out=500, dropout=0.33)
  (rel_mlp_d): MLP(n_in=800, n_out=100, dropout=0.33)
  (rel_mlp_h): MLP(n_in=800, n_out=100, dropout=0.33)
  (arc_attn): Biaffine(n_in=500, bias_x=True)
  (rel_attn): Biaffine(n_in=100, n_out=40, bias_x=True, bias_y=True)
  (criterion): CrossEntropyLoss()
)

2021-04-28 13:45:51 INFO Loading the data
2021-04-28 13:46:06 INFO
train: Dataset(n_sentences=45937, n_batches=78, n_buckets=32)
dev:   Dataset(n_sentences=5743, n_batches=25, n_buckets=25)
test:  Dataset(n_sentences=5743, n_batches=26, n_buckets=26)

Traceback (most recent call last):
  File "/usr/lib/python3.7/runpy.py", line 194, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "/usr/lib/python3.7/runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "/home/yasuoka/.local/lib/python3.7/site-packages/supar/cmds/biaffine_dep.py", line 46, in <module>
    main()
  File "/home/yasuoka/.local/lib/python3.7/site-packages/supar/cmds/biaffine_dep.py", line 42, in main
    parse(parser)
  File "/home/yasuoka/.local/lib/python3.7/site-packages/supar/cmds/cmd.py", line 30, in parse
    parser.train(**args)
  File "/home/yasuoka/.local/lib/python3.7/site-packages/supar/parsers/dep.py", line 62, in train
    return super().train(**Config().update(locals()))
  File "/home/yasuoka/.local/lib/python3.7/site-packages/supar/parsers/parser.py", line 49, in train
    self.optimizer = Adam(self.model.parameters(), args.lr, (args.mu, args.nu), args.eps, args.weight_decay)
AttributeError: 'Config' object has no attribute 'lr'

Umm... The parameters are the same with old supar.cmds.biaffine_dependency of SuPar 1.0.1. Where should I change?

yzhangcs commented 3 years ago

@KoichiYasuoka you should use -c option to specify a config file, e.g., biaffine-dep-en or a local file.

KoichiYasuoka commented 3 years ago

Thank you @yzhangcs, I've just finished to retrain all models in my SuPar-Kanbun and SuPar-Unidic. But it took three days under my GPU... If you update Parser in the future, please keep predict compatible backward.

yzhangcs commented 3 years ago

@KoichiYasuoka Thank you for your advice. In fact I just wrote a script to convert params and vars of previous models to the new format. Maybe it's a cheaper way If there are too many models.