File size: 7,999 Bytes
a7037f7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
"""
=== MIA · Clasificador de Emociones (Pretrained Encoder + MLP) ===
- Mantiene compatibilidad con tu API pública.
- Permite usar tu TextEmbedder aleatorio (emb_dim) o un encoder preentrenado (BETO) con 768D.
- Expone freeze/unfreeze para controlar el fine-tuning desde el trainer.
"""

import torch
import torch.nn as nn
from typing import List, Optional
from transformers import AutoTokenizer, AutoModel


# ==================== MÓDULO 1A: TextEmbedder (embedding aleatorio) ====================
class TextEmbedder(nn.Module):
    """
    Módulo de Embedding simple:
    - Usa el tokenizador de BETO para sub-palabras (por conveniencia, vocab, pad_id, etc.)
    - La representación es un embedding aleatorio + mean pooling (no contextual).
    """
    def __init__(
        self,
        model_name: str = "dccuchile/bert-base-spanish-wwm-cased",
        emb_dim: int = 300,
        max_length: int = 128,
        device: Optional[torch.device] = None
    ):
        super().__init__()
        self.tokenizer = AutoTokenizer.from_pretrained(model_name, use_fast=True)
        self.vocab_size = self.tokenizer.vocab_size
        self.pad_id = self.tokenizer.pad_token_id
        self.cls_id = self.tokenizer.cls_token_id
        self.sep_id = self.tokenizer.sep_token_id
        self.max_length = max_length

        # Capa de embedding
        self.embedding = nn.Embedding(self.vocab_size, emb_dim, padding_idx=self.pad_id)
        nn.init.xavier_uniform_(self.embedding.weight)
        with torch.no_grad():
            if self.pad_id is not None:
                self.embedding.weight[self.pad_id].zero_()

        # Regularización opcional (ayuda contra sobreajuste)
        self.emb_dropout = nn.Dropout(p=0.1)

        self.device = device or torch.device("cuda" if torch.cuda.is_available() else "cpu")
        self.to(self.device)

    def embed_batch(self, texts: List[str]) -> torch.Tensor:
        batch = self.tokenizer(
            texts, padding=True, truncation=True, max_length=self.max_length, return_tensors="pt"
        )
        input_ids = batch["input_ids"].to(self.device)           # [B, T]
        attention_mask = batch["attention_mask"].to(self.device) # [B, T]

        embeds = self.embedding(input_ids)                       # [B, T, E]
        if self.training:
            embeds = self.emb_dropout(embeds)

        mask = attention_mask.bool()                             # [B, T]
        if self.cls_id is not None:
            mask = mask & (input_ids != self.cls_id)
        if self.sep_id is not None:
            mask = mask & (input_ids != self.sep_id)

        mask_f = mask.unsqueeze(-1).float()                      # [B, T, 1]
        summed = (embeds * mask_f).sum(dim=1)                    # [B, E]
        counts = mask_f.sum(dim=1).clamp(min=1.0)                # [B, 1]
        sentence_vecs = summed / counts                          # [B, E]
        return sentence_vecs

    def embed_sentence(self, text: str) -> torch.Tensor:
        return self.embed_batch([text])[0]


# ==================== MÓDULO 1B: BETOEmbedder (encoder preentrenado) ====================
class BETOEmbedder(nn.Module):
    """
    Usa el encoder de BETO (BERT en español) para obtener embeddings contextuales.
    Mean pooling sobre last_hidden_state.
    Salida: [B, 768]
    """
    def __init__(
        self,
        model_name: str = "dccuchile/bert-base-spanish-wwm-cased",
        max_length: int = 128,
        device: Optional[torch.device] = None
    ):
        super().__init__()
        self.device = device or torch.device("cuda" if torch.cuda.is_available() else "cpu")
        self.tokenizer = AutoTokenizer.from_pretrained(model_name, use_fast=True)
        self.encoder = AutoModel.from_pretrained(model_name)
        self.max_length = max_length
        self.encoder.to(self.device)

    def embed_batch(self, texts: List[str]) -> torch.Tensor:
        inputs = self.tokenizer(
            texts, padding=True, truncation=True, max_length=self.max_length, return_tensors="pt"
        ).to(self.device)
        outputs = self.encoder(**inputs)  # last_hidden_state [B, T, 768]
        last_hidden = outputs.last_hidden_state
        mask = inputs["attention_mask"].unsqueeze(-1).float()  # [B, T, 1]
        pooled = (last_hidden * mask).sum(dim=1) / mask.sum(dim=1).clamp(min=1e-9)  # [B, 768]
        return pooled


# ==================== MÓDULO 2: MLP Classifier ====================
class MLPClassifier(nn.Module):
    """
    Feedforward para clasificación de emociones:
    Input → 128 → 64 → 6 (logits)
    """
    def __init__(
        self,
        input_dim: int = 300,
        hidden1: int = 128,
        hidden2: int = 64,
        num_classes: int = 6,
        dropout: float = 0.3
    ):
        super().__init__()
        self.fc1 = nn.Linear(input_dim, hidden1)
        self.relu1 = nn.ReLU()
        self.dropout1 = nn.Dropout(dropout)

        self.fc2 = nn.Linear(hidden1, hidden2)
        self.relu2 = nn.ReLU()
        self.dropout2 = nn.Dropout(dropout)

        self.fc3 = nn.Linear(hidden2, num_classes)

    def forward(self, x: torch.Tensor) -> torch.Tensor:
        x = self.fc1(x); x = self.relu1(x); x = self.dropout1(x)
        x = self.fc2(x); x = self.relu2(x); x = self.dropout2(x)
        x = self.fc3(x)
        return x


# ==================== MÓDULO 3: Modelo Completo ====================
class EmotionClassifier(nn.Module):
    """
    Integra embedder (aleatorio o BETO) + MLP.
    - `pretrained_encoder=None` → usa TextEmbedder (emb_dim configurable)
    - `pretrained_encoder="beto"` → usa BETOEmbedder (salida 768D)
    """
    def __init__(
        self,
        model_name: str = "dccuchile/bert-base-spanish-wwm-cased",
        emb_dim: int = 300,
        max_length: int = 128,
        hidden1: int = 128,
        hidden2: int = 64,
        num_classes: int = 6,
        dropout: float = 0.3,
        device: Optional[torch.device] = None,
        pretrained_encoder: Optional[str] = None
    ):
        super().__init__()
        self.device = device or torch.device("cuda" if torch.cuda.is_available() else "cpu")

        if pretrained_encoder == "beto":
            self.embedder = BETOEmbedder(model_name=model_name, max_length=max_length, device=self.device)
            embed_dim = 768
        else:
            self.embedder = TextEmbedder(model_name=model_name, emb_dim=emb_dim, max_length=max_length, device=self.device)
            embed_dim = emb_dim

        self.classifier = MLPClassifier(
            input_dim=embed_dim, hidden1=hidden1, hidden2=hidden2, num_classes=num_classes, dropout=dropout
        )

        self.label_map = {0: "tristeza", 1: "alegría", 2: "amor", 3: "ira", 4: "miedo", 5: "sorpresa"}

        self.to(self.device)

    # ---------- Forward & Utils ----------
    def forward(self, texts: List[str]) -> torch.Tensor:
        embeddings = self.embedder.embed_batch(texts)  # [B, D]
        logits = self.classifier(embeddings)           # [B, C]
        return logits

    def predict(self, texts: List[str], return_probs: bool = False):
        self.eval()
        with torch.no_grad():
            logits = self.forward(texts)
            probs = torch.softmax(logits, dim=-1)
            predictions = torch.argmax(probs, dim=-1)
            emotions = [self.label_map[p.item()] for p in predictions]
            if return_probs:
                return emotions, probs.cpu().numpy()
            return emotions

    def predict_single(self, text: str, return_probs: bool = False):
        out = self.predict([text], return_probs=return_probs)
        if return_probs:
            emotions, probs = out
            return emotions[0], probs[0]
        return out[0]

    # ---------- Fine-tuning helpers ----------
    def freeze_encoder(self):
        for p in self.embedder.parameters():
            p.requires_grad = False

    def unfreeze_encoder(self):
        for p in self.embedder.parameters():
            p.requires_grad = True