Upload PIBot Joint BERT model package
Browse files- README.md +164 -0
- __init__.py +1 -0
- added_tokens.json +3 -0
- config.json +36 -0
- labels/activity_label.txt +3 -0
- labels/calc_mode_label.txt +4 -0
- labels/investment_label.txt +3 -0
- labels/region_label.txt +3 -0
- labels/req_form_label.txt +3 -0
- labels/slot_label.txt +15 -0
- model.safetensors +3 -0
- modeling_jointbert.py +138 -0
- module.py +62 -0
- special_tokens_map.json +51 -0
- spm.model +3 -0
- tokenizer_config.json +58 -0
- training_args.bin +3 -0
README.md
ADDED
|
@@ -0,0 +1,164 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
language: es
|
| 3 |
+
tags:
|
| 4 |
+
- intent-classification
|
| 5 |
+
- slot-filling
|
| 6 |
+
- joint-bert
|
| 7 |
+
- spanish
|
| 8 |
+
- economics
|
| 9 |
+
- chile
|
| 10 |
+
- multi-head
|
| 11 |
+
license: mit
|
| 12 |
+
base_model: microsoft/mdeberta-v3-base
|
| 13 |
+
pipeline_tag: token-classification
|
| 14 |
+
---
|
| 15 |
+
|
| 16 |
+
# PIBot Joint BERT
|
| 17 |
+
|
| 18 |
+
Modelo **Joint BERT multi-head** para clasificación de intención y slot filling,
|
| 19 |
+
especializado en consultas sobre indicadores macroeconómicos del Banco Central de Chile.
|
| 20 |
+
|
| 21 |
+
## Arquitectura
|
| 22 |
+
|
| 23 |
+
| Componente | Detalle |
|
| 24 |
+
|---|---|
|
| 25 |
+
| Base | `microsoft/mdeberta-v3-base` |
|
| 26 |
+
| Task | `pibimacecv3` |
|
| 27 |
+
| Intent heads | 5 (`activity`, `calc_mode`, `investment`, `region`, `req_form`) |
|
| 28 |
+
| Slot labels | 15 (BIO) |
|
| 29 |
+
| Custom code | `modeling_jointbert.py`, `module.py` |
|
| 30 |
+
|
| 31 |
+
### Intent Heads
|
| 32 |
+
|
| 33 |
+
| Head | Clases | Valores |
|
| 34 |
+
|---|---|---|
|
| 35 |
+
| `activity` | 3 | `none`, `specific`, `general` |
|
| 36 |
+
| `calc_mode` | 4 | `original`, `prev_period`, `yoy`, `contribution` |
|
| 37 |
+
| `investment` | 3 | `none`, `specific`, `general` |
|
| 38 |
+
| `region` | 3 | `none`, `specific`, `general` |
|
| 39 |
+
| `req_form` | 3 | `latest`, `point`, `range` |
|
| 40 |
+
|
| 41 |
+
### Slot Entities (BIO)
|
| 42 |
+
|
| 43 |
+
Entidades extraídas: `activity`, `frequency`, `indicator`, `investment`, `period`, `region`, `seasonality`
|
| 44 |
+
|
| 45 |
+
Esquema BIO completo: 15 etiquetas (`O`, `B-*`, `I-*`).
|
| 46 |
+
|
| 47 |
+
## Uso
|
| 48 |
+
|
| 49 |
+
### Instalación
|
| 50 |
+
|
| 51 |
+
```bash
|
| 52 |
+
pip install torch transformers
|
| 53 |
+
```
|
| 54 |
+
|
| 55 |
+
### Carga del Modelo
|
| 56 |
+
|
| 57 |
+
```python
|
| 58 |
+
import torch
|
| 59 |
+
from transformers import AutoTokenizer, AutoConfig
|
| 60 |
+
|
| 61 |
+
# Cargar tokenizer y config
|
| 62 |
+
tokenizer = AutoTokenizer.from_pretrained("BCCh/pibert", trust_remote_code=True)
|
| 63 |
+
config = AutoConfig.from_pretrained("BCCh/pibert", trust_remote_code=True)
|
| 64 |
+
|
| 65 |
+
# Cargar labels desde el repo
|
| 66 |
+
from huggingface_hub import hf_hub_download
|
| 67 |
+
import os
|
| 68 |
+
|
| 69 |
+
label_dir = os.path.dirname(hf_hub_download("BCCh/pibert", "labels/slot_label.txt"))
|
| 70 |
+
|
| 71 |
+
# Leer intent y slot labels
|
| 72 |
+
def read_labels(path):
|
| 73 |
+
with open(path) as f:
|
| 74 |
+
return [line.strip() for line in f if line.strip()]
|
| 75 |
+
|
| 76 |
+
slot_labels = read_labels(os.path.join(label_dir, "slot_label.txt"))
|
| 77 |
+
|
| 78 |
+
# Preparar intent_label_lst para cada head
|
| 79 |
+
intent_label_lst = []
|
| 80 |
+
for head in ['activity', 'calc_mode', 'investment', 'region', 'req_form']:
|
| 81 |
+
intent_label_lst.append(read_labels(os.path.join(label_dir, f"{head}_label.txt")))
|
| 82 |
+
|
| 83 |
+
# Cargar modelo con custom code
|
| 84 |
+
from transformers import AutoModelForTokenClassification
|
| 85 |
+
from modeling_jointbert import JointBERT # auto-cargado con trust_remote_code
|
| 86 |
+
|
| 87 |
+
model = JointBERT.from_pretrained(
|
| 88 |
+
"BCCh/pibert",
|
| 89 |
+
config=config,
|
| 90 |
+
intent_label_lst=intent_label_lst,
|
| 91 |
+
slot_label_lst=slot_labels,
|
| 92 |
+
trust_remote_code=True,
|
| 93 |
+
)
|
| 94 |
+
model.eval()
|
| 95 |
+
```
|
| 96 |
+
|
| 97 |
+
### Predicción
|
| 98 |
+
|
| 99 |
+
```python
|
| 100 |
+
text = "cuál fue el imacec de agosto 2024"
|
| 101 |
+
tokens = tokenizer(text, return_tensors="pt", padding=True, truncation=True)
|
| 102 |
+
|
| 103 |
+
with torch.no_grad():
|
| 104 |
+
outputs = model(**tokens)
|
| 105 |
+
# outputs contiene intent_logits (lista) y slot_logits
|
| 106 |
+
```
|
| 107 |
+
|
| 108 |
+
## Estructura del Paquete
|
| 109 |
+
|
| 110 |
+
```
|
| 111 |
+
model_package/
|
| 112 |
+
├── config.json # Configuración BERT + task
|
| 113 |
+
├── model.safetensors # Pesos del modelo
|
| 114 |
+
├── tokenizer.json # Tokenizer
|
| 115 |
+
├── tokenizer_config.json
|
| 116 |
+
├── special_tokens_map.json
|
| 117 |
+
├── vocab.txt
|
| 118 |
+
├── modeling_jointbert.py # Arquitectura JointBERT (custom)
|
| 119 |
+
├── module.py # CRF y módulos auxiliares
|
| 120 |
+
├── __init__.py
|
| 121 |
+
├── README.md # Este archivo
|
| 122 |
+
└── labels/
|
| 123 |
+
├── slot_label.txt
|
| 124 |
+
├── activity_label.txt
|
| 125 |
+
├── calc_mode_label.txt
|
| 126 |
+
├── investment_label.txt
|
| 127 |
+
├── region_label.txt
|
| 128 |
+
├── req_form_label.txt
|
| 129 |
+
```
|
| 130 |
+
|
| 131 |
+
## Datos de Entrenamiento
|
| 132 |
+
|
| 133 |
+
Entrenado con datos de consultas sobre indicadores macroeconómicos chilenos:
|
| 134 |
+
- **IMACEC** (Indicador Mensual de Actividad Económica)
|
| 135 |
+
- **PIB** (Producto Interno Bruto)
|
| 136 |
+
- Sectores económicos, frecuencias, períodos, regiones
|
| 137 |
+
|
| 138 |
+
## Limitaciones
|
| 139 |
+
|
| 140 |
+
- Especializado en consultas macroeconómicas del Banco Central de Chile
|
| 141 |
+
- Mejor rendimiento en consultas cortas (< 50 tokens)
|
| 142 |
+
- Requiere `trust_remote_code=True` por la arquitectura custom
|
| 143 |
+
|
| 144 |
+
## Cita
|
| 145 |
+
|
| 146 |
+
```bibtex
|
| 147 |
+
@misc{pibot-jointbert,
|
| 148 |
+
author = {Banco Central de Chile},
|
| 149 |
+
title = {PIBot Joint BERT - Multi-head Intent + Slot Filling},
|
| 150 |
+
year = {2025},
|
| 151 |
+
publisher = {Hugging Face},
|
| 152 |
+
howpublished = {\url{https://huggingface.co/BCCh/pibert}}
|
| 153 |
+
}
|
| 154 |
+
```
|
| 155 |
+
|
| 156 |
+
## Referencias
|
| 157 |
+
|
| 158 |
+
- [BERT for Joint Intent Classification and Slot Filling](https://arxiv.org/abs/1902.10909)
|
| 159 |
+
- [JointBERT implementation](https://github.com/monologg/JointBERT)
|
| 160 |
+
- [BETO: Spanish BERT](https://github.com/dccuchile/beto)
|
| 161 |
+
|
| 162 |
+
## Licencia
|
| 163 |
+
|
| 164 |
+
MIT License
|
__init__.py
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
from .modeling_jointbert import JointBERT
|
added_tokens.json
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"[MASK]": 250101
|
| 3 |
+
}
|
config.json
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"_name_or_path": "microsoft/mdeberta-v3-base",
|
| 3 |
+
"architectures": [
|
| 4 |
+
"JointBERT"
|
| 5 |
+
],
|
| 6 |
+
"attention_probs_dropout_prob": 0.1,
|
| 7 |
+
"finetuning_task": "pibimacecv3",
|
| 8 |
+
"hidden_act": "gelu",
|
| 9 |
+
"hidden_dropout_prob": 0.1,
|
| 10 |
+
"hidden_size": 768,
|
| 11 |
+
"initializer_range": 0.02,
|
| 12 |
+
"intermediate_size": 3072,
|
| 13 |
+
"layer_norm_eps": 1e-07,
|
| 14 |
+
"max_position_embeddings": 512,
|
| 15 |
+
"max_relative_positions": -1,
|
| 16 |
+
"model_type": "deberta-v2",
|
| 17 |
+
"norm_rel_ebd": "layer_norm",
|
| 18 |
+
"num_attention_heads": 12,
|
| 19 |
+
"num_hidden_layers": 12,
|
| 20 |
+
"pad_token_id": 0,
|
| 21 |
+
"pooler_dropout": 0,
|
| 22 |
+
"pooler_hidden_act": "gelu",
|
| 23 |
+
"pooler_hidden_size": 768,
|
| 24 |
+
"pos_att_type": [
|
| 25 |
+
"p2c",
|
| 26 |
+
"c2p"
|
| 27 |
+
],
|
| 28 |
+
"position_biased_input": false,
|
| 29 |
+
"position_buckets": 256,
|
| 30 |
+
"relative_attention": true,
|
| 31 |
+
"share_att_key": true,
|
| 32 |
+
"torch_dtype": "float32",
|
| 33 |
+
"transformers_version": "4.46.0",
|
| 34 |
+
"type_vocab_size": 0,
|
| 35 |
+
"vocab_size": 251000
|
| 36 |
+
}
|
labels/activity_label.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
none
|
| 2 |
+
specific
|
| 3 |
+
general
|
labels/calc_mode_label.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
original
|
| 2 |
+
prev_period
|
| 3 |
+
yoy
|
| 4 |
+
contribution
|
labels/investment_label.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
none
|
| 2 |
+
specific
|
| 3 |
+
general
|
labels/region_label.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
none
|
| 2 |
+
specific
|
| 3 |
+
general
|
labels/req_form_label.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
latest
|
| 2 |
+
point
|
| 3 |
+
range
|
labels/slot_label.txt
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
O
|
| 2 |
+
B-ACTIVITY
|
| 3 |
+
B-FREQUENCY
|
| 4 |
+
B-INDICATOR
|
| 5 |
+
B-INVESTMENT
|
| 6 |
+
B-PERIOD
|
| 7 |
+
B-REGION
|
| 8 |
+
B-SEASONALITY
|
| 9 |
+
I-ACTIVITY
|
| 10 |
+
I-FREQUENCY
|
| 11 |
+
I-INDICATOR
|
| 12 |
+
I-INVESTMENT
|
| 13 |
+
I-PERIOD
|
| 14 |
+
I-REGION
|
| 15 |
+
I-SEASONALITY
|
model.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:b9e8b66c4ad817ab5a3c0651f984607c39c29c4c017a43a13bb7508f37254246
|
| 3 |
+
size 1112997152
|
modeling_jointbert.py
ADDED
|
@@ -0,0 +1,138 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import torch.nn as nn
|
| 3 |
+
from transformers import PreTrainedModel, AutoModel
|
| 4 |
+
from .module import CalcModeClassifier, ActivityClassifier, RegionClassifier, InvestmentClassifier, ReqFormClassifier, SlotClassifier
|
| 5 |
+
|
| 6 |
+
try:
|
| 7 |
+
from torchcrf import CRF
|
| 8 |
+
except ImportError:
|
| 9 |
+
CRF = None
|
| 10 |
+
|
| 11 |
+
class JointBERT(PreTrainedModel):
|
| 12 |
+
def __init__(self, config, args, calc_mode_label_lst, activity_label_lst, region_label_lst, investment_label_lst, req_form_label_lst, slot_label_lst):
|
| 13 |
+
super(JointBERT, self).__init__(config)
|
| 14 |
+
self.args = args
|
| 15 |
+
|
| 16 |
+
self.num_calc_mode_labels = len(calc_mode_label_lst)
|
| 17 |
+
self.num_activity_labels = len(activity_label_lst)
|
| 18 |
+
self.num_region_labels = len(region_label_lst)
|
| 19 |
+
self.num_investment_labels = len(investment_label_lst)
|
| 20 |
+
self.num_req_form_labels = len(req_form_label_lst)
|
| 21 |
+
self.num_slot_labels = len(slot_label_lst)
|
| 22 |
+
|
| 23 |
+
# Usar AutoModel para soportar cualquier encoder transformer
|
| 24 |
+
self.encoder = AutoModel.from_pretrained(args.model_name_or_path, config=config)
|
| 25 |
+
|
| 26 |
+
self.calc_mode_classifier = CalcModeClassifier(config.hidden_size, self.num_calc_mode_labels, args.dropout_rate)
|
| 27 |
+
self.activity_classifier = ActivityClassifier(config.hidden_size, self.num_activity_labels, args.dropout_rate)
|
| 28 |
+
self.region_classifier = RegionClassifier(config.hidden_size, self.num_region_labels, args.dropout_rate)
|
| 29 |
+
self.investment_classifier = InvestmentClassifier(config.hidden_size, self.num_investment_labels, args.dropout_rate)
|
| 30 |
+
self.req_form_classifier = ReqFormClassifier(config.hidden_size, self.num_req_form_labels, args.dropout_rate)
|
| 31 |
+
self.slot_classifier = SlotClassifier(config.hidden_size, self.num_slot_labels, args.dropout_rate)
|
| 32 |
+
|
| 33 |
+
if args.use_crf:
|
| 34 |
+
if CRF is None:
|
| 35 |
+
raise ImportError("torchcrf no está instalado. Instala con: pip install pytorch-crf o ejecuta sin --use_crf")
|
| 36 |
+
crf_init_errors = []
|
| 37 |
+
for init_fn in (
|
| 38 |
+
lambda: CRF(self.num_slot_labels, pad_idx=None, use_gpu=False),
|
| 39 |
+
lambda: CRF(self.num_slot_labels, batch_first=True),
|
| 40 |
+
lambda: CRF(num_tags=self.num_slot_labels, batch_first=True),
|
| 41 |
+
lambda: CRF(self.num_slot_labels),
|
| 42 |
+
lambda: CRF(num_tags=self.num_slot_labels),
|
| 43 |
+
):
|
| 44 |
+
try:
|
| 45 |
+
self.crf = init_fn()
|
| 46 |
+
break
|
| 47 |
+
except TypeError as e:
|
| 48 |
+
crf_init_errors.append(str(e))
|
| 49 |
+
else:
|
| 50 |
+
raise TypeError("No se pudo inicializar CRF con las firmas conocidas: " + " | ".join(crf_init_errors))
|
| 51 |
+
|
| 52 |
+
def forward(self, input_ids, attention_mask, token_type_ids=None,
|
| 53 |
+
calc_mode_label_ids=None, activity_label_ids=None, region_label_ids=None, investment_label_ids=None, req_form_label_ids=None, slot_labels_ids=None):
|
| 54 |
+
outputs = self.encoder(input_ids, attention_mask=attention_mask,
|
| 55 |
+
token_type_ids=token_type_ids) # sequence_output, pooled_output, (hidden_states), (attentions)
|
| 56 |
+
sequence_output = outputs[0]
|
| 57 |
+
pooled_output = getattr(outputs, "pooler_output", None)
|
| 58 |
+
if pooled_output is None:
|
| 59 |
+
if len(outputs) > 1 and outputs[1] is not None and getattr(outputs[1], "dim", lambda: 0)() == 2:
|
| 60 |
+
pooled_output = outputs[1]
|
| 61 |
+
else:
|
| 62 |
+
pooled_output = sequence_output[:, 0]
|
| 63 |
+
|
| 64 |
+
calc_mode_logits = self.calc_mode_classifier(pooled_output)
|
| 65 |
+
activity_logits = self.activity_classifier(pooled_output)
|
| 66 |
+
region_logits = self.region_classifier(pooled_output)
|
| 67 |
+
investment_logits = self.investment_classifier(pooled_output)
|
| 68 |
+
req_form_logits = self.req_form_classifier(pooled_output)
|
| 69 |
+
slot_logits = self.slot_classifier(sequence_output)
|
| 70 |
+
|
| 71 |
+
total_loss = 0
|
| 72 |
+
|
| 73 |
+
def _get_weight(head_name):
|
| 74 |
+
"""Retorna class weights registrados como buffer, o None."""
|
| 75 |
+
buf_name = f"{head_name}_class_weights"
|
| 76 |
+
w = getattr(self, buf_name, None)
|
| 77 |
+
return w
|
| 78 |
+
|
| 79 |
+
# 1. Calc Mode CrossEntropy
|
| 80 |
+
if calc_mode_label_ids is not None:
|
| 81 |
+
calc_mode_loss_fct = nn.CrossEntropyLoss(weight=_get_weight('calc_mode'))
|
| 82 |
+
calc_mode_loss = calc_mode_loss_fct(calc_mode_logits.view(-1, self.num_calc_mode_labels), calc_mode_label_ids.view(-1))
|
| 83 |
+
total_loss += calc_mode_loss
|
| 84 |
+
|
| 85 |
+
# 2. Activity CrossEntropy
|
| 86 |
+
if activity_label_ids is not None:
|
| 87 |
+
activity_loss_fct = nn.CrossEntropyLoss(weight=_get_weight('activity'))
|
| 88 |
+
activity_loss = activity_loss_fct(activity_logits.view(-1, self.num_activity_labels), activity_label_ids.view(-1))
|
| 89 |
+
total_loss += activity_loss
|
| 90 |
+
|
| 91 |
+
# 3. Region CrossEntropy
|
| 92 |
+
if region_label_ids is not None:
|
| 93 |
+
region_loss_fct = nn.CrossEntropyLoss(weight=_get_weight('region'))
|
| 94 |
+
region_loss = region_loss_fct(region_logits.view(-1, self.num_region_labels), region_label_ids.view(-1))
|
| 95 |
+
total_loss += region_loss
|
| 96 |
+
|
| 97 |
+
# 4. Investment CrossEntropy
|
| 98 |
+
if investment_label_ids is not None:
|
| 99 |
+
investment_loss_fct = nn.CrossEntropyLoss(weight=_get_weight('investment'))
|
| 100 |
+
investment_loss = investment_loss_fct(investment_logits.view(-1, self.num_investment_labels), investment_label_ids.view(-1))
|
| 101 |
+
total_loss += investment_loss
|
| 102 |
+
|
| 103 |
+
# 5. Req Form CrossEntropy
|
| 104 |
+
if req_form_label_ids is not None:
|
| 105 |
+
req_form_loss_fct = nn.CrossEntropyLoss(weight=_get_weight('req_form'))
|
| 106 |
+
req_form_loss = req_form_loss_fct(req_form_logits.view(-1, self.num_req_form_labels), req_form_label_ids.view(-1))
|
| 107 |
+
total_loss += req_form_loss
|
| 108 |
+
|
| 109 |
+
# 6. Slot Softmax
|
| 110 |
+
if slot_labels_ids is not None and self.args.slot_loss_coef != 0:
|
| 111 |
+
if self.args.use_crf:
|
| 112 |
+
# CRF doesn't handle ignore_index (-100), so we replace it with PAD (0)
|
| 113 |
+
slot_labels_ids_crf = slot_labels_ids.clone()
|
| 114 |
+
slot_labels_ids_crf[slot_labels_ids_crf == self.args.ignore_index] = 0
|
| 115 |
+
if hasattr(self.crf, 'viterbi_decode'):
|
| 116 |
+
# TorchCRF API: forward returns log-likelihood per batch item
|
| 117 |
+
slot_loss = -self.crf(slot_logits, slot_labels_ids_crf, attention_mask.bool()).mean()
|
| 118 |
+
else:
|
| 119 |
+
# pytorch-crf API
|
| 120 |
+
slot_loss = self.crf(slot_logits, slot_labels_ids_crf, mask=attention_mask.bool(), reduction='mean')
|
| 121 |
+
slot_loss = -1 * slot_loss # negative log-likelihood
|
| 122 |
+
else:
|
| 123 |
+
slot_loss_fct = nn.CrossEntropyLoss(ignore_index=self.args.ignore_index)
|
| 124 |
+
# Only keep active parts of the loss
|
| 125 |
+
if attention_mask is not None:
|
| 126 |
+
active_loss = attention_mask.view(-1) == 1
|
| 127 |
+
active_logits = slot_logits.view(-1, self.num_slot_labels)[active_loss]
|
| 128 |
+
active_labels = slot_labels_ids.view(-1)[active_loss]
|
| 129 |
+
slot_loss = slot_loss_fct(active_logits, active_labels)
|
| 130 |
+
else:
|
| 131 |
+
slot_loss = slot_loss_fct(slot_logits.view(-1, self.num_slot_labels), slot_labels_ids.view(-1))
|
| 132 |
+
total_loss += self.args.slot_loss_coef * slot_loss
|
| 133 |
+
|
| 134 |
+
outputs = ((calc_mode_logits, activity_logits, region_logits, investment_logits, req_form_logits, slot_logits),) + outputs[2:] # add hidden states and attention if they are here
|
| 135 |
+
|
| 136 |
+
outputs = (total_loss,) + outputs
|
| 137 |
+
|
| 138 |
+
return outputs # (loss), logits, (hidden_states), (attentions) # Logits is a tuple of all classifier logits
|
module.py
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch.nn as nn
|
| 2 |
+
|
| 3 |
+
class CalcModeClassifier(nn.Module):
|
| 4 |
+
def __init__(self, input_dim, num_calc_mode_labels, dropout_rate=0.):
|
| 5 |
+
super(CalcModeClassifier, self).__init__()
|
| 6 |
+
self.dropout = nn.Dropout(dropout_rate)
|
| 7 |
+
self.linear = nn.Linear(input_dim, num_calc_mode_labels)
|
| 8 |
+
|
| 9 |
+
def forward(self, x):
|
| 10 |
+
x = self.dropout(x)
|
| 11 |
+
return self.linear(x)
|
| 12 |
+
|
| 13 |
+
class ActivityClassifier(nn.Module):
|
| 14 |
+
def __init__(self, input_dim, num_activity_labels, dropout_rate=0.):
|
| 15 |
+
super(ActivityClassifier, self).__init__()
|
| 16 |
+
self.dropout = nn.Dropout(dropout_rate)
|
| 17 |
+
self.linear = nn.Linear(input_dim, num_activity_labels)
|
| 18 |
+
|
| 19 |
+
def forward(self, x):
|
| 20 |
+
x = self.dropout(x)
|
| 21 |
+
return self.linear(x)
|
| 22 |
+
|
| 23 |
+
class RegionClassifier(nn.Module):
|
| 24 |
+
def __init__(self, input_dim, num_region_labels, dropout_rate=0.):
|
| 25 |
+
super(RegionClassifier, self).__init__()
|
| 26 |
+
self.dropout = nn.Dropout(dropout_rate)
|
| 27 |
+
self.linear = nn.Linear(input_dim, num_region_labels)
|
| 28 |
+
|
| 29 |
+
def forward(self, x):
|
| 30 |
+
x = self.dropout(x)
|
| 31 |
+
return self.linear(x)
|
| 32 |
+
|
| 33 |
+
class InvestmentClassifier(nn.Module):
|
| 34 |
+
def __init__(self, input_dim, num_investment_labels, dropout_rate=0.):
|
| 35 |
+
super(InvestmentClassifier, self).__init__()
|
| 36 |
+
self.dropout = nn.Dropout(dropout_rate)
|
| 37 |
+
self.linear = nn.Linear(input_dim, num_investment_labels)
|
| 38 |
+
|
| 39 |
+
def forward(self, x):
|
| 40 |
+
x = self.dropout(x)
|
| 41 |
+
return self.linear(x)
|
| 42 |
+
|
| 43 |
+
class ReqFormClassifier(nn.Module):
|
| 44 |
+
def __init__(self, input_dim, num_req_form_labels, dropout_rate=0.):
|
| 45 |
+
super(ReqFormClassifier, self).__init__()
|
| 46 |
+
self.dropout = nn.Dropout(dropout_rate)
|
| 47 |
+
self.linear = nn.Linear(input_dim, num_req_form_labels)
|
| 48 |
+
|
| 49 |
+
def forward(self, x):
|
| 50 |
+
x = self.dropout(x)
|
| 51 |
+
return self.linear(x)
|
| 52 |
+
|
| 53 |
+
class SlotClassifier(nn.Module):
|
| 54 |
+
def __init__(self, input_dim, num_slot_labels, dropout_rate=0.):
|
| 55 |
+
super(SlotClassifier, self).__init__()
|
| 56 |
+
self.dropout = nn.Dropout(dropout_rate)
|
| 57 |
+
self.linear = nn.Linear(input_dim, num_slot_labels)
|
| 58 |
+
|
| 59 |
+
def forward(self, x):
|
| 60 |
+
x = self.dropout(x)
|
| 61 |
+
return self.linear(x)
|
| 62 |
+
|
special_tokens_map.json
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"bos_token": {
|
| 3 |
+
"content": "[CLS]",
|
| 4 |
+
"lstrip": false,
|
| 5 |
+
"normalized": false,
|
| 6 |
+
"rstrip": false,
|
| 7 |
+
"single_word": false
|
| 8 |
+
},
|
| 9 |
+
"cls_token": {
|
| 10 |
+
"content": "[CLS]",
|
| 11 |
+
"lstrip": false,
|
| 12 |
+
"normalized": false,
|
| 13 |
+
"rstrip": false,
|
| 14 |
+
"single_word": false
|
| 15 |
+
},
|
| 16 |
+
"eos_token": {
|
| 17 |
+
"content": "[SEP]",
|
| 18 |
+
"lstrip": false,
|
| 19 |
+
"normalized": false,
|
| 20 |
+
"rstrip": false,
|
| 21 |
+
"single_word": false
|
| 22 |
+
},
|
| 23 |
+
"mask_token": {
|
| 24 |
+
"content": "[MASK]",
|
| 25 |
+
"lstrip": false,
|
| 26 |
+
"normalized": false,
|
| 27 |
+
"rstrip": false,
|
| 28 |
+
"single_word": false
|
| 29 |
+
},
|
| 30 |
+
"pad_token": {
|
| 31 |
+
"content": "[PAD]",
|
| 32 |
+
"lstrip": false,
|
| 33 |
+
"normalized": false,
|
| 34 |
+
"rstrip": false,
|
| 35 |
+
"single_word": false
|
| 36 |
+
},
|
| 37 |
+
"sep_token": {
|
| 38 |
+
"content": "[SEP]",
|
| 39 |
+
"lstrip": false,
|
| 40 |
+
"normalized": false,
|
| 41 |
+
"rstrip": false,
|
| 42 |
+
"single_word": false
|
| 43 |
+
},
|
| 44 |
+
"unk_token": {
|
| 45 |
+
"content": "[UNK]",
|
| 46 |
+
"lstrip": false,
|
| 47 |
+
"normalized": true,
|
| 48 |
+
"rstrip": false,
|
| 49 |
+
"single_word": false
|
| 50 |
+
}
|
| 51 |
+
}
|
spm.model
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:13c8d666d62a7bc4ac8f040aab68e942c861f93303156cc28f5c7e885d86d6e3
|
| 3 |
+
size 4305025
|
tokenizer_config.json
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"added_tokens_decoder": {
|
| 3 |
+
"0": {
|
| 4 |
+
"content": "[PAD]",
|
| 5 |
+
"lstrip": false,
|
| 6 |
+
"normalized": false,
|
| 7 |
+
"rstrip": false,
|
| 8 |
+
"single_word": false,
|
| 9 |
+
"special": true
|
| 10 |
+
},
|
| 11 |
+
"1": {
|
| 12 |
+
"content": "[CLS]",
|
| 13 |
+
"lstrip": false,
|
| 14 |
+
"normalized": false,
|
| 15 |
+
"rstrip": false,
|
| 16 |
+
"single_word": false,
|
| 17 |
+
"special": true
|
| 18 |
+
},
|
| 19 |
+
"2": {
|
| 20 |
+
"content": "[SEP]",
|
| 21 |
+
"lstrip": false,
|
| 22 |
+
"normalized": false,
|
| 23 |
+
"rstrip": false,
|
| 24 |
+
"single_word": false,
|
| 25 |
+
"special": true
|
| 26 |
+
},
|
| 27 |
+
"3": {
|
| 28 |
+
"content": "[UNK]",
|
| 29 |
+
"lstrip": false,
|
| 30 |
+
"normalized": true,
|
| 31 |
+
"rstrip": false,
|
| 32 |
+
"single_word": false,
|
| 33 |
+
"special": true
|
| 34 |
+
},
|
| 35 |
+
"250101": {
|
| 36 |
+
"content": "[MASK]",
|
| 37 |
+
"lstrip": false,
|
| 38 |
+
"normalized": false,
|
| 39 |
+
"rstrip": false,
|
| 40 |
+
"single_word": false,
|
| 41 |
+
"special": true
|
| 42 |
+
}
|
| 43 |
+
},
|
| 44 |
+
"bos_token": "[CLS]",
|
| 45 |
+
"clean_up_tokenization_spaces": false,
|
| 46 |
+
"cls_token": "[CLS]",
|
| 47 |
+
"do_lower_case": false,
|
| 48 |
+
"eos_token": "[SEP]",
|
| 49 |
+
"mask_token": "[MASK]",
|
| 50 |
+
"model_max_length": 1000000000000000019884624838656,
|
| 51 |
+
"pad_token": "[PAD]",
|
| 52 |
+
"sep_token": "[SEP]",
|
| 53 |
+
"sp_model_kwargs": {},
|
| 54 |
+
"split_by_punct": false,
|
| 55 |
+
"tokenizer_class": "DebertaV2Tokenizer",
|
| 56 |
+
"unk_token": "[UNK]",
|
| 57 |
+
"vocab_type": "spm"
|
| 58 |
+
}
|
training_args.bin
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:6a815201ec693183d7090277dcbe0b6ccbfd934137cdd1bfefabe2ee4cff99fe
|
| 3 |
+
size 2040
|