slidingpuzzle.nn package¶
Submodules¶
slidingpuzzle.nn.dataset module¶
Utilities for creating, saving, and loading board datasets.
- class SlidingPuzzleDataset(examples: Iterable[tuple[tuple[tuple[int, ...], ...], list[int]]])[source]¶
- build_or_load_dataset(h: int, w: int, num_examples: int | None = None, examples_file: str | None = None, **kwargs) SlidingPuzzleDataset[source]¶
Loads examples, constructs a SlidingPuzzleDataset from them, and returns it. If there is a mismatch between the requested num_examples and the loaded examples file, examples may be truncated or new examples may be constructed and saved to disk. No duplicate examples are produced.
- Parameters:
h – The height of the board to locate a dataset for
w – The width of the board to locate a dataset for
num_examples – The total number of examples desired. If there are too many, examples will be truncated. If there are too few, new examples will be constructed. If
None, the entire dataset loaded will be used.examples_file – The name of the examples file to save or load.
kwargs – Will be forwaded to
make_examples()if it is called.
- Returns:
A dataset for the requested puzzle size.
- generate_examples(h: int, w: int, start: int = 0, stop: int | None = None) list[tuple[tuple[tuple[int, ...], ...], list[int]]][source]¶
Helper to generate all training examples for a board size by performing reverse BFS.
Note
It is safe to interrupt this function any time with a keyboard interrupt and receive back the examples generated so far.
- Parameters:
h – Height of the board
w – Width of the board
start – Start index (0 starts at the initial solved board)
stop – Index to stop at (
Nonewill product all boards)
- Returns:
A list of examples.
- get_examples(h: int, w: int, num_examples: int, prior_examples: list[tuple[tuple[tuple[int, ...], ...], list[int]]], **kwargs) list[tuple[tuple[tuple[int, ...], ...], list[int]]][source]¶
Returns
num_examplestotal unique examples, starting withprior_examplesif they are provided. May truncate or produce new examples as necessary.
- load_dataset(h: int, w: int, examples_file: str | None = None) SlidingPuzzleDataset[source]¶
Loads examples, constructs a SlidingPuzzleDataset from them, and returns it.
- Parameters:
h – The height of the board to locate a dataset for
w – The width of the board to locate a dataset for
examples_file – Path to file. Default will check local
datasetsdirectory.
- Returns:
A dataset for the requested puzzle size.
- load_examples(h: int, w: int, examples_file: str | None = None) list[tuple[tuple[tuple[int, ...], ...], list[int]]][source]¶
Loads examples from a JSON file.
- random_examples(h: int, w: int, num_examples: int, ignore_examples: list[tuple[tuple[tuple[int, ...], ...], list[int]]] | None = None, **kwargs) list[tuple[tuple[tuple[int, ...], ...], list[int]]][source]¶
- Constructs a list of training examples, which are tuples of:
(board, solution)
- Parameters:
h – Height of board.
w – Width of board.
num_examples – Number of examples to produce.
ignore_examples – If any example produced matches one in
ignore_examples, it will be discarded.kwargs – Args to pass to the search algorithm used to find board solutions.
- Returns:
The list of training examples.
slidingpuzzle.nn.eval module¶
Module used to evaluate model performance vs. other heuristics.
- accuracy(expected, predicted) float[source]¶
Helper function to estimate accuracy of sliding puzzle model outputs. This function replaces NaNs/infinities with 0s and uses:
\[f(e, p) = 1 - \frac{|e - p|}{1 + |e - p|}\]It returns the sum along the batch dimension.
Notes
Any NaNs/infs will be replaced by 0.
- Parameters:
expected – A tensor with the expected value
predicted – A tensor with the predicted value
- Returns:
The accuracy as a float in the range [0, 1] summed along batch dim.
- eval_checkpoint(model: Module, tag: str = 'acc', num_iters: int | None = None, device: str | None = None, **kwargs) float[source]¶
Loads the provided model from the checkpoint at
epochand runsevaluate, returning the result. Ifepochis not provided, the latest checkpoint is used.
- eval_heuristic(dataset: SlidingPuzzleDataset, heuristic: Callable[[ndarray[Any, dtype[_ScalarType_co]]], int | float]) float[source]¶
Runs all examples through heuristic and evaluates accuracy as a function of heuristic proximity to the true distance. Can be used to compare model performance against other heuristics.
- evaluate(model: Module, criterion, dataset: Dataset) tuple[float, float][source]¶
Runs the model on provided datatset computing average loss and accuracy.
- Parameters:
model – Model to evaluate
criterion – The criterion function to use for evaluation
dataset – The dataset of examples
- Returns:
A tuple(avg. loss, avg. accuracy)
slidingpuzzle.nn.heuristics module¶
Defines neural network-guided heuristics.
- get_heuristic(h: int, w: int, version: str) Callable[[ndarray[Any, dtype[_ScalarType_co]]], int | float][source]¶
slidingpuzzle.nn.models module¶
Defines a PyTorch model to evaluate sliding puzzle boards.
- class Model_v1(h: int, w: int)[source]¶
A stack of linear layers that accepts a board as input and outputs the estimated distance to the goal.
Trained with:
SGD(lr = 0.001, momentum = 0.9) MSELoss
Total parameters: 1322496
- forward(x)[source]¶
Defines the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Moduleinstance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- class Model_v2(h: int, w: int)[source]¶
A multi-headed attention-based network with about the same number of parameters as v1. (WIP)
Trained with:
SGD(lr = 0.0001, momentum = 0.9) MSELoss
Total parameters: 1170767
- forward(x)[source]¶
Defines the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Moduleinstance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- get_num_params(model: Module) int[source]¶
Compute the total number of parameters in a model.
- Parameters:
model – A model
- Returns:
The total number of trainable parameters
slidingpuzzle.nn.paths module¶
Utilities for dealing with paths required during model training and inference.
- get_checkpoint_dir(h: int, w: int) Path[source]¶
Get the path to store checkpoints for this board size.
- Parameters:
h – Board height
w – Board width
- Returns:
The checkpoint dir path