Update README.md
Browse files
README.md
CHANGED
@@ -67,5 +67,34 @@ Entity Recognition Results:
|
|
67 |
- 各次元のlogit値は,入力文章における各entityの関連度を表現しています.
|
68 |
|
69 |
### 4. entity_logits(entityの埋め込み表現)
|
70 |
-
|
71 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
67 |
- 各次元のlogit値は,入力文章における各entityの関連度を表現しています.
|
68 |
|
69 |
### 4. entity_logits(entityの埋め込み表現)
|
70 |
+
- entityの一覧は,tokenizerがentity_vocabに辞書形式で持つ.
|
71 |
+
```
|
72 |
+
tokenizer.entity_vocab # => {"": 0, ... ,"AGC": 48, ....
|
73 |
+
tokenizer.entity_vocab["味の素"] # => 8469(味の素のentity_id)
|
74 |
+
```
|
75 |
+
- entity_spans及びentitties引数をtokenizerに渡し,tokenをencodeすることで,entityの埋め込み表現を得る.
|
76 |
+
```
|
77 |
+
model.eval()
|
78 |
+
tokens = tokenizer("味の素", entities=["味の素"], entity_spans=[(0, 3)], truncation=True, max_length=512, return_tensors="pt")
|
79 |
+
print(tokens["entity_ids"]) # => tensor([[8469]])
|
80 |
+
with torch.no_grad():
|
81 |
+
outputs = model(**tokens)
|
82 |
+
outputs.entity_logits.shape # 味の素のentity_vector
|
83 |
+
```
|
84 |
+
- entityの埋め込み表現の内積(やコサイン類似度)を計算することで,entity同士の類似度を計算可能
|
85 |
+
```
|
86 |
+
def encode(entity_text):
|
87 |
+
model.eval()
|
88 |
+
tokens = tokenizer(entity_text, entities=[entity_text], entity_spans=[(0, len(entity_text))],
|
89 |
+
truncation=True, max_length=512, return_tensors="pt")
|
90 |
+
with torch.no_grad():
|
91 |
+
outputs = model(**tokens)
|
92 |
+
return outputs.entity_logits[0][0]
|
93 |
+
azinomoto = encode("味の素")
|
94 |
+
nisshin = encode("日清食品ホールディングス")
|
95 |
+
kameda = encode("亀田製菓")
|
96 |
+
sony = encode("ソニーホールディングス")
|
97 |
+
print(azinomoto @ nisshin) # => tensor(24834.6836)
|
98 |
+
print(azinomoto @ kameda) # => tensor(17547.6895)
|
99 |
+
print(azinomoto @ sony) # => tensor(8699.2871)
|
100 |
+
```
|