File size: 3,003 Bytes
fb5a49f
c31ec02
fb5a49f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c31ec02
fb5a49f
c31ec02
fb5a49f
 
 
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
#! /usr/bin/python3
src="KoichiYasuoka/roberta-base-thai-syllable-upos"
tgt="KoichiYasuoka/roberta-base-thai-syllable-ud-goeswith"
url="https://github.com/KoichiYasuoka/spaCy-Thai"
import os
d=os.path.join(os.path.basename(url),"UD_Thai-Corpora")
os.system("test -d {} || git clone --depth=1 {}".format(d,url))
s='{if(NF>0)u=u$0"\\n";else{f=FILENAME;if(u~/\\t0\\troot\\t/)print u>(f~/-dev/?"dev":f~/-test/?"test":"train")".conllu";u=""}}'
os.system("nawk -F'\\t' '{}' {}/*-ud-*.conllu".format(s,d))
class UDgoeswithDataset(object):
  def __init__(self,conllu,tokenizer):
    self.ids,self.tags,label=[],[],set()
    with open(conllu,"r",encoding="utf-8") as r:
      cls,sep,msk=tokenizer.cls_token_id,tokenizer.sep_token_id,tokenizer.mask_token_id
      dep,c="-|_|dep",[]
      for s in r:
        t=s.split("\t")
        if len(t)==10 and t[0].isdecimal():
          c.append(t)
        elif c!=[]:
          v=tokenizer([t[1] for t in c],add_special_tokens=False)["input_ids"]
          for i in range(len(v)-1,-1,-1):
            for j in range(1,len(v[i])):
              c.insert(i+1,[c[i][0],"_","_","X","_","_",c[i][0],"goeswith","_","_"])
          y=["0"]+[t[0] for t in c]
          h=[i if t[6]=="0" else y.index(t[6]) for i,t in enumerate(c,1)]
          p,v=[t[3]+"|"+t[5]+"|"+t[7] for t in c],sum(v,[])
          self.ids.append([cls]+v+[sep])
          self.tags.append([dep]+p+[dep])
          label=set(sum([self.tags[-1],list(label)],[]))
          for i,k in enumerate(v):
            self.ids.append([cls]+v[0:i]+[msk]+v[i+1:]+[sep,k])
            self.tags.append([dep]+[t if h[j]==i+1 else dep for j,t in enumerate(p)]+[dep,dep])
          c=[]
    self.label2id={l:i for i,l in enumerate(sorted(label))}
  def __call__(*args):
    label=set(sum([list(t.label2id) for t in args],[]))
    lid={l:i for i,l in enumerate(sorted(label))}
    for t in args:
      t.label2id=lid
    return lid
  __len__=lambda self:len(self.ids)
  __getitem__=lambda self,i:{"input_ids":self.ids[i],"labels":[self.label2id[t] for t in self.tags[i]]}
from transformers import AutoTokenizer,AutoConfig,AutoModelForTokenClassification,DataCollatorForTokenClassification,TrainingArguments,Trainer
tkz=AutoTokenizer.from_pretrained(src)
trainDS=UDgoeswithDataset("train.conllu",tkz)
devDS=UDgoeswithDataset("dev.conllu",tkz)
testDS=UDgoeswithDataset("test.conllu",tkz)
lid=trainDS(devDS,testDS)
cfg=AutoConfig.from_pretrained(src,num_labels=len(lid),label2id=lid,id2label={i:l for l,i in lid.items()},ignore_mismatched_sizes=True)
arg=TrainingArguments(num_train_epochs=3,per_device_train_batch_size=32,output_dir="/tmp",overwrite_output_dir=True,save_total_limit=2,evaluation_strategy="epoch",learning_rate=5e-05,warmup_ratio=0.1)
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)
trn.train()
trn.save_model(tgt)
tkz.save_pretrained(tgt)