KoichiYasuoka commited on
Commit
c531b0c
1 Parent(s): 7241ece

initial release

Browse files
Files changed (8) hide show
  1. README.md +58 -0
  2. config.json +211 -0
  3. maker.py +54 -0
  4. pytorch_model.bin +3 -0
  5. special_tokens_map.json +9 -0
  6. spm.model +3 -0
  7. tokenizer.json +0 -0
  8. tokenizer_config.json +14 -0
README.md ADDED
@@ -0,0 +1,58 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ language:
3
+ - "ja"
4
+ tags:
5
+ - "japanese"
6
+ - "wikipedia"
7
+ - "pos"
8
+ - "dependency-parsing"
9
+ datasets:
10
+ - "universal_dependencies"
11
+ license: "cc-by-sa-4.0"
12
+ pipeline_tag: "token-classification"
13
+ widget:
14
+ - text: "全学年にわたって小学校の国語の教科書に挿し絵が用いられている"
15
+ ---
16
+
17
+ # deberta-large-japanese-wikipedia-ud-goeswith
18
+
19
+ ## Model Description
20
+
21
+ This is a DeBERTa(V2) model pretrained on Japanese Wikipedia and 青空文庫 texts for POS-tagging and dependency-parsing (using `goeswith` for subwords), derived from [deberta-large-japanese-wikipedia](https://huggingface.co/KoichiYasuoka/deberta-large-japanese-wikipedia) and [UD_Japanese-GSDLUW](https://github.com/UniversalDependencies/UD_Japanese-GSDLUW).
22
+
23
+ ## How to Use
24
+
25
+ ```py
26
+ class UDgoeswith(object):
27
+ def __init__(self,bert):
28
+ from transformers import AutoTokenizer,AutoModelForTokenClassification
29
+ self.tokenizer=AutoTokenizer.from_pretrained(bert)
30
+ self.model=AutoModelForTokenClassification.from_pretrained(bert)
31
+ def __call__(self,text):
32
+ import numpy,torch,ufal.chu_liu_edmonds
33
+ w=self.tokenizer(text,return_offsets_mapping=True)
34
+ v=w["input_ids"]
35
+ n=len(v)-1
36
+ with torch.no_grad():
37
+ d=self.model(input_ids=torch.tensor([v[0:i]+[self.tokenizer.mask_token_id]+v[i+1:]+[v[i]] for i in range(1,n)]))
38
+ e=d.logits.numpy()[:,1:n,:]
39
+ e[:,:,0]=numpy.nan
40
+ m=numpy.full((n,n),numpy.nan)
41
+ m[1:,1:]=numpy.nanmax(e,axis=2).transpose()
42
+ p=numpy.zeros((n,n))
43
+ p[1:,1:]=numpy.nanargmax(e,axis=2).transpose()
44
+ for i in range(1,n):
45
+ m[i,0],m[i,i],p[i,0]=m[i,i],numpy.nan,p[i,i]
46
+ h=ufal.chu_liu_edmonds.chu_liu_edmonds(m)[0]
47
+ u="# text = "+text+"\n"
48
+ v=[(s,e) for s,e in w["offset_mapping"] if s<e]
49
+ for i,(s,e) in enumerate(v,1):
50
+ q=self.model.config.id2label[p[i,h[i]]].split("|")
51
+ u+="\t".join([str(i),text[s:e],"_",q[0],"_","|".join(q[1:-1]),str(h[i]),q[-1],"_","_" if i<len(v) and e<v[i][0] else "SpaceAfter=No"])+"\n"
52
+ return u+"\n"
53
+
54
+ nlp=UDgoeswith("KoichiYasuoka/deberta-large-japanese-wikipedia-ud-goeswith")
55
+ print(nlp("全学年にわたって小学校の国語の教科書に挿し絵が用いられている"))
56
+ ```
57
+
58
+ [ufal.chu-liu-edmonds](https://pypi.org/project/ufal.chu-liu-edmonds/) is required.
config.json ADDED
@@ -0,0 +1,211 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "architectures": [
3
+ "DebertaV2ForTokenClassification"
4
+ ],
5
+ "attention_probs_dropout_prob": 0.1,
6
+ "bos_token_id": 0,
7
+ "eos_token_id": 2,
8
+ "hidden_act": "gelu",
9
+ "hidden_dropout_prob": 0.1,
10
+ "hidden_size": 1024,
11
+ "id2label": {
12
+ "0": "-|_|dep",
13
+ "1": "ADJ|_|acl",
14
+ "2": "ADJ|_|advcl",
15
+ "3": "ADJ|_|amod",
16
+ "4": "ADJ|_|ccomp",
17
+ "5": "ADJ|_|csubj",
18
+ "6": "ADJ|_|dep",
19
+ "7": "ADJ|_|dislocated",
20
+ "8": "ADJ|_|nmod",
21
+ "9": "ADJ|_|nsubj",
22
+ "10": "ADJ|_|obj",
23
+ "11": "ADJ|_|obl",
24
+ "12": "ADJ|_|root",
25
+ "13": "ADP|_|case",
26
+ "14": "ADP|_|fixed",
27
+ "15": "ADV|_|advcl",
28
+ "16": "ADV|_|advmod",
29
+ "17": "ADV|_|dep",
30
+ "18": "ADV|_|obj",
31
+ "19": "ADV|_|root",
32
+ "20": "AUX|Polarity=Neg|aux",
33
+ "21": "AUX|_|aux",
34
+ "22": "AUX|_|cop",
35
+ "23": "AUX|_|fixed",
36
+ "24": "AUX|_|root",
37
+ "25": "CCONJ|_|cc",
38
+ "26": "DET|_|det",
39
+ "27": "INTJ|_|discourse",
40
+ "28": "INTJ|_|root",
41
+ "29": "NOUN|Polarity=Neg|obl",
42
+ "30": "NOUN|Polarity=Neg|root",
43
+ "31": "NOUN|_|acl",
44
+ "32": "NOUN|_|advcl",
45
+ "33": "NOUN|_|ccomp",
46
+ "34": "NOUN|_|compound",
47
+ "35": "NOUN|_|csubj",
48
+ "36": "NOUN|_|dislocated",
49
+ "37": "NOUN|_|nmod",
50
+ "38": "NOUN|_|nsubj",
51
+ "39": "NOUN|_|obj",
52
+ "40": "NOUN|_|obl",
53
+ "41": "NOUN|_|root",
54
+ "42": "NUM|_|advcl",
55
+ "43": "NUM|_|compound",
56
+ "44": "NUM|_|dislocated",
57
+ "45": "NUM|_|nmod",
58
+ "46": "NUM|_|nsubj",
59
+ "47": "NUM|_|nummod",
60
+ "48": "NUM|_|obj",
61
+ "49": "NUM|_|obl",
62
+ "50": "NUM|_|root",
63
+ "51": "PART|_|mark",
64
+ "52": "PRON|_|acl",
65
+ "53": "PRON|_|advcl",
66
+ "54": "PRON|_|dislocated",
67
+ "55": "PRON|_|nmod",
68
+ "56": "PRON|_|nsubj",
69
+ "57": "PRON|_|obj",
70
+ "58": "PRON|_|obl",
71
+ "59": "PRON|_|root",
72
+ "60": "PROPN|_|acl",
73
+ "61": "PROPN|_|advcl",
74
+ "62": "PROPN|_|compound",
75
+ "63": "PROPN|_|dislocated",
76
+ "64": "PROPN|_|nmod",
77
+ "65": "PROPN|_|nsubj",
78
+ "66": "PROPN|_|obj",
79
+ "67": "PROPN|_|obl",
80
+ "68": "PROPN|_|root",
81
+ "69": "PUNCT|_|punct",
82
+ "70": "SCONJ|_|mark",
83
+ "71": "SYM|_|compound",
84
+ "72": "SYM|_|dep",
85
+ "73": "SYM|_|nmod",
86
+ "74": "SYM|_|obl",
87
+ "75": "VERB|_|acl",
88
+ "76": "VERB|_|advcl",
89
+ "77": "VERB|_|ccomp",
90
+ "78": "VERB|_|compound",
91
+ "79": "VERB|_|csubj",
92
+ "80": "VERB|_|dislocated",
93
+ "81": "VERB|_|nmod",
94
+ "82": "VERB|_|obj",
95
+ "83": "VERB|_|obl",
96
+ "84": "VERB|_|root",
97
+ "85": "X|_|dep",
98
+ "86": "X|_|goeswith",
99
+ "87": "X|_|nmod"
100
+ },
101
+ "initializer_range": 0.02,
102
+ "intermediate_size": 4096,
103
+ "label2id": {
104
+ "-|_|dep": 0,
105
+ "ADJ|_|acl": 1,
106
+ "ADJ|_|advcl": 2,
107
+ "ADJ|_|amod": 3,
108
+ "ADJ|_|ccomp": 4,
109
+ "ADJ|_|csubj": 5,
110
+ "ADJ|_|dep": 6,
111
+ "ADJ|_|dislocated": 7,
112
+ "ADJ|_|nmod": 8,
113
+ "ADJ|_|nsubj": 9,
114
+ "ADJ|_|obj": 10,
115
+ "ADJ|_|obl": 11,
116
+ "ADJ|_|root": 12,
117
+ "ADP|_|case": 13,
118
+ "ADP|_|fixed": 14,
119
+ "ADV|_|advcl": 15,
120
+ "ADV|_|advmod": 16,
121
+ "ADV|_|dep": 17,
122
+ "ADV|_|obj": 18,
123
+ "ADV|_|root": 19,
124
+ "AUX|Polarity=Neg|aux": 20,
125
+ "AUX|_|aux": 21,
126
+ "AUX|_|cop": 22,
127
+ "AUX|_|fixed": 23,
128
+ "AUX|_|root": 24,
129
+ "CCONJ|_|cc": 25,
130
+ "DET|_|det": 26,
131
+ "INTJ|_|discourse": 27,
132
+ "INTJ|_|root": 28,
133
+ "NOUN|Polarity=Neg|obl": 29,
134
+ "NOUN|Polarity=Neg|root": 30,
135
+ "NOUN|_|acl": 31,
136
+ "NOUN|_|advcl": 32,
137
+ "NOUN|_|ccomp": 33,
138
+ "NOUN|_|compound": 34,
139
+ "NOUN|_|csubj": 35,
140
+ "NOUN|_|dislocated": 36,
141
+ "NOUN|_|nmod": 37,
142
+ "NOUN|_|nsubj": 38,
143
+ "NOUN|_|obj": 39,
144
+ "NOUN|_|obl": 40,
145
+ "NOUN|_|root": 41,
146
+ "NUM|_|advcl": 42,
147
+ "NUM|_|compound": 43,
148
+ "NUM|_|dislocated": 44,
149
+ "NUM|_|nmod": 45,
150
+ "NUM|_|nsubj": 46,
151
+ "NUM|_|nummod": 47,
152
+ "NUM|_|obj": 48,
153
+ "NUM|_|obl": 49,
154
+ "NUM|_|root": 50,
155
+ "PART|_|mark": 51,
156
+ "PRON|_|acl": 52,
157
+ "PRON|_|advcl": 53,
158
+ "PRON|_|dislocated": 54,
159
+ "PRON|_|nmod": 55,
160
+ "PRON|_|nsubj": 56,
161
+ "PRON|_|obj": 57,
162
+ "PRON|_|obl": 58,
163
+ "PRON|_|root": 59,
164
+ "PROPN|_|acl": 60,
165
+ "PROPN|_|advcl": 61,
166
+ "PROPN|_|compound": 62,
167
+ "PROPN|_|dislocated": 63,
168
+ "PROPN|_|nmod": 64,
169
+ "PROPN|_|nsubj": 65,
170
+ "PROPN|_|obj": 66,
171
+ "PROPN|_|obl": 67,
172
+ "PROPN|_|root": 68,
173
+ "PUNCT|_|punct": 69,
174
+ "SCONJ|_|mark": 70,
175
+ "SYM|_|compound": 71,
176
+ "SYM|_|dep": 72,
177
+ "SYM|_|nmod": 73,
178
+ "SYM|_|obl": 74,
179
+ "VERB|_|acl": 75,
180
+ "VERB|_|advcl": 76,
181
+ "VERB|_|ccomp": 77,
182
+ "VERB|_|compound": 78,
183
+ "VERB|_|csubj": 79,
184
+ "VERB|_|dislocated": 80,
185
+ "VERB|_|nmod": 81,
186
+ "VERB|_|obj": 82,
187
+ "VERB|_|obl": 83,
188
+ "VERB|_|root": 84,
189
+ "X|_|dep": 85,
190
+ "X|_|goeswith": 86,
191
+ "X|_|nmod": 87
192
+ },
193
+ "layer_norm_eps": 1e-07,
194
+ "max_position_embeddings": 512,
195
+ "max_relative_positions": -1,
196
+ "model_type": "deberta-v2",
197
+ "num_attention_heads": 16,
198
+ "num_hidden_layers": 24,
199
+ "pad_token_id": 1,
200
+ "pooler_dropout": 0,
201
+ "pooler_hidden_act": "gelu",
202
+ "pooler_hidden_size": 1024,
203
+ "pos_att_type": null,
204
+ "position_biased_input": true,
205
+ "relative_attention": false,
206
+ "tokenizer_class": "DebertaV2TokenizerFast",
207
+ "torch_dtype": "float32",
208
+ "transformers_version": "4.22.0",
209
+ "type_vocab_size": 0,
210
+ "vocab_size": 32000
211
+ }
maker.py ADDED
@@ -0,0 +1,54 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #! /usr/bin/python3
2
+ src="KoichiYasuoka/deberta-large-japanese-wikipedia"
3
+ tgt="KoichiYasuoka/deberta-large-japanese-wikipedia-ud-goeswith"
4
+ url="https://github.com/UniversalDependencies/UD_Japanese-GSDLUW"
5
+ import os
6
+ d=os.path.basename(url)
7
+ os.system("test -d "+d+" || git clone --depth=1 "+url)
8
+ os.system("for F in train dev test ; do cp "+d+"/*-$F.conllu $F.conllu ; done")
9
+ class UDgoeswithDataset(object):
10
+ def __init__(self,conllu,tokenizer):
11
+ self.ids,self.tags,label=[],[],set()
12
+ with open(conllu,"r",encoding="utf-8") as r:
13
+ cls,sep,msk=tokenizer.cls_token_id,tokenizer.sep_token_id,tokenizer.mask_token_id
14
+ dep,c="-|_|dep",[]
15
+ for s in r:
16
+ t=s.split("\t")
17
+ if len(t)==10 and t[0].isdecimal():
18
+ c.append(t)
19
+ elif c!=[]:
20
+ v=tokenizer([t[1] for t in c],add_special_tokens=False)["input_ids"]
21
+ for i in range(len(v)-1,-1,-1):
22
+ for j in range(1,len(v[i])):
23
+ c.insert(i+1,[c[i][0],"_","_","X","_","_",c[i][0],"goeswith","_","_"])
24
+ y=["0"]+[t[0] for t in c]
25
+ h=[i if t[6]=="0" else y.index(t[6]) for i,t in enumerate(c,1)]
26
+ p,v=[t[3]+"|"+t[5]+"|"+t[7] for t in c],sum(v,[])
27
+ self.ids.append([cls]+v+[sep])
28
+ self.tags.append([dep]+p+[dep])
29
+ label=set(sum([self.tags[-1],list(label)],[]))
30
+ for i,k in enumerate(v):
31
+ self.ids.append([cls]+v[0:i]+[msk]+v[i+1:]+[sep,k])
32
+ self.tags.append([dep]+[t if h[j]==i+1 else dep for j,t in enumerate(p)]+[dep,dep])
33
+ c=[]
34
+ self.label2id={l:i for i,l in enumerate(sorted(label))}
35
+ def __call__(*args):
36
+ label=set(sum([list(t.label2id) for t in args],[]))
37
+ lid={l:i for i,l in enumerate(sorted(label))}
38
+ for t in args:
39
+ t.label2id=lid
40
+ return lid
41
+ __len__=lambda self:len(self.ids)
42
+ __getitem__=lambda self,i:{"input_ids":self.ids[i],"labels":[self.label2id[t] for t in self.tags[i]]}
43
+ from transformers import AutoTokenizer,AutoConfig,AutoModelForTokenClassification,DataCollatorForTokenClassification,TrainingArguments,Trainer
44
+ tkz=AutoTokenizer.from_pretrained(src)
45
+ trainDS=UDgoeswithDataset("train.conllu",tkz)
46
+ devDS=UDgoeswithDataset("dev.conllu",tkz)
47
+ testDS=UDgoeswithDataset("test.conllu",tkz)
48
+ lid=trainDS(devDS,testDS)
49
+ cfg=AutoConfig.from_pretrained(src,num_labels=len(lid),label2id=lid,id2label={i:l for l,i in lid.items()})
50
+ 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)
51
+ trn=Trainer(args=arg,data_collator=DataCollatorForTokenClassification(tkz),model=AutoModelForTokenClassification.from_pretrained(src,config=cfg),train_dataset=trainDS,eval_dataset=devDS)
52
+ trn.train()
53
+ trn.save_model(tgt)
54
+ tkz.save_pretrained(tgt)
pytorch_model.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:e84633943bc3d30811d4aa0c0248417b8423927962e125c7dd08930a3a69a37e
3
+ size 1342912499
special_tokens_map.json ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "bos_token": "[CLS]",
3
+ "cls_token": "[CLS]",
4
+ "eos_token": "[SEP]",
5
+ "mask_token": "[MASK]",
6
+ "pad_token": "[PAD]",
7
+ "sep_token": "[SEP]",
8
+ "unk_token": "[UNK]"
9
+ }
spm.model ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:01ba4719c80b6fe911b091a7c05124b64eeece964e09c058ef8f9805daca546b
3
+ size 1
tokenizer.json ADDED
The diff for this file is too large to render. See raw diff
 
tokenizer_config.json ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "bos_token": "[CLS]",
3
+ "cls_token": "[CLS]",
4
+ "do_lower_case": false,
5
+ "eos_token": "[SEP]",
6
+ "keep_accents": true,
7
+ "mask_token": "[MASK]",
8
+ "model_max_length": 512,
9
+ "pad_token": "[PAD]",
10
+ "sep_token": "[SEP]",
11
+ "split_by_punct": true,
12
+ "tokenizer_class": "DebertaV2TokenizerFast",
13
+ "unk_token": "[UNK]"
14
+ }