Spoken-English math to Typst with a small from-scratch encoder-decoder Transformer, following the build guide: compile-checked synthetic data generator, shared BPE tokenizer, nn.Transformer model, training loop with an overfit sanity check, greedy decoding CLI, and evaluation on synthetic and hand-written test sets. ./run.sh runs the whole pipeline.
18 lines
718 B
Python
18 lines
718 B
Python
import random
|
|
|
|
from isotope_typst.generate import TreeGen
|
|
from isotope_typst.render import to_english, to_typst
|
|
from isotope_typst.tokenizer import BOS, EOS, PAD, train
|
|
|
|
|
|
def test_round_trip(tmp_path):
|
|
rng = random.Random(0)
|
|
gen = TreeGen(rng)
|
|
trees = [gen.tree() for _ in range(2000)]
|
|
texts = [s for t in trees for s in (to_english(t, rng), to_typst(t))]
|
|
tok = train(texts, str(tmp_path / "tok.json"), vocab=1000)
|
|
assert (tok.token_to_id("[PAD]"), tok.token_to_id("[BOS]"), tok.token_to_id("[EOS]")) == (PAD, BOS, EOS)
|
|
for t in trees[:1000]: # guide §5: decode(encode(s)) == s, or nothing downstream can be right
|
|
s = to_typst(t)
|
|
assert tok.decode(tok.encode(s).ids) == s
|