Update README.md
Browse files
README.md
CHANGED
@@ -1,3 +1,112 @@
|
|
1 |
---
|
2 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
---
|
2 |
+
language:
|
3 |
+
- pt
|
4 |
+
thumbnail: "Portugues BERT for the Legal Domain"
|
5 |
+
tags:
|
6 |
+
- bert
|
7 |
+
- pytorch
|
8 |
+
license: "mit"
|
9 |
+
widget:
|
10 |
+
- text: "O advogado apresentou [MASK] ao juíz."
|
11 |
---
|
12 |
+
|
13 |
+
# Legal_BERTimbau
|
14 |
+
|
15 |
+
## Introduction
|
16 |
+
|
17 |
+
Legal_BERTimbau Large is a fine-tuned BERT model based on [BERTimbau](https://huggingface.co/neuralmind/bert-base-portuguese-cased) Large.
|
18 |
+
|
19 |
+
"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.
|
20 |
+
|
21 |
+
For further information or requests, please go to [BERTimbau repository](https://github.com/neuralmind-ai/portuguese-bert/)."
|
22 |
+
|
23 |
+
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 30 000 legal Portuguese Legal documents available online.
|
24 |
+
|
25 |
+
|
26 |
+
## Available models
|
27 |
+
|
28 |
+
| Model | Arch. | #Layers | #Params |
|
29 |
+
| ---------------------------------------- | ---------- | ------- | ------- |
|
30 |
+
| `rufimelo/Legal-BERTimbau-base` | BERT-Base |12 |110M|
|
31 |
+
| `rufimelo/Legal-BERTimbau-large` | BERT-Large | 24 | 335M |
|
32 |
+
|
33 |
+
## Usage
|
34 |
+
|
35 |
+
```python
|
36 |
+
from transformers import AutoTokenizer, AutoModelForMaskedLM
|
37 |
+
|
38 |
+
tokenizer = AutoTokenizer.from_pretrained("rufimelo/Legal-BERTimbau-base")
|
39 |
+
|
40 |
+
model = AutoModelForMaskedLM.from_pretrained("rufimelo/Legal-BERTimbau-base")
|
41 |
+
```
|
42 |
+
|
43 |
+
### Masked language modeling prediction example
|
44 |
+
|
45 |
+
```python
|
46 |
+
from transformers import pipeline
|
47 |
+
from transformers import AutoTokenizer, AutoModelForMaskedLM
|
48 |
+
|
49 |
+
tokenizer = AutoTokenizer.from_pretrained("rufimelo/Legal-BERTimbau-base")
|
50 |
+
model = AutoModelForMaskedLM.from_pretrained("rufimelo/Legal-BERTimbau-base")
|
51 |
+
|
52 |
+
pipe = pipeline('fill-mask', model=model, tokenizer=tokenizer)
|
53 |
+
pipe('O advogado apresentou [MASK] para o juíz')
|
54 |
+
# [{'score': 0.5034703612327576,
|
55 |
+
#'token': 8190,
|
56 |
+
#'token_str': 'recurso',
|
57 |
+
#'sequence': 'O advogado apresentou recurso para o juíz'},
|
58 |
+
#{'score': 0.07347951829433441,
|
59 |
+
#'token': 21973,
|
60 |
+
#'token_str': 'petição',
|
61 |
+
#'sequence': 'O advogado apresentou petição para o juíz'},
|
62 |
+
#{'score': 0.05165359005331993,
|
63 |
+
#'token': 4299,
|
64 |
+
#'token_str': 'resposta',
|
65 |
+
#'sequence': 'O advogado apresentou resposta para o juíz'},
|
66 |
+
#{'score': 0.04611917585134506,
|
67 |
+
#'token': 5265,
|
68 |
+
#'token_str': 'exposição',
|
69 |
+
#'sequence': 'O advogado apresentou exposição para o juíz'},
|
70 |
+
#{'score': 0.04068068787455559,
|
71 |
+
#'token': 19737, 'token_str':
|
72 |
+
#'alegações',
|
73 |
+
#'sequence': 'O advogado apresentou alegações para o juíz'}]
|
74 |
+
|
75 |
+
```
|
76 |
+
|
77 |
+
### For BERT embeddings
|
78 |
+
|
79 |
+
```python
|
80 |
+
import torch
|
81 |
+
from transformers import AutoModel
|
82 |
+
|
83 |
+
model = AutoModel.from_pretrained('rufimelo/Legal-BERTimbau-base')
|
84 |
+
input_ids = tokenizer.encode('O advogado apresentou recurso para o juíz', return_tensors='pt')
|
85 |
+
|
86 |
+
with torch.no_grad():
|
87 |
+
outs = model(input_ids)
|
88 |
+
encoded = outs[0][0, 1:-1]
|
89 |
+
|
90 |
+
#tensor([[ 0.0328, -0.4292, -0.6230, ..., -0.3048, -0.5674, 0.0157],
|
91 |
+
#[-0.3569, 0.3326, 0.7013, ..., -0.7778, 0.2646, 1.1310],
|
92 |
+
#[ 0.3169, 0.4333, 0.2026, ..., 1.0517, -0.1951, 0.7050],
|
93 |
+
#...,
|
94 |
+
#[-0.3648, -0.8137, -0.4764, ..., -0.2725, -0.4879, 0.6264],
|
95 |
+
#[-0.2264, -0.1821, -0.3011, ..., -0.5428, 0.1429, 0.0509],
|
96 |
+
#[-1.4617, 0.6281, -0.0625, ..., -1.2774, -0.4491, 0.3131]])
|
97 |
+
```
|
98 |
+
|
99 |
+
## Citation
|
100 |
+
|
101 |
+
If you use this work, please cite BERTimbau's work:
|
102 |
+
|
103 |
+
```bibtex
|
104 |
+
@inproceedings{souza2020bertimbau,
|
105 |
+
author = {F{\'a}bio Souza and
|
106 |
+
Rodrigo Nogueira and
|
107 |
+
Roberto Lotufo},
|
108 |
+
title = {{BERT}imbau: pretrained {BERT} models for {B}razilian {P}ortuguese},
|
109 |
+
booktitle = {9th Brazilian Conference on Intelligent Systems, {BRACIS}, Rio Grande do Sul, Brazil, October 20-23 (to appear)},
|
110 |
+
year = {2020}
|
111 |
+
}
|
112 |
+
```
|