TensorCode

Program your AI. Train your software.

def route(text):
    if "refund" in text: return "billing"
    if "crash" in text:  return "technical"
    return "unknown"

  1. give me my money backunknown
  2. the app crashes on logintechnical
  3. forgot my password againunknown

Written code and trained models in one program, with a line between them you can move.

Docs GitHub ↗

Write what you know.

Train what you don’t.

01 — Live

Written and trained, in one program.

Both run TensorCode in this tab, with the same results as the Python beside them.

Rules

Rules it can’t break.

A learned score ranks the options. A written mask decides which options exist.

before

action = model.predict(incident)
if action == "restart" and peak_hours:
    action = model.second_choice(incident)   # also allowed? check again…
if action == "rollback" and deploy_age > 60:
    action = ...                             # patch, retrain, hope

with TensorCode · click a rule to switch it off

allowed = {
    "restart":  not peak_hours,             # never at peak
    "rollback": minutes_since_deploy < 60,  # recent deploys only
    "scale":    budget_left > 0,            # it costs money
    "page":     True,                       # a person, always
}
mask = torch.tensor([allowed[a] for a in ACTIONS])
options = CandidateSet(encode(incident),
                       Latent(actions.tensor, space, mask=mask), ACTIONS)
choice = decide(score(options)).identity    # never a masked action

rules.py · trained on 8 postmortems

Incident

    Bars: the learned score. Struck through: masked by a rule. Changing a rule changes the choice at once, with no retraining.

    Mechanics

    Write the physics. Learn the aim.

    The model never sees a correct angle. Its gradient comes back through code you wrote.

    before

    # No closed-form inverse once there's drag. So either:
    #   derive one by hand, per model of the world, or
    #   simulate thousands of launches, log (distance, angle),
    #   fit a regressor, and hope it covers your targets.

    with TensorCode

    class Flight(Operation):             # written: Euler steps, air drag
        replayable = True                # pure tensor code: gradients pass
        def forward(self, aim, *, context=None):
            angle = pi / 2 * sigmoid(aim)    # rule: 0° to 90°
            ...                              # 12 s of x, y, vx, vy
            return landed
    
    aim = Decode({"architecture": "mlp", "output_dimensions": 1, ...})
    
    with trace() as t:
        landed = fly(aim(targets))
    t.supervise(landed, targets, loss="mse", source="the targets themselves")

    physics.py · no angle labels anywhere

    gravity

    Drag the ▼ target anywhere. The aim was never trained on it. Switch gravity and the same aim retrains to the new physics, still with no labels.

    02 — Robots

    Some skills are easier to write.

    A written controller placed 23 of 24 parcels. Seven policies trained on its demonstrations placed none.

    Unitree G1 with Dex3-1 hands in MuJoCo, rendered in Cycles; the scenery is not simulated. Every run ↗

    03 — Start

    A trained model is just another function.

    Call it, branch on it, wrap it in rules. Then teach it through the values your program produces.

    from tensorcode import trace, training
    from tensorcode.ops.vec import Classify
    from tensorcode.ops.vec.encode import VocabularyEncoder
    
    tickets = ["refund my order", "charged twice", "the app crashed",
               "error on upload", "forgot my password", "cannot log in"]
    labels = ["billing", "billing", "technical", "technical", "account", "account"]
    words = sorted({w for t in tickets for w in t.split()})
    
    space = {"name": "tickets", "dimensions": 8}
    encode = VocabularyEncoder({"vocabulary": words, "dimensions": 8,
                                "output_space": space})
    route = Classify({"architecture": "linear", "input_space": space,
                      "labels": ["billing", "technical", "account"]})
    
    with trace() as t:
        guess = route(encode(tickets))
    t.supervise(guess, labels, source="me")
    trainer = training.Trainer.from_ops({"encode": encode, "route": route}, lr=0.5)
    trainer.fit([t], epochs=50)
    
    print(route(encode("the upload crashed")).value)  # technical
    $ python hello.py

    hello.py