File size: 6,713 Bytes
cf52a55 | 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 | from __future__ import annotations
"""
MΓDULO NEURAL β TruthScoringModel
=================================
Modelo PyTorch baseado em Transformer (via `transformers`) que recebe
proposiΓ§Γ΅es textuais (saΓda de L2) e produz:
- logits de classe para o estado paraconsistente:
Verdadeiro | Falso | IntermediΓ‘rio | Indeterminado
- um escalar v β [0,1] representando o valor-verdade aproximado
(compatΓvel com `ParaconsistentValue.truth_value`).
Este mΓ³dulo NΓO Γ© acoplado diretamente ao pipeline; ele pode ser
instanciado e passado opcionalmente para a `ParaconsistentEngine`,
que o usarΓ‘ para calcular (ΞΌ, Ξ») neurais em vez das heurΓsticas puras.
"""
from dataclasses import dataclass
from typing import Dict, List, Tuple, Optional
import torch
import torch.nn as nn
from torch.utils.data import Dataset
from transformers import AutoModel, AutoTokenizer
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# RΓ³tulos paraconsistentes
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
LABEL2ID: Dict[str, int] = {
"Verdadeiro": 0,
"Falso": 1,
"IntermediΓ‘rio": 2,
"Indeterminado": 3,
}
ID2LABEL: Dict[int, str] = {v: k for k, v in LABEL2ID.items()}
@dataclass
class PropositionExample:
"""Exemplo supervisionado para treinamento do modelo neural."""
text: str
label_state: str # uma das chaves de LABEL2ID
truth_value: float # valor-verdade escalar em [0,1]
class PropositionDataset(Dataset):
"""
Dataset simples de proposiΓ§Γ΅es rotuladas com estado paraconsistente
e valor-verdade escalar.
"""
def __init__(self, examples: List[PropositionExample], tokenizer, max_length: int = 64):
self.examples = examples
self.tokenizer = tokenizer
self.max_length = max_length
def __len__(self) -> int:
return len(self.examples)
def __getitem__(self, idx: int) -> Dict[str, torch.Tensor]:
ex = self.examples[idx]
enc = self.tokenizer(
ex.text,
truncation=True,
padding="max_length",
max_length=self.max_length,
return_tensors="pt",
)
item = {k: v.squeeze(0) for k, v in enc.items()}
item["labels_state"] = torch.tensor(LABEL2ID[ex.label_state], dtype=torch.long)
item["labels_truth"] = torch.tensor(float(ex.truth_value), dtype=torch.float)
return item
class TruthScoringModel(nn.Module):
"""
Modelo hΓbrido:
- backbone TransformerEncoder (BERT-like)
- cabeΓ§a de classificaΓ§Γ£o para o estado lΓ³gico
- cabeΓ§a de regressΓ£o para valor-verdade escalar
"""
def __init__(
self,
backbone_name: str = "bert-base-multilingual-cased",
num_labels: int = 4,
) -> None:
super().__init__()
self.backbone = AutoModel.from_pretrained(backbone_name)
hidden_size = self.backbone.config.hidden_size
self.classifier = nn.Linear(hidden_size, num_labels)
self.truth_head = nn.Sequential(
nn.Linear(hidden_size, hidden_size),
nn.ReLU(),
nn.Linear(hidden_size, 1),
nn.Sigmoid(), # restringe para [0,1]
)
def forward(
self,
input_ids: torch.Tensor,
attention_mask: torch.Tensor,
labels_state: Optional[torch.Tensor] = None,
labels_truth: Optional[torch.Tensor] = None,
) -> Dict[str, torch.Tensor]:
outputs = self.backbone(input_ids=input_ids, attention_mask=attention_mask)
cls_emb = outputs.last_hidden_state[:, 0, :]
logits_state = self.classifier(cls_emb)
truth_score = self.truth_head(cls_emb).squeeze(-1)
loss: Optional[torch.Tensor] = None
if labels_state is not None and labels_truth is not None:
ce_loss = nn.CrossEntropyLoss()(logits_state, labels_state)
mse_loss = nn.MSELoss()(truth_score, labels_truth)
loss = ce_loss + 0.5 * mse_loss
return {
"logits_state": logits_state,
"truth_score": truth_score,
"loss": loss,
}
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# Helpers de inferΓͺncia
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def load_tokenizer(backbone_name: str = "bert-base-multilingual-cased"):
"""Cria um tokenizer compatΓvel com o backbone."""
return AutoTokenizer.from_pretrained(backbone_name)
def score_proposition(
model: TruthScoringModel,
tokenizer,
text: str,
device: Optional[torch.device] = None,
) -> Tuple[str, float]:
"""
Executa inferΓͺncia para uma ΓΊnica proposiΓ§Γ£o textual.
Retorna (label_state, truth_score).
"""
if device is None:
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model.eval()
enc = tokenizer(
text,
truncation=True,
padding="max_length",
max_length=64,
return_tensors="pt",
)
enc = {k: v.to(device) for k, v in enc.items()}
model = model.to(device)
with torch.no_grad():
out = model(**enc)
logits = out["logits_state"]
truth_score = float(out["truth_score"].cpu().item())
pred_id = int(logits.argmax(dim=-1).cpu().item())
pred_label = ID2LABEL[pred_id]
return pred_label, truth_score
def neural_annotations(
model: TruthScoringModel,
tokenizer,
text: str,
) -> Tuple[float, float, str, float]:
"""
Mapeia a saΓda do modelo neural para (ΞΌ, Ξ») compatΓveis com L3.
Retorna (mu, lam, state_label, truth_score).
"""
state, v = score_proposition(model, tokenizer, text)
if state == "Verdadeiro":
mu = v
lam = 1.0 - v
elif state == "Falso":
mu = 1.0 - v
lam = v
elif state == "IntermediΓ‘rio":
mu = 0.4 + 0.2 * v
lam = 0.4 + 0.2 * (1.0 - v)
else: # Indeterminado
mu = 0.3
lam = 0.3
return float(mu), float(lam), state, float(v)
|