KoichiYasuoka commited on
Commit
a2eb47f
1 Parent(s): 563a060

root analysis improved

Browse files
Files changed (1) hide show
  1. README.md +7 -7
README.md CHANGED
@@ -30,16 +30,16 @@ class UDgoeswith(object):
30
  import numpy,torch,ufal.chu_liu_edmonds
31
  w=self.tokenizer(text,return_offsets_mapping=True)
32
  v=w["input_ids"]
33
- n=len(v)-1
34
  with torch.no_grad():
35
- 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)]))
36
- e=d.logits.numpy()[:,1:n,:]
37
- e[:,:,0]=numpy.nan
38
- m=numpy.full((n,n),numpy.nan)
39
  m[1:,1:]=numpy.nanmax(e,axis=2).transpose()
40
- p=numpy.zeros((n,n))
41
  p[1:,1:]=numpy.nanargmax(e,axis=2).transpose()
42
- for i in range(1,n):
43
  m[i,0],m[i,i],p[i,0]=m[i,i],numpy.nan,p[i,i]
44
  h=ufal.chu_liu_edmonds.chu_liu_edmonds(m)[0]
45
  u="# text = "+text+"\n"
 
30
  import numpy,torch,ufal.chu_liu_edmonds
31
  w=self.tokenizer(text,return_offsets_mapping=True)
32
  v=w["input_ids"]
33
+ x=[v[0:i]+[self.tokenizer.mask_token_id]+v[i+1:]+[j] for i,j in enumerate(v[1:-1],1)]
34
  with torch.no_grad():
35
+ e=self.model(input_ids=torch.tensor(x)).logits.numpy()[:,1:-2,:]
36
+ r=[1 if i==0 else -1 if j.endswith("|root") else 0 for i,j in sorted(self.model.config.id2label.items())]
37
+ e+=numpy.where(numpy.add.outer(numpy.identity(e.shape[0]),r)==0,0,numpy.nan)
38
+ m=numpy.full((e.shape[0]+1,e.shape[1]+1),numpy.nan)
39
  m[1:,1:]=numpy.nanmax(e,axis=2).transpose()
40
+ p=numpy.zeros(m.shape)
41
  p[1:,1:]=numpy.nanargmax(e,axis=2).transpose()
42
+ for i in range(1,m.shape[0]):
43
  m[i,0],m[i,i],p[i,0]=m[i,i],numpy.nan,p[i,i]
44
  h=ufal.chu_liu_edmonds.chu_liu_edmonds(m)[0]
45
  u="# text = "+text+"\n"