Skip to content
Writing

Teaching a network to think longer

My CS440 project asked whether a recursive network can stand in for search. It learned to think in steps. It did not learn when to stop. You can play it below.

Published
Read
7 min
On this page
  1. The question
  2. Why Connect Four
  3. Attempt one: it answered in one step
  4. Attempt two: put the thinking in the outer loop
  5. It learned to think
  6. It didn’t learn when to stop
  7. Play it
  8. What I’d try next

The model you can play at the bottom of this post thinks before it moves. Each step it refines its answer, and a small head decides when the answer is good enough. How long it thinks is its own call.

Getting it to use those steps at all took three attempts. Getting it to spend more of them on harder positions is the part I haven’t solved.

This was my final project for CS440. The code, the data and every number below are in CS440-Final-Project.

The question

Game-playing agents like AlphaZero pair a neural network with tree search. The network says which moves look good; the search checks by playing them out. The search is what makes them strong, and it’s also what makes them expensive.

A transformer already looks at every square of the board at once, so it has breadth for free. What a fixed stack of layers doesn’t have is depth: it can only take as many dependent reasoning steps as it has layers. Search supplies that depth by going one move further down the tree, then another.

Recursive models like TRM take a different route. They reuse one small network over and over, updating a latent state of the whole board each time. Each pass is something like one round of value iteration over every square at once. So the hypothesis was:

A weight-tied recursive network does something like search in latent space, and the number of steps it needs grows with how far ahead the position requires you to look.

Why Connect Four

Because it’s solved. Pascal Pons’ solver gives the exact value of every move in every position, so I never have to guess whether the model was right.

From the solver’s scores I built a label for how hard a position is: r, the required lookahead. It is the smallest search depth that proves the best move beats every other move. A position with r = 1 is “win now”. A position with r = 9 needs nine plies of lookahead before the winning move separates from the rest. The label is checked against a brute-force search in the tests: a depth-k search goes from chance to 100% exactly at k = r.

The test set is 12,991 positions from solver-played games, each with exactly one winning move, balanced by r and by game stage so that “hard” doesn’t just mean “late in the game”. I’ll call it the tactical set.

Attempt one: it answered in one step

The first models were TRMs trained the standard way: each step runs the network 42 times, gets graded on its own, and hands its state to the next step with the gradient cut. They were good. The best one picked the optimal move 89.7% of the time, with a quarter of the parameters of a plain 8-layer transformer that scored 84.1%.

But it got 89.8% after one step and 89.7% after sixteen. The learned stop head stopped at step 1 on almost every position. Running extra recursion at test time moved accuracy by about a point, the same at every depth. A run that forced 32 steps on every position learned too slowly to pass its go/no-go check and was stopped at 60,000 updates.

The models that learned well did it in one step. Looking back, the training made that the only sensible thing to do:

What the training didEffect
Each step was 42 layer passes deepOne step already solved what the model could solve
The gradient was cut between stepsA step was never rewarded for setting up a later one
Only the single best move countedHalf-finished lookahead earned nothing

Attempt two: put the thinking in the outer loop

So I changed all three.

  • Shallow steps. One step is now 4 layer passes, not 42. The model gets up to 24 of them, 96 passes in all.
  • Credit across steps. The state is never detached, so the learning signal flows back through every step. A step can be rewarded for helping a later one.
  • A halting-weighted loss, after PonderNet. The stop head gives the chance of stopping at each step. The loss grades each step’s answer weighted by the chance the model stops there, plus a penalty that keeps the expected number of steps near 6.
  • Partial credit. A new head predicts, for every column, what playing it leads to: win or loss, and roughly how soon. The model can get the easy columns right before the hard ones.

Running the gradient through 24 steps would need about 870 MB per step on a batch of 128. Recomputing each step on the backward pass instead of storing it brought that to 13 MB, so memory stopped deciding the design. torch.compile plus fp16 made training 1.57× faster, measured with neural-cost. Every run trained for 22,000 updates of 128 positions on two Macs at home.

It learned to think

Accuracy rises from 51–75% at step 1 to 79–100% by step 9, for every lookahead bucketAccuracy rises from 51–75% at step 1 to 79–100% by step 9, for every lookahead bucket
Figure 1 Optimal-move accuracy at each outer step for the best model, by required lookahead r. Tactical set, 12,991 positions.

This is the curve that had been flat in every earlier model. The best model gets 57.9% at step 1 and 88.1% at the step it chooses to stop on, about step 9.5. On the hardest positions, r = 9 and up, it goes from 50% to 78%. The steps are now doing real work.

The adaptive model with full credit and the per-column target scores 88.1%, ahead of every fixed-step controlThe adaptive model with full credit and the per-column target scores 88.1%, ahead of every fixed-step control
Figure 2 Tactical-set accuracy and mean steps for each training setup. Same architecture, same data, same number of updates.

The ablations say which change mattered. Cutting the gradient between steps (the old setup, with everything else new) gives 79.1%, worse than a model trained to answer in a single step. Letting credit flow across steps gives 86.4%. The per-column target adds another 1.7 points. Models trained to always take a fixed number of steps top out at 86.0%, and that one needs all 24.

In a round-robin of 101,880 games from 1,415 openings, this model finished top of the field at 1546 Elo. The perfect solver sat at 1726, and the model took 23% of the points against it.

It didn’t learn when to stop

Here is the part that didn’t work.

If the network were doing something like search, positions that need more lookahead should get more steps. They barely do:

The model stops around step 8.5 to 9.9 at every depth, while its answer settles by step 1 to 3The model stops around step 8.5 to 9.9 at every depth, while its answer settles by step 1 to 3
Figure 3 When the model stops vs. when its answer stops changing, by required lookahead r.

It stops at step 8.5 on r = 1 positions and about 9.8 on everything from r = 5 to r = 16. The correlation between stopping step and r, measured within each game stage, is 0.25. That clears the bar I set for the pilot (0.1) and misses the one I set for success before running anything (0.3 in every stage).

The blue line is the giveaway. On a typical position the model’s answer has settled by step 1, 2 or 3, and then it keeps thinking for another six or seven steps anyway. It overthinks easy positions and leaves almost no slack on the hardest ones.

A model that always stopped at step 9 would score 87.8%, 0.3 points behind the adaptive one. On the second training seed the difference is zero. If you could stop each position at its best step, you’d get 94.8%. That gap of 6.7 points is what a good stop rule is worth, and this one isn’t capturing it.

So the honest summary: the network learned to use thinking steps, and they buy a lot of accuracy. It did not learn to spend them where they’re needed. The depth hypothesis is still neither confirmed nor ruled out.

Play it

This is the same checkpoint, running in your browser.

Loading the model…

1.25M parametersNo searchRuns on your device

Play it full screen →

What you’re looking at:

  • The bars over the board are its move head after each step: how much it likes each column right now. Early steps are often wrong; watch for the step where it changes its mind.
  • The ring fills with its readiness to stop. It plays when the ring closes, and the number inside is the step it’s on. It tends to think longest in the middle of the game, about 10.4 steps around move 24 against 8 on the first.
  • The dots mark the empty holes where its internal state moved most in that step. It’s a rough picture of where the update is happening, not an explanation of why.

There is no ML runtime here. The network is 1.25M parameters, exported to a 2.5 MB file of 16-bit floats and run in about 250 lines of TypeScript in a Web Worker. Rounding the weights to fp16 changed none of its moves on 2,000 tactical positions, and a check script compares the browser port’s outputs to PyTorch’s at every step; they agree to within 0.000003. One step takes about a tenth of a second on a laptop, so a move takes about a second, and what you see is the real pace.

It’s a strong player for a model that never looks a single move ahead, and a weak one next to the solver. You can beat it.

What I’d try next

The stop head is trained only through the halting-weighted loss, which pays it for stopping when the answer is good but doesn’t make stopping later costly enough on easy positions. The next thing I’d try is giving it a direct target: the first step after which the answer stops changing. Then I’d run more seeds, because a 0.3-point edge on one seed and zero on the other isn’t a result yet.

Let's connect

Spot something I got wrong?

Corrections, follow-up questions, or a war story of your own. I read every message and reply to the ones that aren't a template.

Email me

Usually replies within a day or two · St. Louis, Missouri

esc

Home

Main landing page & highlights

Page

Work

All engineering projects & case studies

Page

Writing

Technical notes & engineering articles

Page

Games

Browser games, several against real models

Page

About

Background, career timeline & skills

Page

Contact

Email, inquiries & consultation

Page

Copy Email Address

davidgraymi@gmail.com

Action

Toggle Light / Dark Theme

Switch color theme

Action

Resume

Web page, with a PDF download

Read

Visit GitHub

github.com/davidgraymi

External

Visit LinkedIn

linkedin.com/in/david-gray-mi

External

Four in a row

Connect Four against my recursive thinking model. It decides how long to think, and you watch its pick change at every step.

Game

Beat the bot

Tetris against the value network from my reinforcement-learning project. Same pieces, two minutes, most lines wins.

Game

Draw a digit

Draw 0–9 and watch a weighted KNN classifier find its nearest MNIST neighbours as you go.

Game

Is it poisonous?

A real mushroom from the UCI dataset. Make your call, then watch an ID3 decision tree make its own.

Game

Route the robots

Draw a warehouse, place robots and goals, and race a LaCAM multi-agent path-finding solver to the shortest plan.

Game

Daily Wordlet

One five-letter word a day, the same for everyone. Six guesses.

Game

Bot or human?

14 rounds. One line from a public-domain classic, one written by a language model. Spot the bot.

Game

Reaction test

Wait for the panel to light up amber, then hit it as fast as you can.

Game

Lost signal

The 404 page's dodge game. Packets rain down; steer the receiver out of the way.

Game

neural-cost

A framework-neutral Python library that tells you how fast a neural network should run, not just how fast it does — FLOPs, tensor traffic and a roofline model across PyTorch, JAX and TensorFlow.

Personal

Sign Language Interpretation System

Control any smart device with American Sign Language — hand tracking, a gesture classifier and grammar correction, wired together over a client-server link.

Capstone

Bindersnap

Production policy and procedure management software for small healthcare providers. Every approved version carries a permanent, exportable record of who reviewed and signed off on it.

Product

Patient Care Portal Triage

An NLP pipeline that routes patient portal messages to the right team and surfaces relevant evidence-based research while they wait for a human.

Research

Threat modeling a zero-trust deployment pipeline

A continuous deployment pipeline for configuration-as-code and infrastructure-as-code into Kubernetes, designed from its threat model outward so the release process does the security review.

Professional

Moving three toolchain apps to Kubernetes on Azure

Three toolchain applications moved to Kubernetes on Azure, deployed one way with Helm and infrastructure-as-code, and secured with an Istio service mesh.

Professional

A processor built from the ground up

Kernel, task scheduler and communication protocol written from nothing, in C and Ada, now in production and distributed globally.

Professional

A system library that runs anywhere

One portable C++ library targeting any RTOS or bare-metal system, packaged with Docker, Bazel and GitLab CI — cutting porting work by 4×.

Professional

LaCam for Multi-Agent Path Finding

A from-scratch LaCam solver for CS440 — agents searching a space of joint configurations, each expanded by a constraint tree that rules out collisions and swaps.

School

Wordlet

A word guessing game built in Flutter and compiled to the web — playable right here, no install, no account.

Personal

Reinforcement Learning applied to Tetris

An agent that learns to play Tetris — a genuinely fun excuse to get hands-on with reward shaping.

School

Handwritten digit recognition with weighted KNN

A KNN classifier for handwritten digits, plus a k-value optimizer that collapses repeated distance calculations into a single pass.

School

ID3 mushroom classification

A from-scratch ID3 decision tree that predicts whether a mushroom is edible or poisonous from its attributes.

School

Healthcare length-of-stay estimator

A data mining project predicting how long a patient will stay, from admission-time features.

School

This website

A static site built with Astro, hosted free on GitHub Pages, designed so writing a post costs one markdown file and nothing else.

Personal

Teaching a network to think longer

My CS440 project asked whether a recursive network can stand in for search. It learned to think in steps. It did not learn when to stop. You can play it below.

Post

A roofline number should say what it doesn't know

I pointed neural-cost at my own AlphaZero chess net and got a confident 69% efficiency that was wrong three ways. Here is what it now reports instead, and the four bugs that only showed up on a real Apple GPU.

Post

Bindersnap's new look, and why most of it isn't color

Bindersnap now has a warm redesign, its own home at bindersnap.com, the app at app.bindersnap.com, and a landing page that paints in 2.4 s on a phone.

Post

An Unexpected Email, REPRO-SIGN, and 11 Citations on Google Scholar

Years after our college capstone, an out-of-the-blue email from European AI researchers wanting to reproduce our sign language paper sent me to Google Scholar, where we've been cited 11 times.

Post

Connecting the Dots: How a Sign Language Capstone Got Me Hooked on AI

Looking back at my senior capstone project at Missouri State — building a real-time American Sign Language smart home controller with MediaPipe, custom CNNs, and networking — and how it sparked my path in machine learning.

Post

GPU vs. CPU: Roofline Benchmarks on Apple Silicon MPS with neural-cost v0.2.0

The neural-cost v0.2.0 GPU benchmark moves from CPU roofline analysis to Apple Silicon's MPS GPU backend — measuring PyTorch and JAX across five architectures, four batch sizes, and both eager and compiled execution modes.

Post

PyTorch vs. JAX vs. TensorFlow: A Roofline Benchmark Across 5 Architectures

An empirical investigation comparing PyTorch 2.14, JAX 0.11, and TensorFlow across five model architectures and four batch sizes, measuring compiler speedups, kernel overheads, and hardware limits on Apple Silicon.

Post

neural-cost: Roofline Analysis Across PyTorch, JAX, and TensorFlow

A framework-neutral Python library that estimates a neural network's FLOPs and tensor traffic, measures its runtime, and uses a roofline model to tell you exactly where performance is being left on the table.

Post

I rebuilt this site so that publishing costs one file

The old site was a single 41,000-character index.html. Every change was archaeology. Here is what I replaced it with and why the constraint was maintenance, not design.

Post
↑ ↓ to move↵ to openesc to close

Terminal