Download packages/softasp-solver/src/softasp_solver/sudoku/baselines.py from wabdalm/AS2: direct link, hf CLI and curl.
- Browser
- Download file 2.56 kB
-
https://huggingface.co/wabdalm/AS2/resolve/main/packages/softasp-solver/src/softasp_solver/sudoku/baselines.py
- Command line
-
hf download hf://wabdalm/AS2/packages/softasp-solver/src/softasp_solver/sudoku/baselines.py
-
curl -L -o baselines.py https://huggingface.co/wabdalm/AS2/resolve/main/packages/softasp-solver/src/softasp_solver/sudoku/baselines.py
2.56 kB
| """CNN perception stage used by Sudoku training from scratch.""" | |
| from __future__ import annotations | |
| import torch | |
| import torch.nn as nn | |
| import torch.nn.functional as F | |
| from softasp_solver.base_model import BaseSoftASPModel | |
| from softasp_solver.registry import register_model | |
| from softasp_solver.sudoku.softasp_sudoku import CellCNN | |
| class PerceptionOnlySudoku(BaseSoftASPModel): | |
| """Perception-only baseline: CNN encoder + direct classification. | |
| No constraint reasoning. Blank cells are predicted from their (empty) image | |
| alone, so accuracy on blank cells should be ~11% (random guessing among 9). | |
| Args: | |
| d_model: Embedding dimension. | |
| """ | |
| def __init__(self, d_model: int = 128) -> None: | |
| super().__init__() | |
| self.d_model = d_model | |
| self.cell_cnn = CellCNN(d_model=d_model) | |
| self.head = nn.Linear(d_model, 9) | |
| def _extract_cells(self, images: torch.Tensor) -> torch.Tensor: | |
| B = images.shape[0] | |
| x = images.reshape(B, 1, 9, 28, 9, 28) | |
| x = x.permute(0, 2, 4, 1, 3, 5) | |
| return x.reshape(B * 81, 1, 28, 28) | |
| def forward(self, batch: dict[str, torch.Tensor]) -> dict[str, torch.Tensor]: | |
| images = batch["images"] | |
| labels = batch["labels"] | |
| puzzle_mask = batch["puzzle_mask"] # (B, 81) int64, 1 = clue cell | |
| B = images.shape[0] | |
| if images.dim() == 4 and images.shape[-1] == 1: | |
| images = images.permute(0, 3, 1, 2) | |
| if images.dtype != torch.float32: | |
| images = images.float() / 255.0 | |
| cells = self._extract_cells(images) | |
| embeds = self.cell_cnn(cells).reshape(B, 81, self.d_model) | |
| logits = self.head(embeds) # (B, 81, 9) | |
| # CE loss only on clue cells — blank cells are featureless black | |
| # patches; the model cannot learn anything from them. | |
| clue_flat = puzzle_mask.reshape(-1).bool() # (B*81,) | |
| loss = F.cross_entropy( | |
| logits.reshape(-1, 9)[clue_flat], | |
| labels.reshape(-1)[clue_flat], | |
| ) | |
| predictions = logits.argmax(dim=-1) # (B, 81) | |
| # Flat clue-cell tensors for the clue_accuracy metric in the evaluator | |
| clue_logits = logits.reshape(-1, 9)[clue_flat] # (n_clue, 9) | |
| clue_labels = labels.reshape(-1)[clue_flat] # (n_clue,) | |
| return { | |
| "loss": loss, | |
| "logits": logits, | |
| "predictions": predictions, | |
| "clue_logits": clue_logits, | |
| "clue_labels": clue_labels, | |
| } | |