taishan1994 / pytorch_bert_bilstm_crf_ner

基于pytorch的bert_bilstm_crf中文命名实体识别
517 stars 81 forks source link

导出onnx问题 Error(s) in loading state_dict for BertNerModel: Unexpected key(s) in state_dict: "linear.weight", "linear.bias". #42

Closed fightingst closed 1 year ago

fightingst commented 1 year ago

使用bert_cr模型导出onnx失败,大佬有空帮忙看一下

训练

python main.py --bert_dir="./model_hub/chinese-bert-wwm-ext/" --data_dir="./data/xxxx/" --data_name="xxx" --model_name="bert" --log_dir="./logs/" --output_dir="./checkpoints/" --num_tags=5 --seed=123 --gpu_ids="0" --max_seq_len=400 --lr=3e-5 --crf_lr=3e-2 --other_lr=3e-4 --train_batch_size=16 --train_epochs=3 --eval_batch_size=16 --max_grad_norm=1 --warmup_proportion=0.1 --adam_epsilon=1e-8 --weight_decay=0.01 --lstm_hidden=128 --num_layers=1 --use_lstm="False" --use_crf="True" --dropout_prob=0.1 --dropout=0.1 ... ... ...

导出

python convert_onnx.py Traceback (most recent call last): File "convert_onnx.py", line 131, in model.load_state_dict(torch.load(model_path, map_location=torch.device('cuda')), strict=True) File "/root/miniconda3/envs/myconda/lib/python3.7/site-packages/torch/nn/modules/module.py", line 1045, in load_state_dict self.class.name, "\n\t".join(error_msgs))) RuntimeError: Error(s) in loading state_dict for BertNerModel: Unexpected key(s) in state_dict: "linear.weight", "linear.bias".

taishan1994 commented 1 year ago

使用bert_cr模型导出onnx失败,大佬有空帮忙看一下

更新一下convert_connx下面模型的代码。

fightingst commented 1 year ago

使用bert_cr模型导出onnx失败,大佬有空帮忙看一下

更新一下convert_connx下面模型的代码。

感谢感谢!!(大佬解决问题真的快,属实吾辈楷模)