Update README.md
Browse files
README.md
CHANGED
@@ -1,125 +1,115 @@
|
|
1 |
---
|
2 |
-
|
|
|
|
|
3 |
tags:
|
4 |
-
-
|
5 |
-
-
|
6 |
-
-
|
7 |
-
|
|
|
|
|
|
|
|
|
8 |
---
|
9 |
|
10 |
-
#
|
11 |
|
12 |
-
|
13 |
|
14 |
-
|
15 |
|
16 |
-
|
17 |
|
18 |
-
|
19 |
|
20 |
-
|
21 |
-
pip install -U sentence-transformers
|
22 |
-
```
|
23 |
|
24 |
-
Then you can use the model like this:
|
25 |
|
26 |
-
|
27 |
-
from sentence_transformers import SentenceTransformer
|
28 |
-
sentences = ["This is an example sentence", "Each sentence is converted"]
|
29 |
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
|
35 |
-
|
36 |
-
|
37 |
-
## Usage (HuggingFace Transformers)
|
38 |
-
Without [sentence-transformers](https://www.SBERT.net), you can use the model like this: First, you pass your input through the transformer model, then you have to apply the right pooling-operation on-top of the contextualized word embeddings.
|
39 |
|
40 |
```python
|
41 |
-
from transformers import AutoTokenizer,
|
42 |
-
import torch
|
43 |
-
|
44 |
|
45 |
-
|
46 |
-
def mean_pooling(model_output, attention_mask):
|
47 |
-
token_embeddings = model_output[0] #First element of model_output contains all token embeddings
|
48 |
-
input_mask_expanded = attention_mask.unsqueeze(-1).expand(token_embeddings.size()).float()
|
49 |
-
return torch.sum(token_embeddings * input_mask_expanded, 1) / torch.clamp(input_mask_expanded.sum(1), min=1e-9)
|
50 |
|
51 |
-
|
52 |
-
# Sentences we want sentence embeddings for
|
53 |
-
sentences = ['This is an example sentence', 'Each sentence is converted']
|
54 |
-
|
55 |
-
# Load model from HuggingFace Hub
|
56 |
-
tokenizer = AutoTokenizer.from_pretrained('{MODEL_NAME}')
|
57 |
-
model = AutoModel.from_pretrained('{MODEL_NAME}')
|
58 |
-
|
59 |
-
# Tokenize sentences
|
60 |
-
encoded_input = tokenizer(sentences, padding=True, truncation=True, return_tensors='pt')
|
61 |
-
|
62 |
-
# Compute token embeddings
|
63 |
-
with torch.no_grad():
|
64 |
-
model_output = model(**encoded_input)
|
65 |
-
|
66 |
-
# Perform pooling. In this case, mean pooling.
|
67 |
-
sentence_embeddings = mean_pooling(model_output, encoded_input['attention_mask'])
|
68 |
-
|
69 |
-
print("Sentence embeddings:")
|
70 |
-
print(sentence_embeddings)
|
71 |
```
|
72 |
|
|
|
73 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
74 |
|
75 |
-
|
76 |
-
|
77 |
-
<!--- Describe how your model was evaluated -->
|
78 |
-
|
79 |
-
For an automated evaluation of this model, see the *Sentence Embeddings Benchmark*: [https://seb.sbert.net](https://seb.sbert.net?model_name={MODEL_NAME})
|
80 |
-
|
81 |
-
|
82 |
-
## Training
|
83 |
-
The model was trained with the parameters:
|
84 |
|
85 |
-
|
86 |
|
87 |
-
|
88 |
-
|
89 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
90 |
```
|
91 |
|
92 |
-
|
93 |
|
94 |
-
|
95 |
|
96 |
-
|
97 |
-
|
98 |
-
{
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
"optimizer_params": {
|
105 |
-
"lr": 1e-05
|
106 |
-
},
|
107 |
-
"scheduler": "constantlr",
|
108 |
-
"steps_per_epoch": null,
|
109 |
-
"warmup_steps": 10000,
|
110 |
-
"weight_decay": 0
|
111 |
}
|
112 |
```
|
113 |
-
|
114 |
-
|
115 |
-
## Full Model Architecture
|
116 |
-
```
|
117 |
-
SentenceTransformer(
|
118 |
-
(0): Transformer({'max_seq_length': 512, 'do_lower_case': False}) with Transformer model: BertModel
|
119 |
-
(1): Pooling({'word_embedding_dimension': 1024, 'pooling_mode_cls_token': False, 'pooling_mode_mean_tokens': True, 'pooling_mode_max_tokens': False, 'pooling_mode_mean_sqrt_len_tokens': False})
|
120 |
-
)
|
121 |
-
```
|
122 |
-
|
123 |
-
## Citing & Authors
|
124 |
-
|
125 |
-
<!--- Describe where people can find more information -->
|
|
|
1 |
---
|
2 |
+
language:
|
3 |
+
- pt
|
4 |
+
thumbnail: "Portugues BERT for the Legal Domain"
|
5 |
tags:
|
6 |
+
- bert
|
7 |
+
- pytorch
|
8 |
+
- tsdae
|
9 |
+
datasets:
|
10 |
+
- rufimelo/PortugueseLegalSentences-v1
|
11 |
+
license: "mit"
|
12 |
+
widget:
|
13 |
+
- text: "O advogado apresentou [MASK] ao juíz."
|
14 |
---
|
15 |
|
16 |
+
# Legal_BERTimbau
|
17 |
|
18 |
+
## Introduction
|
19 |
|
20 |
+
Legal_BERTimbau Large is a fine-tuned BERT model based on [BERTimbau](https://huggingface.co/neuralmind/bert-base-portuguese-cased) Large.
|
21 |
|
22 |
+
"BERTimbau Base is a pretrained BERT model for Brazilian Portuguese that achieves state-of-the-art performances on three downstream NLP tasks: Named Entity Recognition, Sentence Textual Similarity and Recognizing Textual Entailment. It is available in two sizes: Base and Large.
|
23 |
|
24 |
+
For further information or requests, please go to [BERTimbau repository](https://github.com/neuralmind-ai/portuguese-bert/)."
|
25 |
|
26 |
+
The performance of Language Models can change drastically when there is a domain shift between training and test data. In order create a Portuguese Language Model adapted to a Legal domain, the original BERTimbau model was submitted to a fine-tuning stage where it was performed 1 "PreTraining" epoch over 200000 cleaned documents (lr: 2e-5, using TSDAE technique)
|
|
|
|
|
27 |
|
|
|
28 |
|
29 |
+
## Available models
|
|
|
|
|
30 |
|
31 |
+
| Model | Arch. | #Layers | #Params |
|
32 |
+
| ---------------------------------------- | ---------- | ------- | ------- |
|
33 |
+
|`rufimelo/Legal-BERTimbau-base` |BERT-Base |12 |110M|
|
34 |
+
| `rufimelo/Legal-BERTimbau-large` | BERT-Large | 24 | 335M |
|
35 |
|
36 |
+
## Usage
|
|
|
|
|
|
|
37 |
|
38 |
```python
|
39 |
+
from transformers import AutoTokenizer, AutoModelForMaskedLM
|
|
|
|
|
40 |
|
41 |
+
tokenizer = AutoTokenizer.from_pretrained("rufimelo/Legal-BERTimbau-large-TSDAE")
|
|
|
|
|
|
|
|
|
42 |
|
43 |
+
model = AutoModelForMaskedLM.from_pretrained("rufimelo/Legal-BERTimbau-large-TSDAE")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
44 |
```
|
45 |
|
46 |
+
### Masked language modeling prediction example
|
47 |
|
48 |
+
```python
|
49 |
+
from transformers import pipeline
|
50 |
+
from transformers import AutoTokenizer, AutoModelForMaskedLM
|
51 |
+
|
52 |
+
tokenizer = AutoTokenizer.from_pretrained("rufimelo/Legal-BERTimbau-large-TSDAE")
|
53 |
+
model = AutoModelForMaskedLM.from_pretrained("rufimelo/Legal-BERTimbau-large-TSDAE")
|
54 |
+
|
55 |
+
pipe = pipeline('fill-mask', model=model, tokenizer=tokenizer)
|
56 |
+
pipe('O advogado apresentou [MASK] para o juíz')
|
57 |
+
# [{'score': 0.5034703612327576,
|
58 |
+
#'token': 8190,
|
59 |
+
#'token_str': 'recurso',
|
60 |
+
#'sequence': 'O advogado apresentou recurso para o juíz'},
|
61 |
+
#{'score': 0.07347951829433441,
|
62 |
+
#'token': 21973,
|
63 |
+
#'token_str': 'petição',
|
64 |
+
#'sequence': 'O advogado apresentou petição para o juíz'},
|
65 |
+
#{'score': 0.05165359005331993,
|
66 |
+
#'token': 4299,
|
67 |
+
#'token_str': 'resposta',
|
68 |
+
#'sequence': 'O advogado apresentou resposta para o juíz'},
|
69 |
+
#{'score': 0.04611917585134506,
|
70 |
+
#'token': 5265,
|
71 |
+
#'token_str': 'exposição',
|
72 |
+
#'sequence': 'O advogado apresentou exposição para o juíz'},
|
73 |
+
#{'score': 0.04068068787455559,
|
74 |
+
#'token': 19737, 'token_str':
|
75 |
+
#'alegações',
|
76 |
+
#'sequence': 'O advogado apresentou alegações para o juíz'}]
|
77 |
|
78 |
+
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
79 |
|
80 |
+
### For BERT embeddings
|
81 |
|
82 |
+
```python
|
83 |
+
import torch
|
84 |
+
from transformers import AutoModel
|
85 |
+
|
86 |
+
model = AutoModel.from_pretrained('rufimelo/Legal-BERTimbau-large-TSDAE')
|
87 |
+
input_ids = tokenizer.encode('O advogado apresentou recurso para o juíz', return_tensors='pt')
|
88 |
+
|
89 |
+
with torch.no_grad():
|
90 |
+
outs = model(input_ids)
|
91 |
+
encoded = outs[0][0, 1:-1]
|
92 |
+
|
93 |
+
#tensor([[ 0.0328, -0.4292, -0.6230, ..., -0.3048, -0.5674, 0.0157],
|
94 |
+
#[-0.3569, 0.3326, 0.7013, ..., -0.7778, 0.2646, 1.1310],
|
95 |
+
#[ 0.3169, 0.4333, 0.2026, ..., 1.0517, -0.1951, 0.7050],
|
96 |
+
#...,
|
97 |
+
#[-0.3648, -0.8137, -0.4764, ..., -0.2725, -0.4879, 0.6264],
|
98 |
+
#[-0.2264, -0.1821, -0.3011, ..., -0.5428, 0.1429, 0.0509],
|
99 |
+
#[-1.4617, 0.6281, -0.0625, ..., -1.2774, -0.4491, 0.3131]])
|
100 |
```
|
101 |
|
102 |
+
## Citation
|
103 |
|
104 |
+
If you use this work, please cite BERTimbau's work:
|
105 |
|
106 |
+
```bibtex
|
107 |
+
@inproceedings{souza2020bertimbau,
|
108 |
+
author = {F{\'a}bio Souza and
|
109 |
+
Rodrigo Nogueira and
|
110 |
+
Roberto Lotufo},
|
111 |
+
title = {{BERT}imbau: pretrained {BERT} models for {B}razilian {P}ortuguese},
|
112 |
+
booktitle = {9th Brazilian Conference on Intelligent Systems, {BRACIS}, Rio Grande do Sul, Brazil, October 20-23 (to appear)},
|
113 |
+
year = {2020}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
114 |
}
|
115 |
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|