Deep Maze Solver
Source: Dev.to
Introduction
A few days ago I saw a X Post explaining that diffusion models could be used to solve algorithmic tasks like solving mazes. Intrigued, I decided to replicate this work on a smaller scale and began building a neural network that can solve mazes.
Problem
The goal is to build a convolutional neural network (CNN) that can solve mazes using supervised learning.
Given a maze image, the CNN must find the path from the top‑left corner to the bottom‑right corner.
We will use PyTorch for this task.
Dataset
Because maze generation and solving are deterministic, we can create an infinite dataset on the fly—no manual labeling required!
- Maze generation: Recursive Division algorithm
- Maze solving: Depth‑first Search
We will generate mazes of sizes from 5×5 to 127×127 (where n×n denotes the number of blocks horizontally and vertically). Each maze is resized to 128×128 pixels using nearest‑neighbor interpolation. The resizing may introduce slight distortion, but the maze structure remains unchanged.
Example
A sample 57×57 maze and its solution:
Pre‑processing
- Input: The raw maze image.
- Label: The solved maze showing only the path (borders and walls are ignored).
The same maze with only the solution path:
Both the input and label are resized to 128×128 pixels:
The Model
We will use a standard U‑Net architecture—nothing fancy—to map the input maze to its solution path.
Training
Hyperparameters
| Parameter | Value |
|---|---|
| Batch size | 16 |
| Optimizer | Adam |
| Loss function | Binary Cross‑Entropy |
| Learning rate | 1e‑4 |
| Training set size | 500 000 samples |
| Validation set size | 100 000 samples |
Because mazes are generated randomly, an “epoch” (i.e., a full pass through the training set) does not have the usual meaning; each epoch simply corresponds to generating a new batch of mazes.
Results
Training / Validation Loss
The graph below shows training loss computed every 1 000 samples. Validation loss was evaluated on 200 samples after each 1 000‑sample training segment.
Sample Outputs
15 × 15 maze
57 × 57 maze
127 × 127 maze
The model performs well on small and medium mazes, but its accuracy drops on the largest mazes. Further improvements (e.g., deeper networks, attention mechanisms, or diffusion‑based approaches) could address this limitation.
Techniques Companies Predominantly Use Right Now
(Big model, Big data, Big‑time training, Big hardware, Big money)
Conclusion
It has been very interesting working on this project. Neural networks are being used to solve far more complex problems, but it is fascinating to see them applied to algorithmic challenges like these.
- Here is the link to the GitHub repo if you are curious about what happens behind the scenes.
- Also, shout‑out to this wonderful repository for providing the U‑Net code.






