-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
222 lines (165 loc) · 7.7 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
import os
import random
import time
import logging
import argparse
from dataclasses import dataclass, field
from typing import Optional,Dict, Union, Any, Tuple, List
import numpy as np
import datasets
import torch
import torch.nn as nn
from torch.utils.data import DataLoader, Dataset
from tqdm import tqdm
import transformers
from transformers import (
AutoModel,
DataCollatorForSeq2Seq,
AutoConfig,
AutoTokenizer,
HfArgumentParser,
TrainingArguments,
Seq2SeqTrainingArguments,
set_seed,
)
from transformers import Trainer, Seq2SeqTrainer
from transformers import TrainingArguments
from transformers import trainer_utils, training_args
from transformers.trainer_pt_utils import nested_detach
from transformers import BertForMaskedLM, BartForConditionalGeneration
from transformers.file_utils import PaddingStrategy
from transformers.modeling_utils import PreTrainedModel
from transformers.tokenization_utils_base import BatchEncoding, PreTrainedTokenizerBase
from transformers.training_args import TrainingArguments
from chinesebert import ChineseBertForMaskedLM, ChineseBertTokenizerFast, ChineseBertConfig
import reader
import model
import runner
from lib import FoolDataCollatorForSeq2Seq
import sys
class Logger(object):
def __init__(self, filename="Default.log"):
self.terminal = sys.stdout
self.log = open(filename, "w+")
def write(self, message):
self.terminal.write(message)
self.log.write(message)
def flush(self):
pass
def main():
# Args
topk = 1
# Model
name_list = [
""
"hfl/chinese-bert-wwm-ext", \
"hfl/chinese-roberta-wwm-ext", \
#"hfl/chinese-macbert-base", \
#"hfl/chinese-xlnet-base", \
"junnyu/ChineseBERT-base", \
#"hfl/chinese-electra-180g-base-discriminator", \
#"/remote-home/xtzhang/CTC/CTC2021/SpecialEdition/tmp/sighan_ReaLiSe/Dot_datasetsighan_ReaLiSe_eval15_epoch10_bs128", \
#"/remote-home/xtzhang/CTC/CTC2021/SpecialEdition/tmp/sighan_ReaLiSe/MaskedLM_datasetsighan_ReaLiSe_eval15_epoch10_bs128/checkpoint-22210", \
#"junnyu/ChineseBERT-base" \
]
name_list_2 = [
"/remote-home/xtzhang/CTC/CTC2021/SpecialEdition/tmp/sighan_raw/ConfusionCluster/3/bert", \
"/remote-home/xtzhang/CTC/CTC2021/SpecialEdition/tmp/sighan_raw/ConfusionCluster/3/roberta", \
#"/remote-home/xtzhang/CTC/CTC2021/SpecialEdition/tmp/sighan_raw/ConfusionCluster/macbert", \
#"/remote-home/xtzhang/CTC/CTC2021/SpecialEdition/tmp/sighan_raw/ConfusionCluster/xlnet", \
"/remote-home/xtzhang/CTC/CTC2021/SpecialEdition/tmp/sighan_raw/ConfusionCluster/3/chinesebert", \
#"/remote-home/xtzhang/CTC/CTC2021/SpecialEdition/tmp/sighan_raw/ConfusionCluster/electra", \
"/remote-home/xtzhang/CTC/CTC2021/SpecialEdition/tmp/sighan_raw/ConfusionCluster/2/bert", \
"/remote-home/xtzhang/CTC/CTC2021/SpecialEdition/tmp/sighan_raw/ConfusionCluster/2/roberta", \
#"/remote-home/xtzhang/CTC/CTC2021/SpecialEdition/tmp/sighan_raw/ConfusionCluster/macbert", \
#"/remote-home/xtzhang/CTC/CTC2021/SpecialEdition/tmp/sighan_raw/ConfusionCluster/xlnet", \
"/remote-home/xtzhang/CTC/CTC2021/SpecialEdition/tmp/sighan_raw/ConfusionCluster/2/chinesebert", \
"/remote-home/xtzhang/CTC/CTC2021/SpecialEdition/tmp/sighan_raw/ConfusionCluster/bert", \
"/remote-home/xtzhang/CTC/CTC2021/SpecialEdition/tmp/sighan_raw/ConfusionCluster/roberta", \
#"/remote-home/xtzhang/CTC/CTC2021/SpecialEdition/tmp/sighan_raw/ConfusionCluster/macbert", \
#"/remote-home/xtzhang/CTC/CTC2021/SpecialEdition/tmp/sighan_raw/ConfusionCluster/xlnet", \
"/remote-home/xtzhang/CTC/CTC2021/SpecialEdition/tmp/sighan_raw/ConfusionCluster/chinesebert", \
]
name_list_3 = [
"ReaLiSe",
"ReaLiSe_holy",
"PLOME_holy",
]
name_list_4 = [
"/remote-home/xtzhang/CTC/CTC2021/SpecialEdition/tmp/sighan_holy/ConfusionCluster/2/bert", \
"/remote-home/xtzhang/CTC/CTC2021/SpecialEdition/tmp/sighan_holy/ConfusionCluster/2/roberta", \
#"/remote-home/xtzhang/CTC/CTC2021/SpecialEdition/tmp/sighan_holy/ConfusionCluster/2/nezha", \
"/remote-home/xtzhang/CTC/CTC2021/SpecialEdition/tmp/sighan_holy/ConfusionCluster/2/chinesebert", \
]
name_list_5 = [
"/remote-home/xtzhang/CTC/CTC2021/SpecialEdition/tmp/sighan_mask/ConfusionCluster/3/bert", \
"/remote-home/xtzhang/CTC/CTC2021/SpecialEdition/tmp/sighan_mask/ConfusionCluster/3/roberta", \
"/remote-home/xtzhang/CTC/CTC2021/SpecialEdition/tmp/sighan_mask/ConfusionCluster/3/chinesebert", \
]
name_list_6 = [
"/remote-home/xtzhang/CTC/CTC2021/SpecialEdition/tmp/sighan_holy_mask/ConfusionCluster/3/bert",
]
name_list_6 = [
"/remote-home/xtzhang/CTC/CTC2021/SpecialEdition/tmp/sighan_ReaLiSe/Proto/macbert/Proto_cls_copy0_cl0_repeat0_eval15_epoch20_bs48_seed32_multi_taskFalse_weight0_v2",
"/remote-home/xtzhang/CTC/CTC2021/SpecialEdition/tmp/sighan_ReaLiSe/Proto/macbert/Proto_cls_copy0_cl0_repeat0_eval15_epoch20_bs48_seed91_multi_taskFalse_weight0_v2",
"/remote-home/xtzhang/CTC/CTC2021/SpecialEdition/tmp/sighan_ReaLiSe/Proto/macbert/Proto_cls_copy0_cl0.0025_repeat1_eval15_epoch20_bs48_seed3471_multi_taskFalse_weight0.005_v1",
"/remote-home/xtzhang/CTC/CTC2021/SpecialEdition/tmp/sighan_ReaLiSe/Proto/macbert/Proto_cls_copy0_cl0.00375_repeat1_eval15_epoch20_bs48_seed3471_multi_taskFalse_weight0.005_v2",
]
name_list_cl = [
"/remote-home/xtzhang/CTC/CTC2021/SpecialEdition/tmp/sighan_holy/Proto/roberta",
]
name_list_cl_abla = [
"/remote-home/xtzhang/CTC/CTC2021/SpecialEdition/tmp/sighan_ReaLiSe/Proto/macbert/Proto_cls_copy0_cl0_repeat0_eval15_epoch20_bs48_seed91_multi_taskFalse_weight0_v2"
]
#name = name_list[0]
#name = name_list_2[2]
#name = name_list_3[-1]
#name = name_list_4[2]
#name = name_list_5[2]
name = name_list_6[1]
#name = name_list_cl_abla[0]
#set the name to your self pretrained model to test here
# name = "your pretrained model dir"
output_path = "./logs/"+ name.replace("/", "_") + "_topk_" + str(topk) +".txt"
print("output_path:", output_path)
sys.stdout = Logger(output_path)
print("Model:", name)
if name in name_list_3:
model = name # we hack
elif 'chinesebert' in name or "ChineseBert" in name:
config = ChineseBertConfig.from_pretrained(name)
model = ChineseBertForMaskedLM.from_pretrained(name, config=config)
elif "Proto" in name:
from models.modeling_bert_v4 import ProtoModel_v3 as ProtoModel
import torch
model = ProtoModel("hfl/chinese-macbert-base", None)
model.load_state_dict(torch.load(name+"/pytorch_model.bin"))
print("ProtoType !")
else:
model = BertForMaskedLM.from_pretrained(name)
#model = BartForConditionalGeneration.from_pretrained(name)
#model = AutoModel.from_pretrained(name)
sighan_reader = reader.SighanReader(name)
confusion_reader = reader.ConfusionSetReader()
# Data Collator
data_collator = FoolDataCollatorForSeq2Seq()#my data collator fix the length for bert.
# Runner
Runner = runner.ConfusionClusterRunner(
model=model,
args=None,#training_args,
reader=sighan_reader,
confusion_reader=confusion_reader,
data_collator=data_collator,
topk=topk,
)
# Run
run_result = Runner.run()
# Train
#train_result = runner.train()
# Evaluate
#evaluate_result = runner.evaluate()
# Predict
#predict_result = runner.predict()
return
if __name__ == "__main__":
main()