KoichiYasuoka
commited on
Commit
Β·
c69ab63
1
Parent(s):
52dc966
initial release
Browse files
maker.py
ADDED
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#! /usr/bin/python3
|
2 |
+
src="KoichiYasuoka/deberta-large-chinese-erlangshen-upos"
|
3 |
+
tgt="KoichiYasuoka/deberta-large-chinese-erlangshen-ud-goeswith"
|
4 |
+
import os
|
5 |
+
for d in ["UD_Chinese-GSD","UD_Chinese-GSDSimp"]:
|
6 |
+
os.system("test -d "+d+" || git clone --depth=1 https://github.com/UniversalDependencies/"+d)
|
7 |
+
os.system("for F in train dev test ; do cat UD_Chinese-*/*-$F.conllu > $F.conllu ; done")
|
8 |
+
class UDgoeswithDataset(object):
|
9 |
+
def __init__(self,conllu,tokenizer):
|
10 |
+
self.ids,self.tags,label=[],[],set()
|
11 |
+
with open(conllu,"r",encoding="utf-8") as r:
|
12 |
+
cls,sep,msk=tokenizer.cls_token_id,tokenizer.sep_token_id,tokenizer.mask_token_id
|
13 |
+
dep,c="-|_|dep",[]
|
14 |
+
for s in r:
|
15 |
+
t=s.split("\t")
|
16 |
+
if len(t)==10 and t[0].isdecimal():
|
17 |
+
c.append(t)
|
18 |
+
elif c!=[]:
|
19 |
+
v=tokenizer([t[1] for t in c],add_special_tokens=False)["input_ids"]
|
20 |
+
for i in range(len(v)-1,-1,-1):
|
21 |
+
for j in range(1,len(v[i])):
|
22 |
+
c.insert(i+1,[c[i][0],"_","_","X","_","_",c[i][0],"goeswith","_","_"])
|
23 |
+
y=["0"]+[t[0] for t in c]
|
24 |
+
h=[i if t[6]=="0" else y.index(t[6]) for i,t in enumerate(c,1)]
|
25 |
+
p,v=[t[3]+"|"+t[5]+"|"+t[7] for t in c],sum(v,[])
|
26 |
+
self.ids.append([cls]+v+[sep])
|
27 |
+
self.tags.append([dep]+p+[dep])
|
28 |
+
label=set(sum([self.tags[-1],list(label)],[]))
|
29 |
+
for i,k in enumerate(v):
|
30 |
+
self.ids.append([cls]+v[0:i]+[msk]+v[i+1:]+[sep,k])
|
31 |
+
self.tags.append([dep]+[t if h[j]==i+1 else dep for j,t in enumerate(p)]+[dep,dep])
|
32 |
+
c=[]
|
33 |
+
self.label2id={l:i for i,l in enumerate(sorted(label))}
|
34 |
+
def __call__(*args):
|
35 |
+
label=set(sum([list(t.label2id) for t in args],[]))
|
36 |
+
lid={l:i for i,l in enumerate(sorted(label))}
|
37 |
+
for t in args:
|
38 |
+
t.label2id=lid
|
39 |
+
return lid
|
40 |
+
__len__=lambda self:len(self.ids)
|
41 |
+
__getitem__=lambda self,i:{"input_ids":self.ids[i],"labels":[self.label2id[t] for t in self.tags[i]]}
|
42 |
+
from transformers import BertTokenizer,AutoConfig,AutoModelForTokenClassification,DataCollatorForTokenClassification,TrainingArguments,Trainer
|
43 |
+
tkz=BertTokenizer.from_pretrained(src,model_max_length=512)
|
44 |
+
trainDS=UDgoeswithDataset("train.conllu",tkz)
|
45 |
+
devDS=UDgoeswithDataset("dev.conllu",tkz)
|
46 |
+
testDS=UDgoeswithDataset("test.conllu",tkz)
|
47 |
+
lid=trainDS(devDS,testDS)
|
48 |
+
cfg=AutoConfig.from_pretrained(src,num_labels=len(lid),label2id=lid,id2label={i:l for l,i in lid.items()},ignore_mismatched_sizes=True)
|
49 |
+
arg=TrainingArguments(num_train_epochs=3,per_device_train_batch_size=8,output_dir="/tmp",overwrite_output_dir=True,save_total_limit=2,evaluation_strategy="epoch",learning_rate=5e-05,warmup_ratio=0.1)
|
50 |
+
trn=Trainer(args=arg,data_collator=DataCollatorForTokenClassification(tkz),model=AutoModelForTokenClassification.from_pretrained(src,config=cfg,ignore_mismatched_sizes=True),train_dataset=trainDS,eval_dataset=devDS)
|
51 |
+
trn.train()
|
52 |
+
trn.save_model(tgt)
|
53 |
+
tkz.save_pretrained(tgt)
|