"""Small temporal intent model operating on frozen two-view embeddings.""" from __future__ import annotations import torch from torch import nn class TwoViewIntentGRU(nn.Module): """Fuse Top/Front embeddings at each time and predict two binary states.""" def __init__( self, embedding_dim: int, projection_dim: int = 384, hidden_dim: int = 256, num_layers: int = 1, dropout: float = 0.15, ) -> None: super().__init__() self.embedding_dim = embedding_dim self.projection_dim = projection_dim self.hidden_dim = hidden_dim self.num_layers = num_layers self.projection = nn.Sequential( nn.Linear(embedding_dim * 2, projection_dim), nn.LayerNorm(projection_dim), nn.GELU(), nn.Dropout(dropout), ) self.temporal = nn.GRU( input_size=projection_dim, hidden_size=hidden_dim, num_layers=num_layers, batch_first=True, dropout=dropout if num_layers > 1 else 0.0, ) self.heads = nn.Linear(hidden_dim, 2) def forward(self, embeddings: torch.Tensor) -> torch.Tensor: if embeddings.ndim != 4 or embeddings.shape[2] != 2: raise ValueError( "expected embeddings shaped [batch, time, 2 views, feature], " f"got {tuple(embeddings.shape)}" ) batch, steps, views, features = embeddings.shape if features != self.embedding_dim: raise ValueError(f"expected feature dim {self.embedding_dim}, got {features}") fused = embeddings.reshape(batch, steps, views * features) projected = self.projection(fused) sequence, _ = self.temporal(projected) return self.heads(sequence[:, -1]) def model_config(model: TwoViewIntentGRU) -> dict[str, int | float]: dropout = model.projection[-1].p return { "embedding_dim": model.embedding_dim, "projection_dim": model.projection_dim, "hidden_dim": model.hidden_dim, "num_layers": model.num_layers, "dropout": dropout, }