torchmodal.utils¶
torchmodal.utils ¶
torchmodal.utils ~~~~~~~~~~~~~~~~
Utility functions for MLNN training and evaluation.
Includes temperature annealing schedules, accessibility matrix helpers, and visualization utilities.
anneal_temperature ¶
anneal_temperature(epoch: int, total_epochs: int, tau_start: float = 2.0, tau_end: float = 0.1, schedule: str = 'linear') -> float
Compute annealed temperature for a given epoch.
Temperature annealing drives the "phase transition" observed in satisfiability experiments (Section 5.4): high temperature allows exploration, low temperature forces crisp assignments.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
epoch
|
int
|
Current epoch (0-indexed). |
required |
total_epochs
|
int
|
Total number of training epochs. |
required |
tau_start
|
float
|
Starting (high) temperature. Default 2.0. |
2.0
|
tau_end
|
float
|
Final (low) temperature. Default 0.1. |
0.1
|
schedule
|
str
|
Annealing schedule — |
'linear'
|
Returns:
| Type | Description |
|---|---|
float
|
Temperature value for the current epoch. |
Source code in torchmodal/utils.py
build_ring_accessibility ¶
build_ring_accessibility(num_worlds: int, bidirectional: bool = False, device: device = torch.device('cpu')) -> Tensor
Build a directed ring accessibility matrix.
Used in the synthetic Diplomacy ring scalability test (Appendix G.3). Agent i can access agent (i+1) mod N.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
num_worlds
|
int
|
Number of worlds (agents) in the ring. |
required |
bidirectional
|
bool
|
If |
False
|
device
|
device
|
Target device. |
device('cpu')
|
Returns:
| Type | Description |
|---|---|
Tensor
|
Binary accessibility matrix |
Source code in torchmodal/utils.py
build_sudoku_accessibility ¶
Build the accessibility matrix for a Sudoku puzzle.
Two cells (worlds) are accessible to each other if they share the same row, column, or sub-grid block.
Used in the CSP experiment (Section 5.4).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
block_size
|
int
|
Size of the sub-grid (3 for standard 9x9 Sudoku). |
3
|
device
|
device
|
Target device. |
device('cpu')
|
Returns:
| Type | Description |
|---|---|
Tensor
|
Binary accessibility matrix |
Source code in torchmodal/utils.py
build_grid_accessibility ¶
build_grid_accessibility(rows: int, cols: int, connectivity: str = '4', device: device = torch.device('cpu')) -> Tensor
Build a grid accessibility matrix.
Useful for spatial reasoning where worlds are arranged in a grid.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rows
|
int
|
Number of rows. |
required |
cols
|
int
|
Number of columns. |
required |
connectivity
|
str
|
|
'4'
|
device
|
device
|
Target device. |
device('cpu')
|
Returns:
| Type | Description |
|---|---|
Tensor
|
Binary accessibility matrix |
Source code in torchmodal/utils.py
decode_one_hot ¶
Decode one-hot truth bounds to class labels.
For each world, returns the proposition index with the highest lower bound exceeding the threshold.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bounds
|
Tensor
|
|
required |
threshold
|
float
|
Minimum confidence threshold. Default 0.5. |
0.5
|
Returns:
| Type | Description |
|---|---|
Tensor
|
|
Tensor
|
the threshold). |
Source code in torchmodal/utils.py
bounds_to_labels ¶
bounds_to_labels(bounds: Tensor, threshold_necessary: float = 0.9, threshold_possible: float = 0.1) -> Tuple[Tensor, Tensor, Tensor]
Convert truth bounds to modal classification labels.
Used in the dialect classification experiment (Section 5.2) to assign Necessary (□), Possible (♢), or Indeterminate labels.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bounds
|
Tensor
|
|
required |
threshold_necessary
|
float
|
Above this → □P. Default 0.9. |
0.9
|
threshold_possible
|
float
|
Above this → ♢P. Default 0.1. |
0.1
|
Returns:
| Type | Description |
|---|---|
Tensor
|
Tuple of |
Tensor
|
boolean tensors. |