datnguyen commited on
Commit
fecd6cd
1 Parent(s): 91fb45a

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +85 -1
README.md CHANGED
@@ -9,4 +9,88 @@ metrics:
9
  - spearmanr
10
  pipeline_tag: sentence-similarity
11
  library_name: rage
12
- ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
  - spearmanr
10
  pipeline_tag: sentence-similarity
11
  library_name: rage
12
+ ---
13
+ # Introduce
14
+ ## Installation 🔥
15
+ - We recommend `python 3.9` or higher, `torch 2.0.0` or higher, `transformers 4.31.0` or higher.
16
+
17
+ - Currently, you can only download from the source, however, in the future, we will upload it to PyPI. RagE can be installed from source with the following commands:
18
+ ```
19
+ git clone https://github.com/anti-aii/RagE.git
20
+ cd RagE
21
+ pip install -e .
22
+ ```
23
+ ## Quick start 🥮
24
+ - [1. Initialize the model](#initialize_model)
25
+ - [2. Load model from Huggingface Hub](#download_hf)
26
+ - [3. List of pretrained models](#list_pretrained)
27
+
28
+ We have detailed instructions for using our models for inference. See [notebook](notebook)
29
+ ### 1. Initialize the model
30
+ <a name= 'initialize_model'></a>
31
+ Let's initalize the SentenceEmbedding model
32
+
33
+ ```python
34
+ >>> import torch
35
+ >>> from pyvi import ViTokenizer
36
+ >>> from rage import SentenceEmbedding
37
+ >>> device= torch.device('cuda' if torch.cuda.is_available() else 'cpu')
38
+ >>> model= SentenceEmbedding(model_name= "vinai/phobert-base-v2", torch_dtype= torch.float32, aggregation_hidden_states= False, strategy_pooling= "dense_first")
39
+ >>> model.to(device)
40
+ SentenceEmbeddingConfig(model_base: {'model_type_base': 'RobertaModel', 'model_name': 'vinai/phobert-base-v2', 'type_backbone': 'mlm', 'required_grad_base_model': True, 'aggregation_hidden_states': False, 'concat_embeddings': False, 'dropout': 0.1, 'quantization_config': None}, pooling: {'strategy_pooling': 'dense_first'})
41
+ ```
42
+ Then, we can show the number of parameters in the model.
43
+ ```python
44
+ >>> model.summary_params()
45
+ trainable params: 135588864 || all params: 135588864 || trainable%: 100.0
46
+ >>> model.summary()
47
+ +---------------------------+-------------+------------------+
48
+ | Layer (type) | Params | Trainable params |
49
+ +---------------------------+-------------+------------------+
50
+ | model (RobertaModel) | 134,998,272 | 134998272 |
51
+ | pooling (PoolingStrategy) | 590,592 | 590592 |
52
+ | drp1 (Dropout) | 0 | 0 |
53
+ +---------------------------+-------------+------------------+
54
+ ```
55
+ Now we can use the SentenceEmbedding model to encode the input words. The output of the model will be a matrix in the shape of (batch, dim). Additionally, we can load weights that we have previously trained and saved.
56
+ ``` python
57
+ >>> model.load("best_sup_general_embedding_phobert2.pt", key= False)
58
+ >>> sentences= ["Tôi đang đi học", "Bạn tên là gì?",]
59
+ >>> sentences= list(map(lambda x: ViTokenizer.tokenize(x), sentences))
60
+ >>> model.encode(sentences, batch_size= 1, normalize_embedding= "l2", return_tensors= "np", verbose= 1)
61
+ 2/2 [==============================] - 0s 43ms/Sample
62
+ array([[ 0.00281098, -0.00829096, -0.01582766, ..., 0.00878178,
63
+ 0.01830498, -0.00459659],
64
+ [ 0.00249859, -0.03076724, 0.00033016, ..., 0.01299141,
65
+ -0.00984358, -0.00703243]], dtype=float32)
66
+ ```
67
+ ### 2. Load model from Huggingface Hub
68
+ <a name= 'download_hf'> </a>
69
+
70
+ First, download a pretrained model.
71
+ ```python
72
+ >>> model= SentenceEmbedding.from_pretrained('anti-ai/VieSemantic-base')
73
+ ```
74
+ Then, we encode the input sentences and compare their similarity.
75
+ ```python
76
+ >>> sentences = ["Nó rất thú_vị", "Nó không thú_vị ."]
77
+ >>> output= model.encode(sentences, batch_size= 1, return_tensors= 'pt')
78
+ >>> torch.cosine_similarity(output[0].view(1, -1), output[1].view(1, -1)).cpu().tolist()
79
+ 2/2 [==============================] - 0s 40ms/Sample
80
+ [0.5605039596557617]
81
+ ```
82
+
83
+ ### 3. List of pretrained models
84
+ <a name= 'list_pretrained'></a>
85
+ This list will be updated with our prominent models. Our models will primarily aim to support Vietnamese language.
86
+ Additionally, you can access our datasets and pretrained models by visiting https://huggingface.co/anti-ai.
87
+
88
+ | Model Name | Model Type | #params | checkpoint|
89
+ | - | - | - | - |
90
+ | anti-ai/ViEmbedding-base | SentenceEmbedding | 135.5M |[model](https://huggingface.co/anti-ai/ViEmbedding-base) |
91
+ | anti-ai/BioViEmbedding-base-unsup | SentenceEmbedding | 135.5M |[model](https://huggingface.co/anti-ai/BioViEmbedding-base-unsup) |
92
+ | anti-ai/VieSemantic-base | SentenceEmbedding | 135.5M |[model](https://huggingface.co/anti-ai/VieSemantic-base) |
93
+
94
+
95
+ ## Contacts
96
+ If you have any questions about this repo, please contact me (nduc0231@gmail.com)