← Community Posts

I wrote a from-scratch primer for the "NNUE, distillation, backprop" jargon in my shogi-AI article

Last updated: Tue, July 7, 2026

0

I wrote up rebuilding a shogi AI for the browser in a day (posted separately as a project), and on re-reading it, the thing is packed with machine-learning terms — NNUE, distillation, backprop, quantization — used with no explanation. If you can play shogi but you're new to ML, you'll probably stall halfway.

So I wrote a primer that builds the prerequisites from zero, for someone who is "completely new to ML / can play shogi around amateur 2-dan / can read a little code." Three principles:

  • Minimal math — explain with analogies instead
  • Copy-paste-runnable Python so you can check each idea by hand
  • Don't stop at the abstract — tie every concept back to the actual shogi AI's implementation, so "this concept → where it matters in the main article" reads as one continuous line

Shogi turns out to be a great vehicle for this. ML intros are usually taught on something a beginner doesn't care about, like handwritten-digit recognition. With shogi, the "input" is a board you can already read, the "output" is just "who's winning," and every abstract idea has a concrete job in a program you can watch play. So the question that sinks most beginners — "okay, but what is this actually for?" — barely comes up.

Here's how the primer breaks down the terms from the main article. This post is a trailer, so it's just the gist; the full version, where you actually run the math and code, is linked at the end.

Neural network: a giant adder with tunable knobs

Start with the most basic building block. A neural network isn't anything mystical — it's a giant function.

What it does is surprisingly simple: take each input (for shogi, features of the board — which piece is on which square, etc.), multiply it by a number called a weight, and add them all up. That's it. Treat that "multiply and add" as one layer, and stack many of them. Between layers, insert a simple step that caps values from growing too large (an activation function). The final layer emits one number: how good the position is.

The key is the weights. There are hundreds of thousands of them, each an independent "knob." How you turn the knobs decides what output comes out of the same input. "Learning" is the process of turning those hundreds of thousands of knobs, little by little, until the output is useful.

The shogi AI's "eyes" are exactly this. Even a seemingly sophisticated judgment — "look at a board, output a number for who's ahead" — is, on the inside, a giant pile of multiply-and-add with a lot of tunable knobs. The main article's homemade net is a 4-stage "2,268 board features → 256 → 32 → 1," with about 590k knobs.

Evaluation function: the shogi AI's "brain"

Next, the evaluation function. It looks at a single position and returns one number for "which side is ahead, and by how much." In shogi this is often expressed in centipawns (cp = hundredths of a pawn). "+300cp" ≈ Black is up three pawns' worth.

Why is it the "brain"? The shogi AI tries many candidate moves a few plies deep, scores the resulting positions with this eval, and picks the line that leads to the best score. So the correctness of the reading ultimately comes down to the correctness of the eval. However deep it reads, a bad leaf score means it chooses the wrong line.

An eval can be handwritten as rules or learned from data. Handwritten ("a pawn is 100, add points if the king is safe") is clear but limited to terms a human thinks of. The climax of the main article is replacing that handwritten eval with a learned one (a neural net).

Loss and gradient descent: walking downhill blindfolded

So how do you tune a net's hundreds of thousands of knobs to "good" values? Enter loss and gradient descent.

Loss is a single number for "how far off the current output is from the correct answer." The bigger the gap between the net's answer and the truth, the bigger the loss. The goal of training is to make this loss as small as possible.

Gradient descent is how. Picture walking down a hill blindfolded. Feel the slope under your feet (which knob, turned which way and how much, lowers the loss), take one small step in the steepest downhill direction, feel again, step again — repeat tens or hundreds of thousands of times, inching toward the bottom of the valley (a good knob setting). The step size ("learning rate") matters: too big and you leap across the valley; too small and you never arrive.

Backpropagation (backprop): the engine room of learning

Gradient descent needed "the slope under your feet" — that is, for every knob, which way and how much to turn it to reduce the loss. With hundreds of thousands of knobs, how do you get all of them? Poking them one at a time is hopeless.

The thing that gets them all at once is backpropagation (backprop). The mechanism: work backward from the output to the input, using the chain rule from calculus, distributing the "blame" for the error back through the layers. "The final error was caused this much by this knob in the last layer, that much by that knob in the layer before" — every knob's contribution, computed back-to-front in one sweep. Hundreds of thousands of slopes, all from a single backward pass.

This is the engine room of learning — and also exactly the part you never want to hand-code (the formulas explode across layers). Which is why the next tool exists.

PyTorch: it does all the tedious calculus for you

The whole loop — loss → gradient → backprop → step — is fully automated by PyTorch, a Python library from Facebook (now Meta) for building and training neural nets.

You just describe "the network looks like this" and "measure the loss this way," and PyTorch handles the tedious differentiation (backprop), the knob updates, and the bookkeeping. The best part is autograd — you only write the forward computation (multiply, add, output), and PyTorch automatically derives the backward slope calculation for you. The main article's training code is written in it.

In the primer, you actually train a tiny neural net in a few lines of PyTorch and watch the loss number drop with your own eyes. That's where "loss," "gradient descent," and "backprop" turn from abstractions into something running in front of you.

Distillation: copying a capable teacher's answers

Here's the concept that leads straight into the main article's core. Distillation is the technique of having a strong, heavy teacher solve a huge number of problems, then training a small, light student to imitate its answers.

In the main article, the teacher is the strong engine YaneuraOu and the student is a homemade small neural net. YaneuraOu is very strong but heavy — it can't run in a browser. But as a "teacher who labels a ton of positions with the right answer," it's ideal. So you have it score millions of positions, and train the student to reproduce those scores.

Why copy the teacher's answers instead of studying from scratch? Because it's far faster and produces a stronger student. Just like copying a capable person's worked solutions gets you the knack faster than studying only from a textbook. Distilling the teacher's judgment down to something small enough to fit in a browser is the central trick that moves a heavy engine's strength into a light net.

Quantization: rounding decimals to integers to go fast

Fresh out of training, a net's weights are fine-grained decimals (floats). Used as-is, the computation is heavy for a browser or phone. So you apply quantizationrounding the decimals to integers.

For example, approximate a weight of "0.5732…" by some integer. Now the multiplies and adds are integer arithmetic, and the computation gets much lighter and faster. You lose a little precision in the rounding — but you trade that precision for practical speed. It's the quiet-but-effective last touch that lets an eval run fast enough to actually play on a weak device. Think of it as the final polish right before deployment.

Accumulator and NNUE: where everything converges

Finally, the place where all the concepts so far come together — NNUE.

NNUE is an evaluation format originally invented for shogi (chess's strong engine Stockfish imported it later). Its key insight: one move changes the board only a little. Basically a single piece moves. So recomputing the entire network from scratch every time is a huge waste.

Instead, NNUE updates only the part that changed (the delta). "Remember the previous position's computed result, and just add/subtract the effect of the one piece that moved." The state that holds that "remembered" value is called the accumulator. Thanks to this incremental trick, a neural eval — normally heavy — stays fast enough to survive a search over millions of positions.

This is where everything converges: neural network (multiply and add), evaluation function (position → number), distillation (learn from a teacher), and quantization (integers for speed). By the time "NNUE" shows up in the main article, you should be able to read it as "the shogi-born, incrementally-updated, distilled small eval net."


The consistent goal: even if you skip the math, the analogies and the runnable code should connect "concept → where it matters in the main article" in a single line. Read this primer first, then the main article, and you should get through it without tripping on the jargon. Whether you want to peek at ML through shogi, or peek at a shogi AI from the ML side.

👉 Full primer (with math and code): https://www.meetyudai.com/blog/applied-algorithms/H9beqVVY7h5qc3hNfskR 👉 Main article (rebuilding a shogi AI in a day): https://www.meetyudai.com/blog/applied-algorithms/9fpLthtYgHvnImuFR0Jf

Comments (0)

No comments yet.

You need to log in to comment.

Log in with Google