Looped CNN - Thinking in Mazes
Introduction
Standard feed-forward neural networks lack the ability to scale their computation based on the “difficulty” of the problem. Their computation budget is limited by their predefined architecture. A feed-forward network trained on a simple $9\times9$ maze has no built-in mechanism to “think longer” when faced with a harder $13\times13$ maze.
The paper Can You Learn an Algorithm? Generalizing from Easy to Hard Problems with Recurrent Networks shows that looping intermediate convolutional layers, trained on simple problems at a limited effective depth, can solve more complex problems by performing additional recurrences during inference. They generalize by extending their iteration budget, without adding parameters.
But what exactly is the network doing during those extra iterations? Has it learned a classical pathfinding algorithm such as breadth-first search, or a different mechanism based on local convolutional updates?
Here, I visualize the output of a weight-tied CNN at each iteration. The behavior suggests morphological pruning: the network explores paths while collapsing dead ends through local updates. From this insight, we can identify their failure cases. For example, I’ll examine circular and disconnected mazes below. And also why input injection is important in these models.
We can also think of these models as a dynamical system where the hidden state evolves following the “field” defined by looped layers. From this perspective, I test an Anderson solver, which is a classical method for finding fixed-point of dynamical systems.
The maze environment
The model receives an image of a maze, with black walls and green and red markers for the start and goal. Its task is to mark the shortest path connecting them.
A solution counts as correct only when the entire predicted path matches the labeled solution.
The setup uses $9\times9$ mazes for training and $33\times33$ mazes for testing. Can the model solve these larger mazes simply by running for more iterations?
Weight-tied CNNs
In a feed-forward ResNet, successive residual blocks have separate parameters. A weight-tied CNN instead applies the same recurrent module repeatedly, updating a hidden representation of the maze. Each iteration reuses the same weights.
The main model visualized in this post also uses input injection (recall): the recurrent module receives the original maze at every iteration, alongside its current hidden state. Its update is $h_{t+1}=F_\theta(h_t,x)$, keeping the maze available throughout the process.
At test time, we can give a harder maze more iterations without adding parameters or rebuilding the network. This is how the model can “think longer” than it did during training.
What is the model doing?
Exploring paths and pruning dead ends
The predictions extend into branches, then retract or disappear around forks and endpoints. This resembles a conditional morphological erosion operation. In one forward pass, the tip of a dead-end branch is pruned. In the next pass, the pixel behind it becomes the new tip. Iteration by iteration, the dead branch collapses backward until it reaches a junction.
This suggests a process that could extend to larger inputs given enough iterations: the local rules of expansion and erosion do not depend on the overall maze size. The visual resemblance is an interpretation of the network’s behavior, rather than proof that it implements a particular classical algorithm.
Circular mazes
The next example probes where this behavior fails by introducing circular paths.
Disconnected paths
A disconnected maze provides another case for inspecting how the updates behave when a connecting path is absent.
Without input injection
Removing input injection leaves the update $h_{t+1}=F_\theta(h_t)$, so the module must carry the maze information through its hidden state. These examples show behavior without input injection on two maze sizes, matching the form implemented by the public RecurModel.
A small maze
A larger maze
Using an Anderson solver
For a fixed maze $x$, repeatedly applying $F_\theta(\cdot,x)$ is a fixed-point iteration. An Anderson solver instead combines information from several previous iterates to seek a state satisfying $h^=F_\theta(h^,x)$. Does this change the route to a solution? Here is the corresponding visualization using an Anderson solver.
The code is in my Thinking Mazes JAX repository.