Delete src/bigru_t/model/gru_hierarchy.py
Browse files12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182"""Xavante - gru_hierarchy.pyResponsabilidade: GRU hierarquica (Teorema 7.1/7.2). Reutiliza o FlexGRUvalidado do FlexNet."""from __future__ import annotations
import loggingfrom typing import List, Optional
import torchimport torch.nn as nn
logger = logging.getLogger(__name__)
class FlexGRUCell(nn.Module): """GRU cell com gate extra de confianca (h_t) que escala update."""
def __init__(self, d_in: int, d_hidden: int): super().__init__() self.d_in = d_in self.d_hidden = d_hidden # Linear concatenado x_t e h_{t-1} self.x2h = nn.Linear(d_in, 3 * d_hidden, bias=True) self.h2h = nn.Linear(d_hidden, 3 * d_hidden, bias=True) # Confidence gate self.conf_gate = nn.Linear(d_in + d_hidden, 1)
def forward(self, x: torch.Tensor, h: Optional[torch.Tensor] = None) -> torch.Tensor: B, D = x.shape if h is None: h = torch.zeros(B, self.d_hidden, device=x.device, dtype=x.dtype) gates_x = self.x2h(x) gates_h = self.h2h(h) x_r, x_z, x_n = gates_x.chunk(3, dim=-1) h_r, h_z, h_n = gates_h.chunk(3, dim=-1) r = torch.sigmoid(x_r + h_r) z = torch.sigmoid(x_z + h_z) n = torch.tanh(x_n + r * h_n) # Confidence scaling (Teorema 17.1) c = torch.sigmoid(self.conf_gate(torch.cat([x, h], dim=-1))) h_new = (1 - z * c) * h + (z * c) * n return h_new
class GRUHierarchy(nn.Module): """GRU hierarquica em L niveis (Teorema 7.1/7.2)."""
def __init__(self, d_model: int, d_hidden: int, n_levels: int = 3): super().__init__() self.n_levels = n_levels self.cells = nn.ModuleList( [FlexGRUCell(d_model if i == 0 else d_hidden, d_hidden) for i in range(n_levels)] ) # Projecao de saida de cada nivel self.projs = nn.ModuleList([nn.Linear(d_hidden, d_model) for _ in range(n_levels)])
def forward(self, x: torch.Tensor) -> torch.Tensor: """ x: [B, L, d_model] Retorna: [B, L, d_model] """ B, L, D = x.shape h_states: List[Optional[torch.Tensor]] = [None] * self.n_levels outputs = [] for t in range(L): xt = x[:, t, :] inp = xt new_h_states = [] for lvl, cell in enumerate(self.cells): h_new = cell(inp, h_states[lvl]) new_h_states.append(h_new) inp = h_new h_states = new_h_states # Use each level's own projection (fixes dead projections bug) combined = sum(self.projs[lvl](h_states[lvl]) for lvl in range(self.n_levels)) / self.n_levels outputs.append(combined) return torch.stack(outputs, dim=1)
__all__ = ["FlexGRUCell", "GRUHierarchy"]src/bigru_t/model/gru_hierarchy.pyDeleted-82