Update README.md
Browse files
README.md
CHANGED
@@ -1,84 +1,86 @@
|
|
1 |
---
|
2 |
-
library_name:
|
3 |
-
pipeline_tag:
|
4 |
tags:
|
5 |
-
-
|
6 |
- feature-extraction
|
7 |
-
-
|
|
|
8 |
- transformers
|
9 |
-
|
10 |
---
|
11 |
|
12 |
# Hierarchy-Transformers/HiT-MiniLM-WordNet
|
13 |
|
14 |
-
|
15 |
-
|
16 |
-
<!--- Describe your model here -->
|
17 |
-
|
18 |
-
## Usage (Sentence-Transformers)
|
19 |
|
20 |
-
|
21 |
-
|
22 |
-
```
|
23 |
-
pip install -U sentence-transformers
|
24 |
-
```
|
25 |
|
26 |
-
|
27 |
|
28 |
-
|
29 |
-
from sentence_transformers import SentenceTransformer
|
30 |
-
sentences = ["This is an example sentence", "Each sentence is converted"]
|
31 |
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
|
|
|
|
|
|
36 |
|
|
|
37 |
|
|
|
38 |
|
39 |
-
|
40 |
-
|
41 |
|
42 |
-
|
43 |
-
from transformers import AutoTokenizer, AutoModel
|
44 |
-
import torch
|
45 |
|
|
|
46 |
|
47 |
-
|
48 |
-
def mean_pooling(model_output, attention_mask):
|
49 |
-
token_embeddings = model_output[0] #First element of model_output contains all token embeddings
|
50 |
-
input_mask_expanded = attention_mask.unsqueeze(-1).expand(token_embeddings.size()).float()
|
51 |
-
return torch.sum(token_embeddings * input_mask_expanded, 1) / torch.clamp(input_mask_expanded.sum(1), min=1e-9)
|
52 |
|
|
|
53 |
|
54 |
-
|
55 |
-
sentences = ['This is an example sentence', 'Each sentence is converted']
|
56 |
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
|
61 |
-
#
|
62 |
-
|
|
|
63 |
|
64 |
-
#
|
65 |
-
|
66 |
-
model_output = model(**encoded_input)
|
67 |
|
68 |
-
#
|
69 |
-
|
70 |
|
71 |
-
|
72 |
-
|
73 |
```
|
74 |
|
|
|
75 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
76 |
|
77 |
-
|
78 |
-
|
79 |
-
<!--- Describe how your model was evaluated -->
|
80 |
-
|
81 |
-
For an automated evaluation of this model, see the *Sentence Embeddings Benchmark*: [https://seb.sbert.net](https://seb.sbert.net?model_name=Hierarchy-Transformers/HiT-MiniLM-WordNet)
|
82 |
|
83 |
|
84 |
|
@@ -90,6 +92,16 @@ HierarchyTransformer(
|
|
90 |
)
|
91 |
```
|
92 |
|
93 |
-
##
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
94 |
|
95 |
-
|
|
|
1 |
---
|
2 |
+
library_name: hierarchy-transformers
|
3 |
+
pipeline_tag: feature-extraction
|
4 |
tags:
|
5 |
+
- hierarchy-transformers
|
6 |
- feature-extraction
|
7 |
+
- hierarchy-encoding
|
8 |
+
- subsumption-relationships
|
9 |
- transformers
|
|
|
10 |
---
|
11 |
|
12 |
# Hierarchy-Transformers/HiT-MiniLM-WordNet
|
13 |
|
14 |
+
A **Hi**erarchy **T**ransformer Encoder (HiT) model that explicitly encodes entities according to their hierarchical relationships.
|
|
|
|
|
|
|
|
|
15 |
|
16 |
+
### Model Description
|
|
|
|
|
|
|
|
|
17 |
|
18 |
+
<!-- Provide a longer summary of what this model is. -->
|
19 |
|
20 |
+
HiT-MiniLM-L12-WordNet is a HiT model trained on WordNet's noun hierarchy with random negative sampling.
|
|
|
|
|
21 |
|
22 |
+
- **Developed by:** Yuan He, Zhangdie Yuan, Jiaoyan Chen, and Ian Horrocks
|
23 |
+
- **Model type:** **Hi**erarchy **T**ransformer Encoder (HiT)
|
24 |
+
- **License:** Apache license 2.0
|
25 |
+
- **Hierarchy**: WordNet (Noun)
|
26 |
+
- **Datasets**: Available at [Zenodo](https://zenodo.org/doi/10.5281/zenodo.10511042)
|
27 |
+
- **Pre-trained model:** [sentence-transformers/all-MiniLM-L12-v2](https://huggingface.co/sentence-transformers/all-MiniLM-L12-v2)
|
28 |
+
- **Training Objectives**: Jointly optimised on *hyperbolic clustering* and *hyperbolic centripetal* losses
|
29 |
|
30 |
+
### Model Sources
|
31 |
|
32 |
+
<!-- Provide the basic links for the model. -->
|
33 |
|
34 |
+
- **Repository:** https://github.com/KRR-Oxford/HierarchyTransformers
|
35 |
+
- **Paper:** [Language Models as Hierarchy Encoders](tbd)
|
36 |
|
37 |
+
## Uses
|
|
|
|
|
38 |
|
39 |
+
<!-- Address questions around how the model is intended to be used, including the foreseeable users of the model and those affected by the model. -->
|
40 |
|
41 |
+
HiT models are used to encode entities (presented as texts) and predict their hierarhical relationships in hyperbolic space.
|
|
|
|
|
|
|
|
|
42 |
|
43 |
+
## Usage (Hierarchy-Transformers)
|
44 |
|
45 |
+
Use the code below to get started with the model.
|
|
|
46 |
|
47 |
+
```python
|
48 |
+
from hierarchy_transformers import HierarchyTransformer
|
49 |
+
from hierarchy_transformers.utils import get_torch_device
|
50 |
|
51 |
+
# set up the device (use cpu if no gpu found)
|
52 |
+
gpu_id = 0
|
53 |
+
device = get_torch_device(gpu_id)
|
54 |
|
55 |
+
# load the model
|
56 |
+
model = HierarchyTransformer.load_pretrained('Hierarchy-Transformers/HiT-MiniLM-L12-WordNet', device)
|
|
|
57 |
|
58 |
+
# entity names to be encoded.
|
59 |
+
entity_names = ["computer", "personal computer", "fruit", "berry"]
|
60 |
|
61 |
+
# get the entity embeddings
|
62 |
+
entity_embeddings = model.encode(entity_names)
|
63 |
```
|
64 |
|
65 |
+
Use the entity embeddings to predict the subsumption relationships between them.
|
66 |
|
67 |
+
```python
|
68 |
+
# suppose we want to compare "personal computer" and "computer", "berry" and "fruit"
|
69 |
+
child_entity_embeddings = model.encode(["personal computer", "berry"])
|
70 |
+
parent_entity_embeddings = model.encode(["computer", "fruit"])
|
71 |
+
|
72 |
+
# compute the hyperbolic distances and norms of entity embeddings
|
73 |
+
dists = model.manifold.dist(child_entity_embeddings, parent_entity_embeddings)
|
74 |
+
child_norms = model.manifold.dist0(child_entity_embeddings)
|
75 |
+
parent_norms = model.manifold.dist0(parent_entity_embeddings)
|
76 |
+
|
77 |
+
# use the empirical function for subsumption prediction proposed in the paper
|
78 |
+
# `centri_score_weight` and the overall threshold are determined on the validation set
|
79 |
+
subsumption_scores = dists + centri_score_weight * (parent_norms - child_norms)
|
80 |
+
```
|
81 |
|
82 |
+
Training and evaluation scripts are available at [GitHub](https://github.com/KRR-Oxford/HierarchyTransformers).
|
83 |
+
Technical details are presented in the [paper](tbd).
|
|
|
|
|
|
|
84 |
|
85 |
|
86 |
|
|
|
92 |
)
|
93 |
```
|
94 |
|
95 |
+
## Citation
|
96 |
+
|
97 |
+
<!-- If there is a paper or blog post introducing the model, the APA and Bibtex information for that should go in this section. -->
|
98 |
+
|
99 |
+
**BibTeX:**
|
100 |
+
|
101 |
+
[More Information Needed]
|
102 |
+
|
103 |
+
|
104 |
+
|
105 |
+
## Model Card Contact
|
106 |
|
107 |
+
For any queries or feedback, please contact Yuan He (yuan.he@cs.ox.ac.uk).
|