ModelScope中我想训练 damo/nlp_raner_named-entity-recognition_chinese-base-ecom-50cls 这么模型的时候 发现 没有对应的 Trainers?我的代码是 import os
import json
from modelscope.trainers import build_trainer
from modelscope.msdatasets import MsDataset
from modelscope.utils.hub import read_config
from modelscope.metainfo import Metrics
from modelscope.utils.constant import DownloadMode
model_id = 'damo/nlp_raner_named-entity-recognition_chinese-base-ecom-50cls'
max_epochs = 3
WORK_DIR = 'tmp'
train_dataset = MsDataset.load('./data/train.txt')
eval_dataset = MsDataset.load('./data/eval.txt')
device="cuda:3"
def cfg_modify_fn(cfg):
cfg.task = 'named-entity-recognition'
cfg.preprocessor = {
"train": {
"type": "sequence-labeling-preprocessor",
"mode": "train"
},
"val": {
"type": "sequence-labeling-preprocessor",
"mode": "eval"
}
}
cfg.pipeline = {
'type': 'sequence-labeling-model'
}
cfg.train.work_dir = WORK_DIR
cfg.train.max_epochs = max_epochs
# 设置评估metric
cfg.evaluation.metrics = 'ner-metric'
cfg.train.dataloader = {
"batch_size_per_gpu": 16,
"workers_per_gpu": 1
}
cfg.train.optimizer = {
"type": "AdamW",
"lr": 5e-5,
"options": {
"grad_clip": {
"max_norm": 2.0
}
}
}
cfg.train.lr_scheduler = {
"type": "StepLR",
"step_size": 2,
"options": {
"warmup": {
"type": "LinearWarmup",
"warmup_iters": 2
}
}
}
return cfg
kwargs = dict(
model=model_id,
train_dataset=train_dataset,
eval_dataset=eval_dataset,
cfg_modify_fn=cfg_modify_fn,
device=device
)
trainer = build_trainer('nlp_raner-trainer', default_args=kwargs)
print('===============================================================')
print('pre-trained model loaded, training started:')
print('===============================================================')
trainer.train()
print('===============================================================')
print('train success.')
print('===============================================================')
for i in range(max_epochs):
eval_results = trainer.evaluate(f'{WORKDIR}/epoch{i + 1}.pth')
print(f'epoch {i} evaluation result:')
print(eval_results)
print('===============================================================')
print('evaluate success')
print('===============================================================')
参考的是别的模型的例子
这个模型训练用adaseq
https://github.com/modelscope/AdaSeq,此回答整理自钉群“魔搭ModelScope开发者联盟群 ①”