Upload folder using huggingface_hub
Browse files- agent_emotion_predict_classifier.py +131 -0
- best_model_agent.pt +2 -2
- config_agent.json +26 -0
- emotion_classifier_model.py +207 -0
- inference_agent_emotion_classifier.py +278 -0
- requirements.txt +86 -0
- train_agent_emotion_classifier.py +440 -0
agent_emotion_predict_classifier.py
ADDED
|
@@ -0,0 +1,131 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
=== MIA · Agent Emotion Predict Classifier (Text/BETO Embedder + MLP) ===
|
| 3 |
+
Objetivo: predecir la emoción del AGENTE (label_agent: 0..5) a partir de:
|
| 4 |
+
- el TEXTO del usuario
|
| 5 |
+
- la EMOCIÓN del texto (label del usuario: 0..5)
|
| 6 |
+
|
| 7 |
+
Arquitectura:
|
| 8 |
+
Texto ──▶ Embedder (TextEmbedder ó BETOEmbedder) ─▶ h_text ∈ R^D
|
| 9 |
+
Label usuario (0..5) ─▶ one-hot(6) ─▶ (feature dropout opcional)
|
| 10 |
+
Concatenación [h_text ; onehot_label] ─▶ MLP ─▶ logits (6)
|
| 11 |
+
|
| 12 |
+
Notas:
|
| 13 |
+
- Si usas BETOEmbedder, se recomienda congelarlo (freeze) para esta segunda red.
|
| 14 |
+
- El feature dropout en la one-hot del label obliga al modelo a mirar el TEXTO en los casos ambiguos.
|
| 15 |
+
"""
|
| 16 |
+
|
| 17 |
+
from typing import List, Optional
|
| 18 |
+
import torch
|
| 19 |
+
import torch.nn as nn
|
| 20 |
+
import torch.nn.functional as F
|
| 21 |
+
from emotion_classifier_model import TextEmbedder, BETOEmbedder # reemplaza con tu import real
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
class FeatureDropout(nn.Module):
|
| 25 |
+
"""Apaga aleatoriamente (con prob p) TODA la rama de la one-hot del label en entrenamiento.
|
| 26 |
+
Si p=0.2, en el 20% de los batches el modelo debe decidir solo con el texto.
|
| 27 |
+
"""
|
| 28 |
+
def __init__(self, p: float = 0.0):
|
| 29 |
+
super().__init__()
|
| 30 |
+
assert 0.0 <= p < 1.0
|
| 31 |
+
self.p = p
|
| 32 |
+
|
| 33 |
+
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
| 34 |
+
if not self.training or self.p <= 0.0:
|
| 35 |
+
return x
|
| 36 |
+
# Con prob p, zerea todo el vector (por muestra)
|
| 37 |
+
mask = (torch.rand(x.size(0), 1, device=x.device) > self.p).float()
|
| 38 |
+
return x * mask
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
class MLP(nn.Module):
|
| 42 |
+
def __init__(self, input_dim: int, hidden1: int = 256, hidden2: int = 64, num_classes: int = 6, dropout: float = 0.2):
|
| 43 |
+
super().__init__()
|
| 44 |
+
self.fc1 = nn.Linear(input_dim, hidden1)
|
| 45 |
+
self.fc2 = nn.Linear(hidden1, hidden2)
|
| 46 |
+
self.out = nn.Linear(hidden2, num_classes)
|
| 47 |
+
self.drop = nn.Dropout(dropout)
|
| 48 |
+
|
| 49 |
+
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
| 50 |
+
x = F.relu(self.fc1(x))
|
| 51 |
+
x = self.drop(x)
|
| 52 |
+
x = F.relu(self.fc2(x))
|
| 53 |
+
x = self.drop(x)
|
| 54 |
+
return self.out(x)
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
class AgentEmotionPredictClassifier(nn.Module):
|
| 58 |
+
"""
|
| 59 |
+
Segunda red: predice la emoción del AGENTE (0..5) a partir de (texto, label_usuario).
|
| 60 |
+
|
| 61 |
+
Parámetros clave:
|
| 62 |
+
- pretrained_encoder: None → TextEmbedder (emb_dim)
|
| 63 |
+
"beto" → BETOEmbedder (768D)
|
| 64 |
+
- label_feature_dropout: apaga la one-hot a veces para forzar al modelo a usar el texto en casos ambiguos.
|
| 65 |
+
"""
|
| 66 |
+
def __init__(
|
| 67 |
+
self,
|
| 68 |
+
model_name: str = "dccuchile/bert-base-spanish-wwm-cased",
|
| 69 |
+
pretrained_encoder: Optional[str] = "beto",
|
| 70 |
+
emb_dim: int = 300,
|
| 71 |
+
max_length: int = 128,
|
| 72 |
+
hidden1: int = 256,
|
| 73 |
+
hidden2: int = 64,
|
| 74 |
+
num_classes: int = 6,
|
| 75 |
+
dropout: float = 0.2,
|
| 76 |
+
label_feature_dropout: float = 0.15,
|
| 77 |
+
device: Optional[torch.device] = None,
|
| 78 |
+
):
|
| 79 |
+
super().__init__()
|
| 80 |
+
self.device = device or torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 81 |
+
|
| 82 |
+
if pretrained_encoder == "beto":
|
| 83 |
+
self.embedder = BETOEmbedder(model_name=model_name, max_length=max_length, device=self.device)
|
| 84 |
+
embed_dim = 768
|
| 85 |
+
else:
|
| 86 |
+
self.embedder = TextEmbedder(model_name=model_name, emb_dim=emb_dim, max_length=max_length, device=self.device)
|
| 87 |
+
embed_dim = emb_dim
|
| 88 |
+
|
| 89 |
+
self.label_dim = 6 # one-hot(6)
|
| 90 |
+
self.feat_drop = FeatureDropout(p=label_feature_dropout)
|
| 91 |
+
self.classifier = MLP(input_dim=embed_dim + self.label_dim,
|
| 92 |
+
hidden1=hidden1, hidden2=hidden2,
|
| 93 |
+
num_classes=num_classes, dropout=dropout) # num_classes = salida del AGENTE (ahora 2)
|
| 94 |
+
self.to(self.device)
|
| 95 |
+
|
| 96 |
+
# ---------- Utils ----------
|
| 97 |
+
@staticmethod
|
| 98 |
+
def _one_hot(labels: torch.Tensor, num_classes: int) -> torch.Tensor:
|
| 99 |
+
# labels: [B] int64 → one-hot [B, C]
|
| 100 |
+
return F.one_hot(labels.long(), num_classes=num_classes).float()
|
| 101 |
+
|
| 102 |
+
def freeze_encoder(self):
|
| 103 |
+
for p in self.embedder.parameters():
|
| 104 |
+
p.requires_grad = False
|
| 105 |
+
|
| 106 |
+
def unfreeze_encoder(self):
|
| 107 |
+
for p in self.embedder.parameters():
|
| 108 |
+
p.requires_grad = True
|
| 109 |
+
|
| 110 |
+
# ---------- Forward / Predict ----------
|
| 111 |
+
def forward(self, texts: List[str], user_labels: torch.Tensor) -> torch.Tensor:
|
| 112 |
+
"""texts: lista de strings (len=B)
|
| 113 |
+
user_labels: tensor [B] con labels del usuario (0..5)
|
| 114 |
+
"""
|
| 115 |
+
h_text = self.embedder.embed_batch(texts) # [B, D]
|
| 116 |
+
onehot = self._one_hot(user_labels.to(h_text.device), self.label_dim) # [B, 6]
|
| 117 |
+
onehot = self.feat_drop(onehot) # feature dropout (solo en train)
|
| 118 |
+
x = torch.cat([h_text, onehot], dim=-1) # [B, D+6]
|
| 119 |
+
logits = self.classifier(x) # [B, 6]
|
| 120 |
+
return logits
|
| 121 |
+
|
| 122 |
+
@torch.inference_mode()
|
| 123 |
+
def predict(self, texts: List[str], user_labels: torch.Tensor):
|
| 124 |
+
self.eval()
|
| 125 |
+
logits = self.forward(texts, user_labels)
|
| 126 |
+
probs = logits.softmax(dim=-1)
|
| 127 |
+
preds = probs.argmax(dim=-1)
|
| 128 |
+
return preds, probs
|
| 129 |
+
|
| 130 |
+
|
| 131 |
+
|
best_model_agent.pt
CHANGED
|
@@ -1,3 +1,3 @@
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
-
oid sha256:
|
| 3 |
-
size
|
|
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:e038b9a44ab93c55483da56820f6fb60742c0d8589d50893d38ccf2eaf2f9014
|
| 3 |
+
size 135
|
config_agent.json
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"model_type": "AgentEmotionPredictClassifier",
|
| 3 |
+
"base_model_id": "dccuchile/bert-base-spanish-wwm-cased",
|
| 4 |
+
"pretrained_encoder": "beto",
|
| 5 |
+
"max_length": 128,
|
| 6 |
+
|
| 7 |
+
"hidden1": 256,
|
| 8 |
+
"hidden2": 64,
|
| 9 |
+
"dropout": 0.4,
|
| 10 |
+
"label_feature_dropout": 0.5,
|
| 11 |
+
|
| 12 |
+
"num_classes": 2,
|
| 13 |
+
"present_classes": [1, 2],
|
| 14 |
+
"class_names": ["alegría", "amor"],
|
| 15 |
+
|
| 16 |
+
"label_space_global": {
|
| 17 |
+
"0": "tristeza",
|
| 18 |
+
"1": "alegría",
|
| 19 |
+
"2": "amor",
|
| 20 |
+
"3": "ira",
|
| 21 |
+
"4": "miedo",
|
| 22 |
+
"5": "sorpresa"
|
| 23 |
+
},
|
| 24 |
+
|
| 25 |
+
"notes": "Head binaria entrenada solo con clases 1(alegría) y 2(amor); mapping preservado en present_classes/class_names."
|
| 26 |
+
}
|
emotion_classifier_model.py
ADDED
|
@@ -0,0 +1,207 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
=== MIA · Clasificador de Emociones (Pretrained Encoder + MLP) ===
|
| 3 |
+
- Mantiene compatibilidad con tu API pública.
|
| 4 |
+
- Permite usar tu TextEmbedder aleatorio (emb_dim) o un encoder preentrenado (BETO) con 768D.
|
| 5 |
+
- Expone freeze/unfreeze para controlar el fine-tuning desde el trainer.
|
| 6 |
+
"""
|
| 7 |
+
|
| 8 |
+
import torch
|
| 9 |
+
import torch.nn as nn
|
| 10 |
+
from typing import List, Optional
|
| 11 |
+
from transformers import AutoTokenizer, AutoModel
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
# ==================== MÓDULO 1A: TextEmbedder (embedding aleatorio) ====================
|
| 15 |
+
class TextEmbedder(nn.Module):
|
| 16 |
+
"""
|
| 17 |
+
Módulo de Embedding simple:
|
| 18 |
+
- Usa el tokenizador de BETO para sub-palabras (por conveniencia, vocab, pad_id, etc.)
|
| 19 |
+
- La representación es un embedding aleatorio + mean pooling (no contextual).
|
| 20 |
+
"""
|
| 21 |
+
def __init__(
|
| 22 |
+
self,
|
| 23 |
+
model_name: str = "dccuchile/bert-base-spanish-wwm-cased",
|
| 24 |
+
emb_dim: int = 300,
|
| 25 |
+
max_length: int = 128,
|
| 26 |
+
device: Optional[torch.device] = None
|
| 27 |
+
):
|
| 28 |
+
super().__init__()
|
| 29 |
+
self.tokenizer = AutoTokenizer.from_pretrained(model_name, use_fast=True)
|
| 30 |
+
self.vocab_size = self.tokenizer.vocab_size
|
| 31 |
+
self.pad_id = self.tokenizer.pad_token_id
|
| 32 |
+
self.cls_id = self.tokenizer.cls_token_id
|
| 33 |
+
self.sep_id = self.tokenizer.sep_token_id
|
| 34 |
+
self.max_length = max_length
|
| 35 |
+
|
| 36 |
+
# Capa de embedding
|
| 37 |
+
self.embedding = nn.Embedding(self.vocab_size, emb_dim, padding_idx=self.pad_id)
|
| 38 |
+
nn.init.xavier_uniform_(self.embedding.weight)
|
| 39 |
+
with torch.no_grad():
|
| 40 |
+
if self.pad_id is not None:
|
| 41 |
+
self.embedding.weight[self.pad_id].zero_()
|
| 42 |
+
|
| 43 |
+
# Regularización opcional (ayuda contra sobreajuste)
|
| 44 |
+
self.emb_dropout = nn.Dropout(p=0.1)
|
| 45 |
+
|
| 46 |
+
self.device = device or torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 47 |
+
self.to(self.device)
|
| 48 |
+
|
| 49 |
+
def embed_batch(self, texts: List[str]) -> torch.Tensor:
|
| 50 |
+
batch = self.tokenizer(
|
| 51 |
+
texts, padding=True, truncation=True, max_length=self.max_length, return_tensors="pt"
|
| 52 |
+
)
|
| 53 |
+
input_ids = batch["input_ids"].to(self.device) # [B, T]
|
| 54 |
+
attention_mask = batch["attention_mask"].to(self.device) # [B, T]
|
| 55 |
+
|
| 56 |
+
embeds = self.embedding(input_ids) # [B, T, E]
|
| 57 |
+
if self.training:
|
| 58 |
+
embeds = self.emb_dropout(embeds)
|
| 59 |
+
|
| 60 |
+
mask = attention_mask.bool() # [B, T]
|
| 61 |
+
if self.cls_id is not None:
|
| 62 |
+
mask = mask & (input_ids != self.cls_id)
|
| 63 |
+
if self.sep_id is not None:
|
| 64 |
+
mask = mask & (input_ids != self.sep_id)
|
| 65 |
+
|
| 66 |
+
mask_f = mask.unsqueeze(-1).float() # [B, T, 1]
|
| 67 |
+
summed = (embeds * mask_f).sum(dim=1) # [B, E]
|
| 68 |
+
counts = mask_f.sum(dim=1).clamp(min=1.0) # [B, 1]
|
| 69 |
+
sentence_vecs = summed / counts # [B, E]
|
| 70 |
+
return sentence_vecs
|
| 71 |
+
|
| 72 |
+
def embed_sentence(self, text: str) -> torch.Tensor:
|
| 73 |
+
return self.embed_batch([text])[0]
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
# ==================== MÓDULO 1B: BETOEmbedder (encoder preentrenado) ====================
|
| 77 |
+
class BETOEmbedder(nn.Module):
|
| 78 |
+
"""
|
| 79 |
+
Usa el encoder de BETO (BERT en español) para obtener embeddings contextuales.
|
| 80 |
+
Mean pooling sobre last_hidden_state.
|
| 81 |
+
Salida: [B, 768]
|
| 82 |
+
"""
|
| 83 |
+
def __init__(
|
| 84 |
+
self,
|
| 85 |
+
model_name: str = "dccuchile/bert-base-spanish-wwm-cased",
|
| 86 |
+
max_length: int = 128,
|
| 87 |
+
device: Optional[torch.device] = None
|
| 88 |
+
):
|
| 89 |
+
super().__init__()
|
| 90 |
+
self.device = device or torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 91 |
+
self.tokenizer = AutoTokenizer.from_pretrained(model_name, use_fast=True)
|
| 92 |
+
self.encoder = AutoModel.from_pretrained(model_name)
|
| 93 |
+
self.max_length = max_length
|
| 94 |
+
self.encoder.to(self.device)
|
| 95 |
+
|
| 96 |
+
def embed_batch(self, texts: List[str]) -> torch.Tensor:
|
| 97 |
+
inputs = self.tokenizer(
|
| 98 |
+
texts, padding=True, truncation=True, max_length=self.max_length, return_tensors="pt"
|
| 99 |
+
).to(self.device)
|
| 100 |
+
outputs = self.encoder(**inputs) # last_hidden_state [B, T, 768]
|
| 101 |
+
last_hidden = outputs.last_hidden_state
|
| 102 |
+
mask = inputs["attention_mask"].unsqueeze(-1).float() # [B, T, 1]
|
| 103 |
+
pooled = (last_hidden * mask).sum(dim=1) / mask.sum(dim=1).clamp(min=1e-9) # [B, 768]
|
| 104 |
+
return pooled
|
| 105 |
+
|
| 106 |
+
|
| 107 |
+
# ==================== MÓDULO 2: MLP Classifier ====================
|
| 108 |
+
class MLPClassifier(nn.Module):
|
| 109 |
+
"""
|
| 110 |
+
Feedforward para clasificación de emociones:
|
| 111 |
+
Input → 128 → 64 → 6 (logits)
|
| 112 |
+
"""
|
| 113 |
+
def __init__(
|
| 114 |
+
self,
|
| 115 |
+
input_dim: int = 300,
|
| 116 |
+
hidden1: int = 128,
|
| 117 |
+
hidden2: int = 64,
|
| 118 |
+
num_classes: int = 6,
|
| 119 |
+
dropout: float = 0.3
|
| 120 |
+
):
|
| 121 |
+
super().__init__()
|
| 122 |
+
self.fc1 = nn.Linear(input_dim, hidden1)
|
| 123 |
+
self.relu1 = nn.ReLU()
|
| 124 |
+
self.dropout1 = nn.Dropout(dropout)
|
| 125 |
+
|
| 126 |
+
self.fc2 = nn.Linear(hidden1, hidden2)
|
| 127 |
+
self.relu2 = nn.ReLU()
|
| 128 |
+
self.dropout2 = nn.Dropout(dropout)
|
| 129 |
+
|
| 130 |
+
self.fc3 = nn.Linear(hidden2, num_classes)
|
| 131 |
+
|
| 132 |
+
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
| 133 |
+
x = self.fc1(x); x = self.relu1(x); x = self.dropout1(x)
|
| 134 |
+
x = self.fc2(x); x = self.relu2(x); x = self.dropout2(x)
|
| 135 |
+
x = self.fc3(x)
|
| 136 |
+
return x
|
| 137 |
+
|
| 138 |
+
|
| 139 |
+
# ==================== MÓDULO 3: Modelo Completo ====================
|
| 140 |
+
class EmotionClassifier(nn.Module):
|
| 141 |
+
"""
|
| 142 |
+
Integra embedder (aleatorio o BETO) + MLP.
|
| 143 |
+
- `pretrained_encoder=None` → usa TextEmbedder (emb_dim configurable)
|
| 144 |
+
- `pretrained_encoder="beto"` → usa BETOEmbedder (salida 768D)
|
| 145 |
+
"""
|
| 146 |
+
def __init__(
|
| 147 |
+
self,
|
| 148 |
+
model_name: str = "dccuchile/bert-base-spanish-wwm-cased",
|
| 149 |
+
emb_dim: int = 300,
|
| 150 |
+
max_length: int = 128,
|
| 151 |
+
hidden1: int = 128,
|
| 152 |
+
hidden2: int = 64,
|
| 153 |
+
num_classes: int = 6,
|
| 154 |
+
dropout: float = 0.3,
|
| 155 |
+
device: Optional[torch.device] = None,
|
| 156 |
+
pretrained_encoder: Optional[str] = None
|
| 157 |
+
):
|
| 158 |
+
super().__init__()
|
| 159 |
+
self.device = device or torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 160 |
+
|
| 161 |
+
if pretrained_encoder == "beto":
|
| 162 |
+
self.embedder = BETOEmbedder(model_name=model_name, max_length=max_length, device=self.device)
|
| 163 |
+
embed_dim = 768
|
| 164 |
+
else:
|
| 165 |
+
self.embedder = TextEmbedder(model_name=model_name, emb_dim=emb_dim, max_length=max_length, device=self.device)
|
| 166 |
+
embed_dim = emb_dim
|
| 167 |
+
|
| 168 |
+
self.classifier = MLPClassifier(
|
| 169 |
+
input_dim=embed_dim, hidden1=hidden1, hidden2=hidden2, num_classes=num_classes, dropout=dropout
|
| 170 |
+
)
|
| 171 |
+
|
| 172 |
+
self.label_map = {0: "tristeza", 1: "alegría", 2: "amor", 3: "ira", 4: "miedo", 5: "sorpresa"}
|
| 173 |
+
|
| 174 |
+
self.to(self.device)
|
| 175 |
+
|
| 176 |
+
# ---------- Forward & Utils ----------
|
| 177 |
+
def forward(self, texts: List[str]) -> torch.Tensor:
|
| 178 |
+
embeddings = self.embedder.embed_batch(texts) # [B, D]
|
| 179 |
+
logits = self.classifier(embeddings) # [B, C]
|
| 180 |
+
return logits
|
| 181 |
+
|
| 182 |
+
def predict(self, texts: List[str], return_probs: bool = False):
|
| 183 |
+
self.eval()
|
| 184 |
+
with torch.no_grad():
|
| 185 |
+
logits = self.forward(texts)
|
| 186 |
+
probs = torch.softmax(logits, dim=-1)
|
| 187 |
+
predictions = torch.argmax(probs, dim=-1)
|
| 188 |
+
emotions = [self.label_map[p.item()] for p in predictions]
|
| 189 |
+
if return_probs:
|
| 190 |
+
return emotions, probs.cpu().numpy()
|
| 191 |
+
return emotions
|
| 192 |
+
|
| 193 |
+
def predict_single(self, text: str, return_probs: bool = False):
|
| 194 |
+
out = self.predict([text], return_probs=return_probs)
|
| 195 |
+
if return_probs:
|
| 196 |
+
emotions, probs = out
|
| 197 |
+
return emotions[0], probs[0]
|
| 198 |
+
return out[0]
|
| 199 |
+
|
| 200 |
+
# ---------- Fine-tuning helpers ----------
|
| 201 |
+
def freeze_encoder(self):
|
| 202 |
+
for p in self.embedder.parameters():
|
| 203 |
+
p.requires_grad = False
|
| 204 |
+
|
| 205 |
+
def unfreeze_encoder(self):
|
| 206 |
+
for p in self.embedder.parameters():
|
| 207 |
+
p.requires_grad = True
|
inference_agent_emotion_classifier.py
ADDED
|
@@ -0,0 +1,278 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
"""
|
| 3 |
+
Inferencia para el AgentEmotionPredictClassifier (MIA · segunda red)
|
| 4 |
+
- Busca 'best_model.pt' y 'config_agent.json' en local; si no están y hay
|
| 5 |
+
huggingface_hub instalado, los descarga del repo indicado.
|
| 6 |
+
- La config DEBE incluir, como mínimo:
|
| 7 |
+
{
|
| 8 |
+
"base_model_id": "dccuchile/bert-base-spanish-wwm-cased",
|
| 9 |
+
"max_length": 128,
|
| 10 |
+
"hidden1": 256,
|
| 11 |
+
"hidden2": 64,
|
| 12 |
+
"num_classes": 2,
|
| 13 |
+
"dropout": 0.4,
|
| 14 |
+
"label_feature_dropout": 0.5,
|
| 15 |
+
"pretrained_encoder": "beto",
|
| 16 |
+
"present_classes": [1, 2], # ids originales (0..5) presentes en train
|
| 17 |
+
"class_names": ["alegría","amor"] # nombres en el mismo orden del mapeo 0..K-1
|
| 18 |
+
}
|
| 19 |
+
|
| 20 |
+
- Uso:
|
| 21 |
+
from inference_agent_emotion import predict
|
| 22 |
+
y = predict("No me siento bien", user_label=0) # 0..5 (tristeza..sorpresa)
|
| 23 |
+
"""
|
| 24 |
+
|
| 25 |
+
from __future__ import annotations
|
| 26 |
+
import os
|
| 27 |
+
import json
|
| 28 |
+
from pathlib import Path
|
| 29 |
+
from typing import Any, Dict, List, Tuple, Optional, Union
|
| 30 |
+
|
| 31 |
+
import torch
|
| 32 |
+
|
| 33 |
+
# Opcional: descarga desde HF si no hay archivos locales
|
| 34 |
+
try:
|
| 35 |
+
from huggingface_hub import hf_hub_download
|
| 36 |
+
except Exception:
|
| 37 |
+
hf_hub_download = None
|
| 38 |
+
|
| 39 |
+
from agent_emotion_predict_classifier import AgentEmotionPredictClassifier
|
| 40 |
+
|
| 41 |
+
# ---------------- Config ----------------
|
| 42 |
+
REPO_ID = "RustyLinux/MiaPredict" # cambia por tu repo si usas el Hub
|
| 43 |
+
|
| 44 |
+
LOCAL_CKPT = Path("best_model_agent.pt") # checkpoint de la segunda red
|
| 45 |
+
LOCAL_CFG = Path("config_agent.json") # config de la segunda red
|
| 46 |
+
|
| 47 |
+
# Mapa global de emociones (usuario y también nombres canónicos)
|
| 48 |
+
EMOTION_ID2NAME = {
|
| 49 |
+
0: "tristeza",
|
| 50 |
+
1: "alegría",
|
| 51 |
+
2: "amor",
|
| 52 |
+
3: "ira",
|
| 53 |
+
4: "miedo",
|
| 54 |
+
5: "sorpresa",
|
| 55 |
+
}
|
| 56 |
+
EMOTION_NAME2ID = {v: k for k, v in EMOTION_ID2NAME.items()}
|
| 57 |
+
|
| 58 |
+
_device: torch.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 59 |
+
_model: Optional[AgentEmotionPredictClassifier] = None
|
| 60 |
+
_cfg: Optional[Dict[str, Any]] = None
|
| 61 |
+
_label_map_fwd: Optional[Dict[int, int]] = None # original_id -> idx(0..K-1) usado en entrenamiento
|
| 62 |
+
_label_map_inv: Optional[Dict[int, int]] = None # idx(0..K-1) -> original_id (para devolver nombre global)
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
# ---------------- Utilidades internas ----------------
|
| 66 |
+
def _resolve_paths() -> Tuple[str, str]:
|
| 67 |
+
"""
|
| 68 |
+
Retorna (ckpt_path, cfg_path). Prefiere local; si no, intenta descarga HF.
|
| 69 |
+
"""
|
| 70 |
+
if LOCAL_CKPT.exists() and LOCAL_CFG.exists():
|
| 71 |
+
print("✅ Cargando archivos desde local.")
|
| 72 |
+
return str(LOCAL_CKPT.resolve()), str(LOCAL_CFG.resolve())
|
| 73 |
+
|
| 74 |
+
if hf_hub_download is None:
|
| 75 |
+
raise RuntimeError(
|
| 76 |
+
"No se encontraron 'best_model_agent.pt' y 'config_agent.json' en local, "
|
| 77 |
+
"y 'huggingface_hub' no está instalado para descargarlos."
|
| 78 |
+
)
|
| 79 |
+
|
| 80 |
+
print("⬇️ Descargando archivos desde Hugging Face Hub...")
|
| 81 |
+
ckpt_path = hf_hub_download(repo_id=REPO_ID, filename="best_model.pt")
|
| 82 |
+
cfg_path = hf_hub_download(repo_id=REPO_ID, filename="config_agent.json")
|
| 83 |
+
return ckpt_path, cfg_path
|
| 84 |
+
|
| 85 |
+
|
| 86 |
+
def _prepare_label_maps(cfg: Dict[str, Any]) -> Tuple[Dict[int, int], Dict[int, int]]:
|
| 87 |
+
"""
|
| 88 |
+
Construye los mapeos entre ids originales (0..5) y los índices 0..K-1 usados por la head.
|
| 89 |
+
"""
|
| 90 |
+
present = cfg.get("present_classes", None)
|
| 91 |
+
if not present:
|
| 92 |
+
# Por compatibilidad: si no viene, asumimos [0..num_classes-1], pero se recomienda guardarlo.
|
| 93 |
+
k = int(cfg.get("num_classes", 2))
|
| 94 |
+
present = list(range(k))
|
| 95 |
+
present = list(sorted(int(x) for x in present))
|
| 96 |
+
fwd = {orig: i for i, orig in enumerate(present)}
|
| 97 |
+
inv = {i: orig for orig, i in fwd.items()}
|
| 98 |
+
return fwd, inv
|
| 99 |
+
|
| 100 |
+
|
| 101 |
+
def _load_config(cfg_path: str) -> Dict[str, Any]:
|
| 102 |
+
global _cfg, _label_map_fwd, _label_map_inv
|
| 103 |
+
if _cfg is not None:
|
| 104 |
+
return _cfg
|
| 105 |
+
with open(cfg_path, "r", encoding="utf-8") as f:
|
| 106 |
+
_cfg = json.load(f)
|
| 107 |
+
_label_map_fwd, _label_map_inv = _prepare_label_maps(_cfg)
|
| 108 |
+
return _cfg
|
| 109 |
+
|
| 110 |
+
|
| 111 |
+
def _build_model(cfg: Dict[str, Any]) -> AgentEmotionPredictClassifier:
|
| 112 |
+
model = AgentEmotionPredictClassifier(
|
| 113 |
+
model_name=cfg.get("base_model_id", "dccuchile/bert-base-spanish-wwm-cased"),
|
| 114 |
+
pretrained_encoder=cfg.get("pretrained_encoder", "beto"),
|
| 115 |
+
emb_dim=cfg.get("emb_dim", 300),
|
| 116 |
+
max_length=cfg.get("max_length", 128),
|
| 117 |
+
hidden1=cfg.get("hidden1", 256),
|
| 118 |
+
hidden2=cfg.get("hidden2", 64),
|
| 119 |
+
num_classes=cfg.get("num_classes", 2),
|
| 120 |
+
dropout=cfg.get("dropout", 0.4),
|
| 121 |
+
label_feature_dropout=cfg.get("label_feature_dropout", 0.0), # en inferencia no se usa
|
| 122 |
+
device=_device,
|
| 123 |
+
)
|
| 124 |
+
# aseguramos eval()
|
| 125 |
+
model.eval()
|
| 126 |
+
return model
|
| 127 |
+
|
| 128 |
+
|
| 129 |
+
def _load_model() -> AgentEmotionPredictClassifier:
|
| 130 |
+
global _model
|
| 131 |
+
if _model is not None:
|
| 132 |
+
return _model
|
| 133 |
+
|
| 134 |
+
ckpt_path, cfg_path = _resolve_paths()
|
| 135 |
+
cfg = _load_config(cfg_path)
|
| 136 |
+
|
| 137 |
+
model = _build_model(cfg)
|
| 138 |
+
|
| 139 |
+
state = torch.load(ckpt_path, map_location=_device)
|
| 140 |
+
if isinstance(state, dict) and "model_state_dict" in state:
|
| 141 |
+
model.load_state_dict(state["model_state_dict"])
|
| 142 |
+
else:
|
| 143 |
+
model.load_state_dict(state)
|
| 144 |
+
|
| 145 |
+
model.eval()
|
| 146 |
+
_model = model
|
| 147 |
+
print(f"✅ Modelo cargado en {_device} | num_classes={cfg.get('num_classes')} | "
|
| 148 |
+
f"present_classes={cfg.get('present_classes')}")
|
| 149 |
+
return _model
|
| 150 |
+
|
| 151 |
+
|
| 152 |
+
def _coerce_user_label(label: Union[int, str]) -> int:
|
| 153 |
+
"""
|
| 154 |
+
Convierte un label de usuario a id 0..5.
|
| 155 |
+
- Si llega string ("alegría"), lo mapea.
|
| 156 |
+
- Valida rango si llega int.
|
| 157 |
+
"""
|
| 158 |
+
if isinstance(label, str):
|
| 159 |
+
label = label.strip().lower()
|
| 160 |
+
if label not in EMOTION_NAME2ID:
|
| 161 |
+
raise ValueError(f"Label de usuario desconocido: {label}. Esperado uno de {list(EMOTION_NAME2ID.keys())}")
|
| 162 |
+
return EMOTION_NAME2ID[label]
|
| 163 |
+
if isinstance(label, int):
|
| 164 |
+
if label < 0 or label > 5:
|
| 165 |
+
raise ValueError("El user_label debe estar en 0..5.")
|
| 166 |
+
return label
|
| 167 |
+
raise TypeError("user_label debe ser int (0..5) o str (nombre de emoción).")
|
| 168 |
+
|
| 169 |
+
|
| 170 |
+
def _map_agent_idx_to_original(idx: int) -> int:
|
| 171 |
+
"""
|
| 172 |
+
Convierte el índice 0..K-1 (head) al id original 0..5 para reportar el nombre global.
|
| 173 |
+
"""
|
| 174 |
+
if _label_map_inv is None:
|
| 175 |
+
raise RuntimeError("Mapeos de etiquetas no inicializados.")
|
| 176 |
+
return _label_map_inv[int(idx)]
|
| 177 |
+
|
| 178 |
+
|
| 179 |
+
def _agent_class_names() -> List[str]:
|
| 180 |
+
"""
|
| 181 |
+
Nombres de clases del agente en el mismo orden que la head (0..K-1).
|
| 182 |
+
"""
|
| 183 |
+
if _cfg is None:
|
| 184 |
+
raise RuntimeError("Config no cargada.")
|
| 185 |
+
names = _cfg.get("class_names", None)
|
| 186 |
+
if names:
|
| 187 |
+
return list(names)
|
| 188 |
+
# fallback: usar nombres globales segun present_classes
|
| 189 |
+
present = sorted(_cfg.get("present_classes", []))
|
| 190 |
+
return [EMOTION_ID2NAME[p] for p in present]
|
| 191 |
+
|
| 192 |
+
|
| 193 |
+
# ---------------- API de inferencia ----------------
|
| 194 |
+
@torch.inference_mode()
|
| 195 |
+
def predict(text: str, user_label: Union[int, str], return_probs: bool = False) -> Any:
|
| 196 |
+
"""
|
| 197 |
+
Predice la emoción CON LA QUE DEBE RESPONDER EL AGENTE.
|
| 198 |
+
Args:
|
| 199 |
+
text: str
|
| 200 |
+
user_label: int(0..5) o nombre ("tristeza", "alegría", "amor", "ira", "miedo", "sorpresa")
|
| 201 |
+
return_probs: si True devuelve (pred_name, probs_dict)
|
| 202 |
+
|
| 203 |
+
Returns:
|
| 204 |
+
- Si return_probs=False: str con el nombre de la emoción objetivo del agente (en nombres globales 0..5).
|
| 205 |
+
- Si return_probs=True: (pred_name:str, probs:Dict[str,float]) usando los nombres en orden de la head.
|
| 206 |
+
"""
|
| 207 |
+
model = _load_model()
|
| 208 |
+
cfg = _cfg # ya cargada
|
| 209 |
+
assert cfg is not None
|
| 210 |
+
|
| 211 |
+
# 1) preparar entrada
|
| 212 |
+
u = _coerce_user_label(user_label)
|
| 213 |
+
user_tensor = torch.tensor([u], dtype=torch.long, device=_device)
|
| 214 |
+
texts = [text]
|
| 215 |
+
|
| 216 |
+
# 2) forward
|
| 217 |
+
logits = model(texts, user_tensor) # [1, K]
|
| 218 |
+
probs = torch.softmax(logits, dim=-1).cpu().numpy()[0]
|
| 219 |
+
pred_idx = int(probs.argmax())
|
| 220 |
+
|
| 221 |
+
# 3) mapear idx(0..K-1) -> id original (0..5) y nombre canónico
|
| 222 |
+
orig_id = _map_agent_idx_to_original(pred_idx)
|
| 223 |
+
pred_name = EMOTION_ID2NAME[orig_id]
|
| 224 |
+
|
| 225 |
+
if not return_probs:
|
| 226 |
+
return pred_name
|
| 227 |
+
|
| 228 |
+
# nombres amistosos en el orden de la head
|
| 229 |
+
names_head = _agent_class_names()
|
| 230 |
+
probs_dict = {names_head[i]: float(probs[i]) for i in range(len(names_head))}
|
| 231 |
+
return pred_name, probs_dict
|
| 232 |
+
|
| 233 |
+
|
| 234 |
+
@torch.inference_mode()
|
| 235 |
+
def predict_batch(texts: List[str], user_labels: List[Union[int, str]], return_probs: bool = False):
|
| 236 |
+
"""
|
| 237 |
+
Batch de inferencia.
|
| 238 |
+
- user_labels: lista paralela a texts con ids (0..5) o nombres de emoción.
|
| 239 |
+
"""
|
| 240 |
+
if len(texts) != len(user_labels):
|
| 241 |
+
raise ValueError("texts y user_labels deben tener la misma longitud.")
|
| 242 |
+
model = _load_model()
|
| 243 |
+
|
| 244 |
+
# preparar
|
| 245 |
+
u_ids = [ _coerce_user_label(u) for u in user_labels ]
|
| 246 |
+
user_tensor = torch.tensor(u_ids, dtype=torch.long, device=_device)
|
| 247 |
+
|
| 248 |
+
logits = model(texts, user_tensor) # [B, K]
|
| 249 |
+
probs = torch.softmax(logits, dim=-1).cpu().numpy()
|
| 250 |
+
pred_idxs = probs.argmax(axis=1)
|
| 251 |
+
|
| 252 |
+
results = []
|
| 253 |
+
names_head = _agent_class_names()
|
| 254 |
+
|
| 255 |
+
for i, idx in enumerate(pred_idxs):
|
| 256 |
+
orig_id = _map_agent_idx_to_original(int(idx))
|
| 257 |
+
pred_name = EMOTION_ID2NAME[orig_id]
|
| 258 |
+
if return_probs:
|
| 259 |
+
pvec = probs[i]
|
| 260 |
+
probs_dict = {names_head[j]: float(pvec[j]) for j in range(len(names_head))}
|
| 261 |
+
results.append((pred_name, probs_dict))
|
| 262 |
+
else:
|
| 263 |
+
results.append(pred_name)
|
| 264 |
+
return results
|
| 265 |
+
|
| 266 |
+
|
| 267 |
+
# ---------------- CLI rápido ----------------
|
| 268 |
+
if __name__ == "__main__":
|
| 269 |
+
# Ejemplos rápidos
|
| 270 |
+
txts = [
|
| 271 |
+
"Tuve ese tipo de sentimiento pero lo ignoré",
|
| 272 |
+
"Estoy muy feliz con la noticia",
|
| 273 |
+
"Me molesta lo que pasó",
|
| 274 |
+
]
|
| 275 |
+
# user_label puede ser int o str
|
| 276 |
+
for t, ulab in zip(txts, [0, "alegría", "ira"]):
|
| 277 |
+
out = predict(t, user_label=ulab, return_probs=True)
|
| 278 |
+
print(f"\nTexto: {t}\nUser label: {ulab}\nPredicción agente: {out[0]}\nProbs: {out[1]}")
|
requirements.txt
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
aiohappyeyeballs==2.6.1
|
| 2 |
+
aiohttp==3.12.15
|
| 3 |
+
aiosignal==1.4.0
|
| 4 |
+
anyio==4.10.0
|
| 5 |
+
attrs==25.3.0
|
| 6 |
+
beautifulsoup4==4.13.5
|
| 7 |
+
certifi==2025.8.3
|
| 8 |
+
charset-normalizer==3.4.3
|
| 9 |
+
contourpy==1.3.3
|
| 10 |
+
cycler==0.12.1
|
| 11 |
+
datasets==4.1.1
|
| 12 |
+
deep-translator==1.11.4
|
| 13 |
+
dill==0.4.0
|
| 14 |
+
filelock==3.19.1
|
| 15 |
+
fonttools==4.60.1
|
| 16 |
+
frozenlist==1.7.0
|
| 17 |
+
fsspec==2025.9.0
|
| 18 |
+
googletrans==4.0.2
|
| 19 |
+
h11==0.16.0
|
| 20 |
+
h2==4.3.0
|
| 21 |
+
hf-xet==1.1.10
|
| 22 |
+
hpack==4.1.0
|
| 23 |
+
httpcore==1.0.9
|
| 24 |
+
httpx==0.28.1
|
| 25 |
+
huggingface-hub==0.35.0
|
| 26 |
+
hyperframe==6.1.0
|
| 27 |
+
idna==3.10
|
| 28 |
+
ijson==3.4.0
|
| 29 |
+
Jinja2==3.1.6
|
| 30 |
+
joblib==1.5.2
|
| 31 |
+
kiwisolver==1.4.9
|
| 32 |
+
MarkupSafe==3.0.3
|
| 33 |
+
matplotlib==3.10.7
|
| 34 |
+
mpmath==1.3.0
|
| 35 |
+
multidict==6.6.4
|
| 36 |
+
multiprocess==0.70.16
|
| 37 |
+
networkx==3.5
|
| 38 |
+
numpy==2.3.3
|
| 39 |
+
nvidia-cublas-cu12==12.8.4.1
|
| 40 |
+
nvidia-cuda-cupti-cu12==12.8.90
|
| 41 |
+
nvidia-cuda-nvrtc-cu12==12.8.93
|
| 42 |
+
nvidia-cuda-runtime-cu12==12.8.90
|
| 43 |
+
nvidia-cudnn-cu12==9.10.2.21
|
| 44 |
+
nvidia-cufft-cu12==11.3.3.83
|
| 45 |
+
nvidia-cufile-cu12==1.13.1.3
|
| 46 |
+
nvidia-curand-cu12==10.3.9.90
|
| 47 |
+
nvidia-cusolver-cu12==11.7.3.90
|
| 48 |
+
nvidia-cusparse-cu12==12.5.8.93
|
| 49 |
+
nvidia-cusparselt-cu12==0.7.1
|
| 50 |
+
nvidia-nccl-cu12==2.27.5
|
| 51 |
+
nvidia-nvjitlink-cu12==12.8.93
|
| 52 |
+
nvidia-nvshmem-cu12==3.3.20
|
| 53 |
+
nvidia-nvtx-cu12==12.8.90
|
| 54 |
+
packaging==25.0
|
| 55 |
+
pandas==2.3.2
|
| 56 |
+
pillow==12.0.0
|
| 57 |
+
propcache==0.3.2
|
| 58 |
+
protobuf==6.33.0
|
| 59 |
+
pyarrow==21.0.0
|
| 60 |
+
pyparsing==3.2.5
|
| 61 |
+
python-dateutil==2.9.0.post0
|
| 62 |
+
pytz==2025.2
|
| 63 |
+
PyYAML==6.0.2
|
| 64 |
+
regex==2025.10.23
|
| 65 |
+
requests==2.32.5
|
| 66 |
+
safetensors==0.6.2
|
| 67 |
+
scikit-learn==1.7.2
|
| 68 |
+
scipy==1.16.2
|
| 69 |
+
seaborn==0.13.2
|
| 70 |
+
setuptools==80.9.0
|
| 71 |
+
six==1.17.0
|
| 72 |
+
sniffio==1.3.1
|
| 73 |
+
soupsieve==2.8
|
| 74 |
+
sympy==1.14.0
|
| 75 |
+
threadpoolctl==3.6.0
|
| 76 |
+
tiktoken==0.12.0
|
| 77 |
+
tokenizers==0.22.1
|
| 78 |
+
torch==2.9.0
|
| 79 |
+
tqdm==4.67.1
|
| 80 |
+
transformers==4.57.1
|
| 81 |
+
triton==3.5.0
|
| 82 |
+
typing_extensions==4.15.0
|
| 83 |
+
tzdata==2025.2
|
| 84 |
+
urllib3==2.5.0
|
| 85 |
+
xxhash==3.5.0
|
| 86 |
+
yarl==1.20.1
|
train_agent_emotion_classifier.py
ADDED
|
@@ -0,0 +1,440 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
=== MIA · Script de Entrenamiento del AgentEmotionPredictClassifier (v2) ===
|
| 3 |
+
- Lee JSON v2 con `label` (usuario) y `label_agent` (objetivo del agente).
|
| 4 |
+
- Soporta encoder preentrenado (BETO) con fine-tuning controlado.
|
| 5 |
+
- Class weights + label smoothing para desbalance.
|
| 6 |
+
- Early stopping por macro-F1.
|
| 7 |
+
- AdamW con dos grupos de LR (encoder vs head) + scheduler lineal con warmup.
|
| 8 |
+
- Clip de gradientes, dump de misclasificados, matrices de confusión (absoluta y normalizada).
|
| 9 |
+
|
| 10 |
+
Requisitos:
|
| 11 |
+
pip install transformers scikit-learn seaborn matplotlib tqdm
|
| 12 |
+
"""
|
| 13 |
+
|
| 14 |
+
import json
|
| 15 |
+
from pathlib import Path
|
| 16 |
+
from typing import Dict, List, Tuple, Optional
|
| 17 |
+
|
| 18 |
+
import numpy as np
|
| 19 |
+
import torch
|
| 20 |
+
import torch.nn as nn
|
| 21 |
+
from torch.utils.data import Dataset, DataLoader
|
| 22 |
+
from sklearn.metrics import classification_report, confusion_matrix, f1_score
|
| 23 |
+
from tqdm import tqdm
|
| 24 |
+
import matplotlib.pyplot as plt
|
| 25 |
+
import seaborn as sns
|
| 26 |
+
|
| 27 |
+
from agent_emotion_predict_classifier import AgentEmotionPredictClassifier
|
| 28 |
+
|
| 29 |
+
# ==================== DATASET ====================
|
| 30 |
+
class AgentEmotionDataset(Dataset):
|
| 31 |
+
def __init__(self, data_path: str, label_map: Optional[dict] = None):
|
| 32 |
+
with open(data_path, 'r', encoding='utf-8') as f:
|
| 33 |
+
raw = json.load(f)
|
| 34 |
+
data = raw.get('data', raw)
|
| 35 |
+
self.texts: List[str] = []
|
| 36 |
+
self.user_labels: List[int] = []
|
| 37 |
+
self.agent_labels: List[int] = []
|
| 38 |
+
for it in data:
|
| 39 |
+
self.texts.append(it['text'])
|
| 40 |
+
u = it['label']; a = it['label_agent']
|
| 41 |
+
u = int(u) if isinstance(u, str) else u
|
| 42 |
+
a = int(a) if isinstance(a, str) else a
|
| 43 |
+
if label_map is not None:
|
| 44 |
+
a = label_map[a] # remapea a {0..K-1}
|
| 45 |
+
self.user_labels.append(u)
|
| 46 |
+
self.agent_labels.append(a)
|
| 47 |
+
|
| 48 |
+
def __len__(self):
|
| 49 |
+
return len(self.texts)
|
| 50 |
+
|
| 51 |
+
def __getitem__(self, idx):
|
| 52 |
+
return self.texts[idx], self.user_labels[idx], self.agent_labels[idx]
|
| 53 |
+
|
| 54 |
+
def collate_fn(batch):
|
| 55 |
+
texts, ulabels, alabels = zip(*batch)
|
| 56 |
+
ulabels = torch.tensor(ulabels, dtype=torch.long)
|
| 57 |
+
alabels = torch.tensor(alabels, dtype=torch.long)
|
| 58 |
+
return list(texts), ulabels, alabels
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
# ==================== TRAINER ====================
|
| 62 |
+
class AgentEmotionTrainer:
|
| 63 |
+
def __init__(
|
| 64 |
+
self,
|
| 65 |
+
model: AgentEmotionPredictClassifier,
|
| 66 |
+
train_loader: DataLoader,
|
| 67 |
+
val_loader: DataLoader,
|
| 68 |
+
test_loader: DataLoader,
|
| 69 |
+
device: Optional[torch.device] = None,
|
| 70 |
+
lr_encoder: float = 2e-5,
|
| 71 |
+
lr_head: float = 1e-3,
|
| 72 |
+
weight_decay: float = 0.01,
|
| 73 |
+
num_epochs: int = 20,
|
| 74 |
+
warmup_ratio: float = 0.1,
|
| 75 |
+
early_stopping_patience: int = 3,
|
| 76 |
+
warmup_freeze_epochs: int = 2,
|
| 77 |
+
num_classes: int = 2,
|
| 78 |
+
class_names: Optional[List[str]] = None,
|
| 79 |
+
):
|
| 80 |
+
self.model = model
|
| 81 |
+
self.train_loader = train_loader
|
| 82 |
+
self.val_loader = val_loader
|
| 83 |
+
self.test_loader = test_loader
|
| 84 |
+
self.device = device or torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 85 |
+
self.num_classes = num_classes
|
| 86 |
+
self.class_names = class_names or ["alegría","amor"]
|
| 87 |
+
|
| 88 |
+
# ---------- Pérdida con pesos de clase + label smoothing ----------
|
| 89 |
+
labels_tensor = torch.tensor(train_loader.dataset.agent_labels)
|
| 90 |
+
class_counts = torch.bincount(labels_tensor, minlength=self.num_classes).float()
|
| 91 |
+
safe_counts = class_counts.clamp(min=1.0)
|
| 92 |
+
inv_freq = (safe_counts.sum() / (self.num_classes * safe_counts)).to(self.device)
|
| 93 |
+
class_weights = inv_freq / inv_freq.mean()
|
| 94 |
+
self.criterion = nn.CrossEntropyLoss(weight=class_weights, label_smoothing=0.05)
|
| 95 |
+
|
| 96 |
+
# ---------- Optimizador AdamW con 2 grupos (encoder vs head) ----------
|
| 97 |
+
self.lr_encoder = lr_encoder
|
| 98 |
+
self.lr_head = lr_head
|
| 99 |
+
self.weight_decay = weight_decay
|
| 100 |
+
|
| 101 |
+
# Warmup: encoder congelado n épocas
|
| 102 |
+
self.warmup_freeze_epochs = warmup_freeze_epochs
|
| 103 |
+
self.model.freeze_encoder()
|
| 104 |
+
|
| 105 |
+
self.optimizer = self._build_optimizer()
|
| 106 |
+
|
| 107 |
+
# ---------- Scheduler lineal con warmup ----------
|
| 108 |
+
self.num_epochs = num_epochs
|
| 109 |
+
total_steps = len(self.train_loader) * self.num_epochs
|
| 110 |
+
from transformers import get_linear_schedule_with_warmup
|
| 111 |
+
self.scheduler = get_linear_schedule_with_warmup(
|
| 112 |
+
self.optimizer,
|
| 113 |
+
num_warmup_steps=int(warmup_ratio * total_steps),
|
| 114 |
+
num_training_steps=total_steps,
|
| 115 |
+
)
|
| 116 |
+
|
| 117 |
+
# ---------- Tracking ----------
|
| 118 |
+
self.train_losses, self.val_losses = [], []
|
| 119 |
+
self.train_accs, self.val_accs = [], []
|
| 120 |
+
self.val_f1s = []
|
| 121 |
+
self.best_val_f1 = 0.0
|
| 122 |
+
self.best_state = None
|
| 123 |
+
self.early_stopping_patience = early_stopping_patience
|
| 124 |
+
|
| 125 |
+
def _build_optimizer(self):
|
| 126 |
+
encoder_params, head_params = [], []
|
| 127 |
+
for n, p in self.model.named_parameters():
|
| 128 |
+
if not p.requires_grad:
|
| 129 |
+
continue
|
| 130 |
+
if "embedder.encoder" in n: # parámetros del Transformer
|
| 131 |
+
encoder_params.append(p)
|
| 132 |
+
else:
|
| 133 |
+
head_params.append(p)
|
| 134 |
+
return torch.optim.AdamW(
|
| 135 |
+
[
|
| 136 |
+
{"params": encoder_params, "lr": self.lr_encoder, "weight_decay": self.weight_decay},
|
| 137 |
+
{"params": head_params, "lr": self.lr_head, "weight_decay": 0.0},
|
| 138 |
+
]
|
| 139 |
+
)
|
| 140 |
+
|
| 141 |
+
def train_epoch(self) -> Tuple[float, float]:
|
| 142 |
+
self.model.train()
|
| 143 |
+
total_loss, correct, total = 0.0, 0, 0
|
| 144 |
+
pbar = tqdm(self.train_loader, desc="Training")
|
| 145 |
+
for texts, ulabels, alabels in pbar:
|
| 146 |
+
alabels = alabels.to(self.device)
|
| 147 |
+
|
| 148 |
+
self.optimizer.zero_grad()
|
| 149 |
+
logits = self.model(texts, ulabels.to(self.device))
|
| 150 |
+
loss = self.criterion(logits, alabels)
|
| 151 |
+
|
| 152 |
+
loss.backward()
|
| 153 |
+
torch.nn.utils.clip_grad_norm_(self.model.parameters(), max_norm=1.0)
|
| 154 |
+
self.optimizer.step()
|
| 155 |
+
if self.scheduler is not None:
|
| 156 |
+
self.scheduler.step()
|
| 157 |
+
|
| 158 |
+
total_loss += loss.item()
|
| 159 |
+
preds = torch.argmax(logits, dim=-1)
|
| 160 |
+
correct += (preds == alabels).sum().item()
|
| 161 |
+
total += alabels.size(0)
|
| 162 |
+
pbar.set_postfix({'loss': f'{loss.item():.4f}', 'acc': f'{100*correct/total:.2f}%'})
|
| 163 |
+
|
| 164 |
+
avg_loss = total_loss / len(self.train_loader)
|
| 165 |
+
accuracy = 100 * correct / total
|
| 166 |
+
return avg_loss, accuracy
|
| 167 |
+
|
| 168 |
+
def validate(self) -> Tuple[float, float, float]:
|
| 169 |
+
self.model.eval()
|
| 170 |
+
total_loss, correct, total = 0.0, 0, 0
|
| 171 |
+
all_preds, all_labels = [], []
|
| 172 |
+
with torch.no_grad():
|
| 173 |
+
for texts, ulabels, alabels in tqdm(self.val_loader, desc="Validation"):
|
| 174 |
+
alabels = alabels.to(self.device)
|
| 175 |
+
logits = self.model(texts, ulabels.to(self.device))
|
| 176 |
+
loss = self.criterion(logits, alabels)
|
| 177 |
+
|
| 178 |
+
total_loss += loss.item()
|
| 179 |
+
preds = torch.argmax(logits, dim=-1)
|
| 180 |
+
correct += (preds == alabels).sum().item()
|
| 181 |
+
total += alabels.size(0)
|
| 182 |
+
|
| 183 |
+
all_preds.extend(preds.cpu().tolist())
|
| 184 |
+
all_labels.extend(alabels.cpu().tolist())
|
| 185 |
+
|
| 186 |
+
avg_loss = total_loss / len(self.val_loader)
|
| 187 |
+
accuracy = 100 * correct / total
|
| 188 |
+
macro_f1 = f1_score(all_labels, all_preds, average='macro')
|
| 189 |
+
return avg_loss, accuracy, macro_f1
|
| 190 |
+
|
| 191 |
+
def test(self, save_dir: Path) -> Dict:
|
| 192 |
+
self.model.eval()
|
| 193 |
+
all_predictions, all_labels, all_texts = [], [], []
|
| 194 |
+
with torch.no_grad():
|
| 195 |
+
for texts, ulabels, alabels in tqdm(self.test_loader, desc="Testing"):
|
| 196 |
+
logits = self.model(texts, ulabels.to(self.device))
|
| 197 |
+
predictions = torch.argmax(logits, dim=-1)
|
| 198 |
+
all_predictions.extend(predictions.cpu().numpy())
|
| 199 |
+
all_labels.extend(alabels.numpy())
|
| 200 |
+
all_texts.extend(texts)
|
| 201 |
+
|
| 202 |
+
accuracy = 100 * np.mean(np.array(all_predictions) == np.array(all_labels))
|
| 203 |
+
labels_ord = list(range(self.num_classes))
|
| 204 |
+
target_names = self.class_names
|
| 205 |
+
|
| 206 |
+
report = classification_report(
|
| 207 |
+
all_labels,
|
| 208 |
+
all_predictions,
|
| 209 |
+
labels=labels_ord,
|
| 210 |
+
target_names=target_names,
|
| 211 |
+
output_dict=True,
|
| 212 |
+
zero_division=0
|
| 213 |
+
)
|
| 214 |
+
cm_abs = confusion_matrix(all_labels, all_predictions, labels=labels_ord)
|
| 215 |
+
cm_norm = confusion_matrix(all_labels, all_predictions, labels=labels_ord, normalize='true')
|
| 216 |
+
|
| 217 |
+
self._dump_misclassified(all_texts, all_labels, all_predictions, target_names, save_dir / "misclassified.txt")
|
| 218 |
+
return {
|
| 219 |
+
'accuracy': accuracy,
|
| 220 |
+
'classification_report': report,
|
| 221 |
+
'confusion_matrix_abs': cm_abs,
|
| 222 |
+
'confusion_matrix_norm': cm_norm,
|
| 223 |
+
}
|
| 224 |
+
|
| 225 |
+
@staticmethod
|
| 226 |
+
def _dump_misclassified(texts, y_true, y_pred, names, path: Path):
|
| 227 |
+
with open(path, "w", encoding="utf-8") as f:
|
| 228 |
+
for t, yt, yp in zip(texts, y_true, y_pred):
|
| 229 |
+
if yt != yp:
|
| 230 |
+
f.write(f"[gold={names[yt]} | pred={names[yp]}] {t}\n")
|
| 231 |
+
|
| 232 |
+
def train(self, num_epochs: Optional[int] = None, early_stopping_patience: Optional[int] = None, save_dir: str = "models/agent_emotion"):
|
| 233 |
+
num_epochs = num_epochs or self.num_epochs
|
| 234 |
+
if early_stopping_patience is not None:
|
| 235 |
+
self.early_stopping_patience = early_stopping_patience
|
| 236 |
+
|
| 237 |
+
print(f"\n{'='*60}\nIniciando entrenamiento por {num_epochs} épocas\nDevice: {self.device}\n{'='*60}")
|
| 238 |
+
patience_counter = 0
|
| 239 |
+
best_f1 = -1.0
|
| 240 |
+
|
| 241 |
+
for epoch in range(1, num_epochs + 1):
|
| 242 |
+
# Unfreeze encoder después del warmup
|
| 243 |
+
if epoch == self.warmup_freeze_epochs + 1:
|
| 244 |
+
print("→ Descongelando encoder para fine-tuning...")
|
| 245 |
+
self.model.unfreeze_encoder()
|
| 246 |
+
self.optimizer = self._build_optimizer()
|
| 247 |
+
|
| 248 |
+
print(f"\nÉpoca {epoch}/{num_epochs}\n" + '-'*60)
|
| 249 |
+
tr_loss, tr_acc = self.train_epoch()
|
| 250 |
+
self.train_losses.append(tr_loss); self.train_accs.append(tr_acc)
|
| 251 |
+
va_loss, va_acc, va_f1 = self.validate()
|
| 252 |
+
self.val_losses.append(va_loss); self.val_accs.append(va_acc); self.val_f1s.append(va_f1)
|
| 253 |
+
|
| 254 |
+
print(f"Train: loss={tr_loss:.4f} acc={tr_acc:.2f}% | Val: loss={va_loss:.4f} acc={va_acc:.2f}% f1m={va_f1:.4f}")
|
| 255 |
+
|
| 256 |
+
# Guardar mejor por Macro-F1
|
| 257 |
+
if va_f1 > best_f1 and best_f1 < 0.9999:
|
| 258 |
+
best_f1 = va_f1
|
| 259 |
+
patience_counter = 0
|
| 260 |
+
Path(save_dir).mkdir(parents=True, exist_ok=True)
|
| 261 |
+
torch.save({
|
| 262 |
+
'epoch': epoch,
|
| 263 |
+
'model_state_dict': self.model.state_dict(),
|
| 264 |
+
'optimizer_state_dict': self.optimizer.state_dict(),
|
| 265 |
+
'train_loss': tr_loss,
|
| 266 |
+
'val_loss': va_loss,
|
| 267 |
+
'val_acc': va_acc,
|
| 268 |
+
'val_f1': va_f1,
|
| 269 |
+
}, f"{save_dir}/best_model.pt")
|
| 270 |
+
print(f" ✓ Mejor modelo guardado (Val Macro-F1: {va_f1:.4f})")
|
| 271 |
+
else:
|
| 272 |
+
patience_counter += 1
|
| 273 |
+
print(f" No improvement. Patience: {patience_counter}/{self.early_stopping_patience}")
|
| 274 |
+
if patience_counter >= self.early_stopping_patience:
|
| 275 |
+
print(f"\nEarly stopping activado en época {epoch}")
|
| 276 |
+
break
|
| 277 |
+
|
| 278 |
+
print(f"\n{'='*60}\nEntrenamiento completado! Mejor Val Macro-F1: {best_f1:.4f}\n{'='*60}")
|
| 279 |
+
|
| 280 |
+
# Evaluar en test con mejor checkpoint
|
| 281 |
+
ckpt = torch.load(f"{save_dir}/best_model.pt", map_location=self.device)
|
| 282 |
+
self.model.load_state_dict(ckpt['model_state_dict'])
|
| 283 |
+
test_results = self.test(Path(save_dir))
|
| 284 |
+
|
| 285 |
+
print(f"\n{'='*60}\nRESULTADOS EN TEST SET\n{'='*60}")
|
| 286 |
+
print(f"Test Accuracy: {test_results['accuracy']:.2f}%\n")
|
| 287 |
+
|
| 288 |
+
# Graficar y guardar reportes
|
| 289 |
+
self.plot_training_history(save_dir)
|
| 290 |
+
self.plot_confusion_matrix(test_results['confusion_matrix_abs'], save_dir, norm=False)
|
| 291 |
+
self.plot_confusion_matrix(test_results['confusion_matrix_norm'], save_dir, norm=True)
|
| 292 |
+
self.save_classification_report(test_results['classification_report'], save_dir)
|
| 293 |
+
return test_results
|
| 294 |
+
|
| 295 |
+
# ---------- utilidades de guardado/plot ----------
|
| 296 |
+
def plot_training_history(self, save_dir: str):
|
| 297 |
+
plt.figure(figsize=(12, 5))
|
| 298 |
+
plt.subplot(1, 2, 1)
|
| 299 |
+
plt.plot(self.train_losses, label='Train Loss', marker='o')
|
| 300 |
+
plt.plot(self.val_losses, label='Val Loss', marker='s')
|
| 301 |
+
plt.xlabel('Época'); plt.ylabel('Loss'); plt.title('Loss'); plt.legend(); plt.grid(True, alpha=0.3)
|
| 302 |
+
plt.subplot(1, 2, 2)
|
| 303 |
+
plt.plot(self.train_accs, label='Train Acc', marker='o')
|
| 304 |
+
plt.plot(self.val_accs, label='Val Acc', marker='s')
|
| 305 |
+
plt.plot(self.val_f1s, label='Val Macro-F1', marker='^')
|
| 306 |
+
plt.xlabel('Época'); plt.ylabel('Score'); plt.title('Acc / Macro-F1'); plt.legend(); plt.grid(True, alpha=0.3)
|
| 307 |
+
plt.tight_layout(); Path(save_dir).mkdir(parents=True, exist_ok=True)
|
| 308 |
+
plt.savefig(f"{save_dir}/training_history.png", dpi=300, bbox_inches='tight'); plt.close()
|
| 309 |
+
print(f"✓ Gráfica de entrenamiento guardada en: {save_dir}/training_history.png")
|
| 310 |
+
|
| 311 |
+
def plot_confusion_matrix(self, cm: np.ndarray, save_dir: str, norm: bool = False):
|
| 312 |
+
plt.figure(figsize=(8, 6))
|
| 313 |
+
fmt = '.2f' if norm else 'd'
|
| 314 |
+
cmap = 'Blues'
|
| 315 |
+
ticklabels = self.class_names
|
| 316 |
+
sns.heatmap(cm, annot=True, fmt=fmt, cmap=cmap, xticklabels=ticklabels, yticklabels=ticklabels,
|
| 317 |
+
vmin=0, vmax=1 if norm else None)
|
| 318 |
+
plt.title('Matriz de Confusión ' + ('(Normalizada)' if norm else '(Absoluta)'))
|
| 319 |
+
plt.ylabel('Etiqueta Real'); plt.xlabel('Etiqueta Predicha')
|
| 320 |
+
plt.tight_layout(); Path(save_dir).mkdir(parents=True, exist_ok=True)
|
| 321 |
+
fname = "confusion_matrix_norm.png" if norm else "confusion_matrix_abs.png"
|
| 322 |
+
plt.savefig(f"{save_dir}/{fname}", dpi=300, bbox_inches='tight'); plt.close()
|
| 323 |
+
print(f"✓ Matriz de confusión guardada en: {save_dir}/{fname}")
|
| 324 |
+
|
| 325 |
+
def save_classification_report(self, report: Dict, save_dir: str):
|
| 326 |
+
Path(save_dir).mkdir(parents=True, exist_ok=True)
|
| 327 |
+
with open(f"{save_dir}/classification_report.txt", 'w', encoding='utf-8') as f:
|
| 328 |
+
f.write("="*60 + "\n")
|
| 329 |
+
f.write("REPORTE DE CLASIFICACIÓN - TEST SET\n")
|
| 330 |
+
f.write("="*60 + "\n\n")
|
| 331 |
+
for emotion, metrics in report.items():
|
| 332 |
+
if emotion in ['accuracy', 'macro avg', 'weighted avg']:
|
| 333 |
+
continue
|
| 334 |
+
f.write(f"\n{emotion.upper()}:\n")
|
| 335 |
+
f.write(f" Precision: {metrics['precision']:.4f}\n")
|
| 336 |
+
f.write(f" Recall: {metrics['recall']:.4f}\n")
|
| 337 |
+
f.write(f" F1-Score: {metrics['f1-score']:.4f}\n")
|
| 338 |
+
f.write(f" Support: {metrics['support']}\n")
|
| 339 |
+
f.write(f"\n{'='*60}\n")
|
| 340 |
+
f.write(f"MACRO AVG:\n Precision: {report['macro avg']['precision']:.4f}\n Recall: {report['macro avg']['recall']:.4f}\n F1-Score: {report['macro avg']['f1-score']:.4f}\n")
|
| 341 |
+
f.write(f"\nWEIGHTED AVG:\n Precision: {report['weighted avg']['precision']:.4f}\n Recall: {report['weighted avg']['recall']:.4f}\n F1-Score: {report['weighted avg']['f1-score']:.4f}\n")
|
| 342 |
+
acc = report.get('accuracy', None)
|
| 343 |
+
if acc is not None:
|
| 344 |
+
f.write(f"\nACCURACY: {acc:.4f}\n")
|
| 345 |
+
f.write("="*60 + "\n")
|
| 346 |
+
print(f"✓ Reporte de clasificación guardado en: {save_dir}/classification_report.txt")
|
| 347 |
+
|
| 348 |
+
|
| 349 |
+
# ==================== MAIN ====================
|
| 350 |
+
def main():
|
| 351 |
+
# Rutas (usa los *v2.json*)
|
| 352 |
+
DATA_DIR = "models/emotion_classifier/data"
|
| 353 |
+
TRAIN_PATH = f"{DATA_DIR}/emotion_dataset_train_es_v2.json"
|
| 354 |
+
VAL_PATH = f"{DATA_DIR}/emotion_dataset_validation_es_v2.json"
|
| 355 |
+
TEST_PATH = f"{DATA_DIR}/emotion_dataset_test_es_v2.json"
|
| 356 |
+
SAVE_DIR = "models/agent_emotion"
|
| 357 |
+
|
| 358 |
+
# Hiperparámetros
|
| 359 |
+
BATCH_SIZE = 16
|
| 360 |
+
NUM_EPOCHS = 20
|
| 361 |
+
EARLY_STOPPING_PATIENCE = 2
|
| 362 |
+
WARMUP_FREEZE_EPOCHS = 2 #antes 5
|
| 363 |
+
LR_ENCODER = 1e-5
|
| 364 |
+
LR_HEAD = 5e-4
|
| 365 |
+
WEIGHT_DECAY = 0.05
|
| 366 |
+
WARMUP_RATIO = 0.1
|
| 367 |
+
|
| 368 |
+
|
| 369 |
+
print("="*60)
|
| 370 |
+
print("CONFIGURACIÓN DEL ENTRENAMIENTO (AgentEmotion v2)")
|
| 371 |
+
print("="*60)
|
| 372 |
+
print(f"Train: {TRAIN_PATH}")
|
| 373 |
+
print(f"Val: {VAL_PATH}")
|
| 374 |
+
print(f"Test: {TEST_PATH}")
|
| 375 |
+
print(f"Batch size: {BATCH_SIZE} | Épocas: {NUM_EPOCHS}")
|
| 376 |
+
print(f"Freeze warmup epochs: {WARMUP_FREEZE_EPOCHS}")
|
| 377 |
+
print(f"LR encoder: {LR_ENCODER} | LR head: {LR_HEAD}")
|
| 378 |
+
|
| 379 |
+
# 1) Detectar clases presentes en TRAIN y remapear a {0..K-1}
|
| 380 |
+
probe = AgentEmotionDataset(TRAIN_PATH)
|
| 381 |
+
present_classes = sorted(set(probe.agent_labels)) # p.ej., [1,2]
|
| 382 |
+
label_names_full = {0:"tristeza",1:"alegría",2:"amor",3:"ira",4:"miedo",5:"sorpresa"}
|
| 383 |
+
class_names = [label_names_full[c] for c in present_classes]
|
| 384 |
+
K = len(present_classes)
|
| 385 |
+
print(f"Clases existentes - valor K : {K}")
|
| 386 |
+
label_map = {orig:i for i, orig in enumerate(present_classes)}
|
| 387 |
+
print(f"Clases de agente en train: {present_classes} → K={K} ({class_names})")
|
| 388 |
+
|
| 389 |
+
# 2) Recrear datasets aplicando el mapeo
|
| 390 |
+
train_ds = AgentEmotionDataset(TRAIN_PATH, label_map=label_map)
|
| 391 |
+
val_ds = AgentEmotionDataset(VAL_PATH, label_map=label_map)
|
| 392 |
+
test_ds = AgentEmotionDataset(TEST_PATH, label_map=label_map)
|
| 393 |
+
|
| 394 |
+
# 3) DataLoaders
|
| 395 |
+
train_loader = DataLoader(train_ds, batch_size=BATCH_SIZE, shuffle=True, collate_fn=collate_fn)
|
| 396 |
+
val_loader = DataLoader(val_ds, batch_size=BATCH_SIZE, shuffle=True, collate_fn=collate_fn)
|
| 397 |
+
test_loader = DataLoader(test_ds, batch_size=BATCH_SIZE, shuffle=True, collate_fn=collate_fn)
|
| 398 |
+
|
| 399 |
+
# 4) Modelo (salida con K clases)
|
| 400 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 401 |
+
model = AgentEmotionPredictClassifier(
|
| 402 |
+
pretrained_encoder="beto",
|
| 403 |
+
max_length=128,
|
| 404 |
+
hidden1=256,
|
| 405 |
+
hidden2=64,
|
| 406 |
+
dropout=0.4, # antes 0.2
|
| 407 |
+
label_feature_dropout=0.5, # antes 0.15
|
| 408 |
+
device=device,
|
| 409 |
+
num_classes=K,
|
| 410 |
+
)
|
| 411 |
+
model.freeze_encoder()
|
| 412 |
+
|
| 413 |
+
# 5) Trainer con num_classes y nombres
|
| 414 |
+
trainer = AgentEmotionTrainer(
|
| 415 |
+
model=model,
|
| 416 |
+
train_loader=train_loader,
|
| 417 |
+
val_loader=val_loader,
|
| 418 |
+
test_loader=test_loader,
|
| 419 |
+
device=device,
|
| 420 |
+
lr_encoder=LR_ENCODER,
|
| 421 |
+
lr_head=LR_HEAD,
|
| 422 |
+
weight_decay=WEIGHT_DECAY,
|
| 423 |
+
num_epochs=NUM_EPOCHS,
|
| 424 |
+
warmup_ratio=WARMUP_RATIO,
|
| 425 |
+
early_stopping_patience=EARLY_STOPPING_PATIENCE,
|
| 426 |
+
warmup_freeze_epochs=WARMUP_FREEZE_EPOCHS,
|
| 427 |
+
num_classes=K,
|
| 428 |
+
class_names=class_names,
|
| 429 |
+
)
|
| 430 |
+
|
| 431 |
+
# 6) Entrenar y evaluar
|
| 432 |
+
trainer.train(
|
| 433 |
+
num_epochs=NUM_EPOCHS,
|
| 434 |
+
early_stopping_patience=EARLY_STOPPING_PATIENCE,
|
| 435 |
+
save_dir=SAVE_DIR,
|
| 436 |
+
)
|
| 437 |
+
|
| 438 |
+
|
| 439 |
+
if __name__ == "__main__":
|
| 440 |
+
main()
|