File size: 5,932 Bytes
5bcfbd1 82129c9 5bcfbd1 82129c9 5bcfbd1 82129c9 5bcfbd1 82129c9 5bcfbd1 82129c9 5bcfbd1 82129c9 5bcfbd1 82129c9 5bcfbd1 82129c9 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 | ---
license: cc-by-nc-4.0
tags:
- arithmetic
- threshold-network
- hand-crafted-weights
- deepmind-mathematics
- pytorch
library_name: pytorch
---
# LeftBrain b20: hand-written threshold network for DeepMind arithmetic (offline, 0 wrong answers)
**LeftBrain b20** is a single PyTorch network that reads seven kinds of [DeepMind Mathematics](https://github.com/google-deepmind/mathematics_dataset) arithmetic questions as raw characters and writes the answer as characters.
- **Nothing was trained.** Every weight was written by hand as a threshold unit, `y = W2 · H(W1·x + b1)`, where H is the step function.
- **No calculator is called.** Python never computes the answer. The network runs inside one fixed-length loop, and every step is a matrix multiply or a 0/1 gate.
- **It refuses instead of guessing.** If an input is outside what the network was built for, it outputs all zeros, which means "I refuse."
## Results (v4, 2026-11-30)
These are the official DeepMind `train-easy` problems, 200 per module and 1,400 in total. Each answer was compared with our reference pipeline and the official answer string.
| Module | Same as reference | Wrong | Refused |
|---|---|---|---|
| arithmetic__div | 200 | 0 | 0 |
| arithmetic__mul | 200 | 0 | 0 |
| numbers__div_remainder | 200 | 0 | 0 |
| numbers__gcd | 200 | 0 | 0 |
| arithmetic__mixed | 200 | 0 | 0 |
| algebra__linear_1d | 200 | 0 | 0 |
| arithmetic__add_or_sub | 200 | 0 | 0 |
| **Total** | **1,400** | **0** | **0** |
New in v4: add / subtract now go through a hand-wired **segment core** (6-bit segments, the same weights as our big-number pipeline) with 32-bit input registers, so the 23 add/subtract problems that v3 refused (numbers wider than 20 bits) are answered. v3 was 1,377 same / 0 wrong / 23 refused.
Other checks:
| Check | Result |
|---|---|
| Exhaustive tests at 6-bit width | 122,611 cases (base modules, incl. big-number samples) and 30,013 cases (expression stack machine), all match the specification. Expected refusals are counted as refusals, not as correct answers. |
| Random tests at 20-bit width | 3,741 cases (1,376 of them big add/subtract up to 2^33) + 990 stack-machine cases, all match. A sample, not exhaustive. |
| Negative controls | Every deliberately broken copy of the network is caught by the tests (e.g. carry removed from the binary-to-decimal layer: 53 of 64 caught). |
| Isolated run | A fresh process in an empty folder, holding only `leftbrain_b20.pt` and `left_brain_rt.py`, with no network access, goes from English question to answer and gives identical output on 240 problems. A clean-folder rerun of all 1,400 official problems with `python -I`: 1,400 correct, 0 wrong, 0 refused. |
Full numbers are in `results.json`. Its field names are in Chinese.
## Usage
The only requirement is `torch`. Nothing else from our project is needed.
```python
import left_brain_rt as RT
m = RT.LeftBrainRT.load("leftbrain_b20.pt")
print(m.answer([
"Divide 33 by -225.", # -11/75
"Calculate (1 - 3) + 14/6.", # 1/3
"Solve -38*z - 300 = 42 for z.", # -9
"What is the remainder when 62 is divided by 8?", # 6
]))
```
An empty string means the network refused. You can also run it from the command line:
```
python left_brain_rt.py leftbrain_b20.pt "Divide 33 by -225."
```
## How it works
1. **Reader.** A finite-state machine, written as weights, reads the question one character at a time. It recognises each sentence pattern and builds numbers digit by digit.
2. **Step machine.** This part does exact rational arithmetic (add, subtract, multiply, divide, and reduce a fraction) and gcd. It works on 20-bit registers.
3. **Expression stack machine** (for mixed expressions and linear equations). This is a shunting-yard parser with 7 operator slots and 5 value slots.
- A linear equation is solved by evaluating f(0) and f(1) in two lanes, then computing x = f0 / (f0 − f1).
- Equations where both sides of a multiplication contain the variable, or where something is divided by the variable, are refused.
4. **Output layer.** It writes integers, fractions (p/q), or decimals as character codes.
5. **Segment core** (add / subtract). One fixed network over 7 segments of 6 bits, stepped inside the same loop; it replaces the 20-bit fraction machine for these four question types.
Up to 14,385 loop steps; the loop stops early once every question is settled. The weights must be stored as float64, because the largest intermediate sum is about 7.5e13. Unit and non-zero counts are in `results.json` (field `規模`).
## Limits
- It handles only the 7 modules above, and only their fixed DeepMind sentence patterns. Changes in capital letters or spacing may cause it to refuse.
- Add/subtract numbers wider than 32 bits, and numbers wider than 20 bits in the other modules, are refused.
- It does not read natural-language word problems such as GSM8K. That is a separate part of the project and is not included here.
## 中文說明
這是一顆手搓門檻網路:權重全部手寫,沒有經過訓練,計算不呼叫外部的 Python 計算器。
- 輸入英文題目的字元,就輸出答案的字元。
- 它做 DeepMind 官方 7 類算術題:除、乘、餘數、gcd、混合運算、一元一次、加減。
- 1,400 題全部跟參考答案相同,答錯 0 題、拒答 0 題(v4:加減改走段核心,v3 拒答的 23 題大數加減現在會答)。
- 執行只需要 torch,斷網也能跑。
The same model is also in `Bigbearbo/handwired-left-brain-1` (tag `v0.5`, folder `combined/`), next to the pipeline it was checked against.
## License
CC BY-NC 4.0 (non-commercial) for the code and the weights. The test questions come from the Apache-2.0 licensed DeepMind mathematics_dataset generator.
Checksum: `leftbrain_b20.pt` sha256 `46270d6d8aa78bfc3a58aa0bdfcf601db16bf128c6175b44315bfc46fafaaf87` (v3 was `96ad14b3…`)
|