From 318103eac3092cb8da6d4014eb3617facf7c2c27 Mon Sep 17 00:00:00 2001 From: shengyumao <619397525@qq.com> Date: Wed, 28 Jun 2023 18:41:40 +0800 Subject: [PATCH] update ee --- example/ee/standard/README.md | 30 ++++++++++++++++++--------- example/ee/standard/README_CN.md | 30 ++++++++++++++++++--------- example/ee/standard/conf/predict.yaml | 8 +++---- example/ee/standard/conf/train.yaml | 12 +++++------ example/ee/standard/predict.py | 12 ++++++----- example/ee/standard/requirements.txt | 7 +++---- 6 files changed, 60 insertions(+), 39 deletions(-) diff --git a/example/ee/standard/README.md b/example/ee/standard/README.md index 2afc7951..529f3356 100644 --- a/example/ee/standard/README.md +++ b/example/ee/standard/README.md @@ -12,6 +12,7 @@ ```bash python==3.8 pip install -r requirements.txt + pip install hydra-core==1.3.1 # ignore the conlict with deepke ``` ## Download Code @@ -30,30 +31,39 @@ Follow the instruction [here](./data/DuEE/README.md) ## Train -Modify the parameters in `./conf/train.yaml`. +Modify the parameters in `./conf/train.yaml`. Select different dataset by set `data_name`, and change the `model_name_or_path` for different dataset. -- Trigger +- Trigger (Event Detection or Trigger Classification) + + First train trigger classification model, and predict the trigger of each instance. + Set `task_name` to `trigger`. - Select different dataset by set `data_name`. Then run the following command: + ```bash python run.py ``` + + The prediction will be conducted after the training, and the result will be in `exp/xx/trigger/xxx/eval_pred.json`. -- Role - Here we train the event arguments extraction model with the gold trigger. +Then train the event arguments extraction model by the gold trigger. +- Role (Event Arguments Extraction) + Then, we train the event arguements extraction models, here we train the event arguments extraction model with the gold trigger. Set `task_name` to `role`. - Select different dataset by set `data_name`. Then run the following command: + ```bash python run.py ``` -## Predict +## Predict (Event Arguments Extraction) + +The trigger prediction has been conducted during training, and the result is in the `output_dir`. Here we predict the event arguments extraction results with pred trigger result. + +Modify the parameters in `./conf/predict.yaml`. Set the `model_name_or_path` to the trained role model path, and `do_pipeline_predict=True` to do the pipeline prediction. -The trigger prediction has been conducted during training, and the result is in the `output_dir`.Here we predict the event arguments extraction results with pred trigger result. -Modify the parameters in `./conf/predict.yaml`. Then run the following command: ```bash python predict.py -``` \ No newline at end of file +``` +The final result will be in `eval_pred.json` of the role model path folder. \ No newline at end of file diff --git a/example/ee/standard/README_CN.md b/example/ee/standard/README_CN.md index e41fdbcd..c1a233dd 100644 --- a/example/ee/standard/README_CN.md +++ b/example/ee/standard/README_CN.md @@ -12,6 +12,7 @@ ```bash python==3.8 pip install -r requirements.txt + pip install hydra-core==1.3.1 # ignore the conlict with deepke ``` ## 克隆代码 @@ -31,30 +32,39 @@ cd DeepKE/example/ee/standard ## 训练 -在`./conf/train.yaml`中修改模型参数 +在`./conf/train.yaml`中修改训练参数。可以通过更改`data_name`参数来选择不同的数据集,并将`model_name_or_path`改成对应的模型。 + +整个事件抽取的训练分为两部分,第一部分是训练触发词分类的模型,第二部分是训练事件角色抽取的模型。 - Trigger 触发词 + + 首先是完成每个instance的触发词的抽取, + 将`task_name`设置为`trigger`。 - 可以通过更改`data_name`参数来选择不同的数据集。 然后运行下述命令 + ```bash python run.py ``` - -- 事件角色 - 在这里我们用正确的trigger训练事件元素抽取模型 + + 在训练后会完成触发词的预测,结果保存在`exp/xx/trigger/xxx/eval_pred.json`。 + +- 事件元素 (Event Arguments Extraction) + 然后是训练一个事件角色抽取的模型,在这里我们用正确的trigger训练事件元素抽取模型。 将`task_name`设置为`role`。 - 可以通过更改`data_name`参数来选择不同的数据集。 然后运行下述命令 + ```bash python run.py ``` -## 预测 +## 预测 (Event Arguments Extraction) -触发词的预测在训练的过程中会完成,预测的结果在`output_dir`中。在这里我们使用预测得到的触发词来抽取事件抽取元素。 -在`./conf/predict.yaml`中修改模型参数。 +触发词的预测在训练的过程中会完成,预测的结果在`output_dir`中。在这里我们使用预测得到的触发词来完成pipeline的事件元素抽取。 +在`./conf/predict.yaml`中修改模型参数。将`model_name_or_path`设置为训练好的事件元素抽取模型的路径,并且设置`do_pipeline_predict=True`来完成pipeline的事件抽取。 然后运行下述命令 + ```bash python predict.py -``` \ No newline at end of file +``` +最后的预测结果会在role模型对应目录的`eval_pred.json`下。 \ No newline at end of file diff --git a/example/ee/standard/conf/predict.yaml b/example/ee/standard/conf/predict.yaml index 9bb6afdd..b505074c 100644 --- a/example/ee/standard/conf/predict.yaml +++ b/example/ee/standard/conf/predict.yaml @@ -1,8 +1,8 @@ defaults: - train -data_name: ACE # [ACE, DuEE] -model_name_or_path: ./exp/ACE/role/bert-base-uncased +data_name: DuEE # [ACE, DuEE] +model_name_or_path: ./exp/DuEE/role/bert-base-chinese task_name: role # the trigger prediction is done during the training process. do_train: False do_eval: True @@ -12,5 +12,5 @@ do_pipeline_predict: True overwrite_cache: True -dev_trigger_pred_file: ./exp/ACE/trigger/bert-base-uncased/eval_pred.json -test_trigger_pred_file: ./exp/ACE/trigger/bert-base-uncased/test_pred.json +dev_trigger_pred_file: ./exp/DuEE/trigger/bert-base-chinese/eval_pred.json # change to your pred file of trigger classification +test_trigger_pred_file: ./exp/DuEE/trigger/bert-base-chinese/test_pred.json diff --git a/example/ee/standard/conf/train.yaml b/example/ee/standard/conf/train.yaml index e73dbe66..52ef9331 100644 --- a/example/ee/standard/conf/train.yaml +++ b/example/ee/standard/conf/train.yaml @@ -1,6 +1,6 @@ -data_name: ACE # [ACE, DuEE] -model_name_or_path: ../../../../models/bert-base-uncased # [bert-base-uncased, bert-base-chinese] english for ace, chinese for duee -task_name: role # [trigger, role] +data_name: DuEE # [ACE, DuEE] +model_name_or_path: /newdisk1/msy/models/bert-base-chinese # [bert-base-uncased, bert-base-chinese] english for ace, chinese for duee +task_name: trigger # [trigger, role] model_type: bertcrf do_train: True do_eval: True @@ -19,8 +19,8 @@ per_gpu_eval_batch_size: 16 gradient_accumulation_steps: 1 max_seq_length: 256 max_grad_norm: 1.0 -num_train_epochs: 10 -max_steps: 5000 +num_train_epochs: 5 +max_steps: 500 warmup_steps: 0 logging_steps: 500 save_steps: 500 @@ -28,7 +28,7 @@ eval_all_checkpoints: False no_cuda: False n_gpu: 0 overwrite_output_dir: True -overwrite_cache: False +overwrite_cache: True seed: 42 fp16: False fp16_opt_level: "01" diff --git a/example/ee/standard/predict.py b/example/ee/standard/predict.py index 02c0769c..af700c71 100644 --- a/example/ee/standard/predict.py +++ b/example/ee/standard/predict.py @@ -46,8 +46,8 @@ def main(args): args.data_dir = os.path.join(args.cwd, "./data/" + args.data_name + "/" + args.task_name) args.tag_path = os.path.join(args.cwd, "./data/" + args.data_name + "/schema") args.model_name_or_path = os.path.join(args.cwd, args.model_name_or_path) - args.dev_trigger_pred_file = os.path.join(args.cwd, args.dev_trigger_pred_file) if args.do_pipeline_predict else None - args.test_trigger_pred_file = os.path.join(args.cwd, args.test_trigger_pred_file) if args.do_pipeline_predict else None + args.dev_trigger_pred_file = os.path.join(args.cwd, args.dev_trigger_pred_file) if args.do_pipeline_predict and args.task_name=="role" else None + args.test_trigger_pred_file = os.path.join(args.cwd, args.test_trigger_pred_file) if args.do_pipeline_predict and args.task_name=="role" else None args.do_predict = True if args.data_name == "ACE" else False # Setup CUDA, GPU & distributed training @@ -75,8 +75,10 @@ def main(args): args.model_type = args.model_type.lower() config_class, model_class, tokenizer_class = MODEL_CLASSES[args.model_type] + config = config_class.from_pretrained(args.model_name_or_path) tokenizer = tokenizer_class.from_pretrained(args.model_name_or_path, do_lower_case=args.do_lower_case) - model = model_class.from_pretrained(args.model_name_or_path) + model = model_class.from_pretrained(args.model_name_or_path, config=config) + logger.info(f"label_nums:{config.num_labels}") model.to(device) pad_token_label_id = -100 @@ -91,7 +93,7 @@ def main(args): raw_path = "/".join(args.data_dir.split("/")[:-1]) if args.do_eval: - if args.dev_trigger_pred_file is not None: + if args.task_name=="role" and args.dev_trigger_pred_file is not None: processor.process_dev_with_pred_trigger(args, raw_path, "dev_with_pred_trigger.tsv") eval_examples = processor.get_examples(os.path.join(args.data_dir, "dev_with_pred_trigger.tsv"), "dev") else: @@ -99,7 +101,7 @@ def main(args): eval_dataset = load_and_cache_examples(args, eval_examples , tokenizer, labels, pad_token_label_id, mode="dev") if args.do_predict: - if args.test_trigger_pred_file is not None: + if args.task_name=="role" and args.test_trigger_pred_file is not None: processor.process_test_with_pred_trigger(args, raw_path, "test_with_pred_trigger.tsv") test_examples = processor.get_examples(os.path.join(args.data_dir, "test_with_pred_trigger.tsv"), "test") else: diff --git a/example/ee/standard/requirements.txt b/example/ee/standard/requirements.txt index 4300818e..3b8db82a 100644 --- a/example/ee/standard/requirements.txt +++ b/example/ee/standard/requirements.txt @@ -1,11 +1,10 @@ torch==1.10.0 transformers==4.26.0 -hydra-core==1.3.1 -tensorboardx==2.4 +tensorboardx==2.5.1 lxml==4.9.1 beautifulsoup4==4.9.3 bs4==0.0.1 stanza==1.2 sentencepiece==0.1.95 -ipdb==0.13.9 -deepke \ No newline at end of file +ipdb==0.13.11 +deepke==2.2.3 \ No newline at end of file