模型概述
模型特點
模型能力
使用案例
🚀 (BERT large) 葡萄牙語法律領域命名實體識別(NER)模型(LeNER - Br)
ner - bert - large - portuguese - cased - lenerbr 是一個用於葡萄牙語法律領域的命名實體識別(NER)模型(標記分類)。該模型於 2021 年 12 月 20 日在 Google Colab 上,基於 [pierreguillou/bert - large - cased - pt - lenerbr](https://huggingface.co/pierreguillou/bert - large - cased - pt - lenerbr) 模型,在 LeNER_br 數據集上使用 NER 目標進行微調得到。
由於微調數據集規模較小,模型在訓練結束前出現了過擬合現象。以下是驗證數據集上的整體最終指標(注意:查看“按命名實體劃分的驗證指標”段落可獲取詳細指標):
- f1 值:0.9082022949426265
- 精確率:0.8975220495590088
- 召回率:0.9191397849462366
- 準確率:0.9808310603867311
- 損失值:0.1228889599442482
你也可以查看 [此模型的基礎版本](https://huggingface.co/pierreguillou/ner - bert - base - cased - pt - lenerbr),其 f1 值為 0.893。
注意:[pierreguillou/bert - large - cased - pt - lenerbr](https://huggingface.co/pierreguillou/bert - large - cased - pt - lenerbr) 模型是一個語言模型,它是通過在 LeNER - Br 語言建模 數據集上使用 MASK 目標對 [BERTimbau large](https://huggingface.co/neuralmind/bert - large - portuguese - cased) 模型進行微調而創建的。在進行 NER 任務微調之前對語言模型進行這種初步的專門化處理,有助於得到更好的 NER 模型。
📚 詳細文檔
博客文章
[《NLP | 用於巴西法律領域命名實體識別(NER)的模型和 Web 應用》](https://medium.com/@pierre_guillou/nlp - modelos - e - web - app - para - reconhecimento - de - entidade - nomeada - ner - no - domínio - jurídico - b658db55edfb)(2021 年 12 月 29 日)
小部件與應用程序
你可以在本頁面的小部件中測試此模型。
也可以使用 [NER 應用程序](https://huggingface.co/spaces/pierreguillou/ner - bert - pt - lenerbr),該應用程序允許將在法律 LeNER - Br 數據集上進行 NER 任務擬合的 2 個 BERT 模型(基礎版和大版本)進行比較。
💻 使用示例
基礎用法
# install pytorch: check https://pytorch.org/
# !pip install transformers
from transformers import AutoModelForTokenClassification, AutoTokenizer
import torch
# parameters
model_name = "pierreguillou/ner-bert-large-cased-pt-lenerbr"
model = AutoModelForTokenClassification.from_pretrained(model_name)
tokenizer = AutoTokenizer.from_pretrained(model_name)
input_text = "Acrescento que não há de se falar em violação do artigo 114, § 3º, da Constituição Federal, posto que referido dispositivo revela-se impertinente, tratando da possibilidade de ajuizamento de dissídio coletivo pelo Ministério Público do Trabalho nos casos de greve em atividade essencial."
# tokenization
inputs = tokenizer(input_text, max_length=512, truncation=True, return_tensors="pt")
tokens = inputs.tokens()
# get predictions
outputs = model(**inputs).logits
predictions = torch.argmax(outputs, dim=2)
# print predictions
for token, prediction in zip(tokens, predictions[0].numpy()):
print((token, model.config.id2label[prediction]))
你也可以使用管道(pipeline),不過,輸入序列的最大長度似乎存在問題。
高級用法
!pip install transformers
import transformers
from transformers import pipeline
model_name = "pierreguillou/ner-bert-large-cased-pt-lenerbr"
ner = pipeline(
"ner",
model=model_name
)
ner(input_text)
🔧 技術細節
訓練過程
筆記本
微調的筆記本([HuggingFace_Notebook_token_classification_NER_LeNER_Br.ipynb](https://github.com/piegu/language - models/blob/master/HuggingFace_Notebook_token_classification_NER_LeNER_Br.ipynb))可在 GitHub 上找到。
超參數
- 每個設備的批量大小:2
- 梯度累積步數:2
- 學習率:2e - 5
- 訓練輪數:10
- 權重衰減:0.01
- 優化器:AdamW
- 貝塔值:(0.9, 0.999)
- 學習率調度器類型:線性
- 隨機種子:42
保存模型與加載最佳模型
- 保存總數限制:7
- 日誌記錄步數:500
- 評估步數:日誌記錄步數
- 評估策略:按步數
- 日誌記錄策略:按步數
- 保存策略:按步數
- 保存步數:日誌記錄步數
- 訓練結束時加載最佳模型:是
- 混合精度訓練:是
通過指標獲取最佳模型
- 最佳模型的評估指標:'eval_f1'
- 指標越大越好:是
訓練結果
Num examples = 7828
Num Epochs = 20
Instantaneous batch size per device = 2
Total train batch size (w. parallel, distributed & accumulation) = 4
Gradient Accumulation steps = 2
Total optimization steps = 39140
Step Training Loss Validation Loss Precision Recall F1 Accuracy
500 0.250000 0.140582 0.760833 0.770323 0.765548 0.963125
1000 0.076200 0.117882 0.829082 0.817849 0.823428 0.966569
1500 0.082400 0.150047 0.679610 0.914624 0.779795 0.957213
2000 0.047500 0.133443 0.817678 0.857419 0.837077 0.969190
2500 0.034200 0.230139 0.895672 0.845591 0.869912 0.964070
3000 0.033800 0.108022 0.859225 0.887312 0.873043 0.973700
3500 0.030100 0.113467 0.855747 0.885376 0.870310 0.975879
4000 0.029900 0.118619 0.850207 0.884946 0.867229 0.974477
4500 0.022500 0.124327 0.841048 0.890968 0.865288 0.975041
5000 0.020200 0.129294 0.801538 0.918925 0.856227 0.968077
5500 0.019700 0.128344 0.814222 0.908602 0.858827 0.969250
6000 0.024600 0.182563 0.908087 0.866882 0.887006 0.968565
6500 0.012600 0.159217 0.829883 0.913763 0.869806 0.969357
7000 0.020600 0.183726 0.854557 0.893333 0.873515 0.966447
7500 0.014400 0.141395 0.777716 0.905161 0.836613 0.966828
8000 0.013400 0.139378 0.873042 0.899140 0.885899 0.975772
8500 0.014700 0.142521 0.864152 0.901505 0.882433 0.976366
9000 0.010900 0.122889 0.897522 0.919140 0.908202 0.980831
9500 0.013500 0.143407 0.816580 0.906667 0.859268 0.973395
10000 0.010400 0.144946 0.835608 0.908387 0.870479 0.974629
10500 0.007800 0.143086 0.847587 0.910108 0.877735 0.975985
11000 0.008200 0.156379 0.873778 0.884301 0.879008 0.976321
11500 0.008200 0.133356 0.901193 0.910108 0.905628 0.980328
12000 0.006900 0.133476 0.892202 0.920215 0.905992 0.980572
12500 0.006900 0.129991 0.890159 0.904516 0.897280 0.978683
按命名實體劃分的驗證指標
{'JURISPRUDENCIA': {'f1': 0.8135593220338984,
'number': 657,
'precision': 0.865979381443299,
'recall': 0.7671232876712328},
'LEGISLACAO': {'f1': 0.8888888888888888,
'number': 571,
'precision': 0.8952042628774423,
'recall': 0.882661996497373},
'LOCAL': {'f1': 0.850467289719626,
'number': 194,
'precision': 0.7777777777777778,
'recall': 0.9381443298969072},
'ORGANIZACAO': {'f1': 0.8740635033892258,
'number': 1340,
'precision': 0.8373205741626795,
'recall': 0.914179104477612},
'PESSOA': {'f1': 0.9836677554829678,
'number': 1072,
'precision': 0.9841269841269841,
'recall': 0.9832089552238806},
'TEMPO': {'f1': 0.9669669669669669,
'number': 816,
'precision': 0.9481743227326266,
'recall': 0.9865196078431373},
'overall_accuracy': 0.9808310603867311,
'overall_f1': 0.9082022949426265,
'overall_precision': 0.8975220495590088,
'overall_recall': 0.9191397849462366}
📄 許可證
文檔中未提及相關許可證信息。








