BooleanGPT

A tiny decoder-only GPT transformer built from scratch in PyTorch for Boolean logic evaluation.

Built for the Machine Learning module (CS7CS4) of the M.Sc. in Computer Science (Intelligent Systems), Trinity College Dublin, 2025/26.

Model details

Property Value
Parameters 0.34 M
Architecture 3-layer 路 4-head 路 96-dim 路 GELU feed-forward
Context window 48 characters
Vocab 19 characters (True, False, AND, OR, NOT, XOR, NAND, NOR, (, ), =, space, \n)
Tokeniser Character-level
Exact-match accuracy 98 % on held-out test set

What it does

Given a Boolean expression (e.g. (True XOR True) AND False=), the model generates True or False one character at a time. Supports AND, OR, NOT, XOR, NAND, NOR, XNOR.

Usage

import torch
import torch.nn as nn
import torch.nn.functional as F

# --- paste the model class definition here (see companion repo) ---

BOOL_CHARS = ['\n',' ','(',')',  '=','A','D','F','N','O','R','T','X','a','e','l','r','s','u']
stoi = {c: i for i, c in enumerate(BOOL_CHARS)}
itos = {i: c for i, c in enumerate(BOOL_CHARS)}

model = GPTLanguageModel(vocab_size=19, n_embd=96, n_head=4, n_layer=3, block_size=48, dropout=0.1, gelu=True)
model.load_state_dict(torch.load("booleangpt.pth", map_location="cpu"))
model.eval()

expression = "(True XOR True) AND False="
ctx = torch.tensor([[stoi[c] for c in expression]], dtype=torch.long)
with torch.no_grad():
    out = model.generate(ctx, max_new_tokens=6)
result = "".join([itos[i] for i in out[0].tolist()]).split("\n")[0]
print(result)  # (True XOR True) AND False=False

Training

Trained for 6,000 iterations with AdamW (lr=1e-3) on 40,000 Boolean logic expressions. Validated every 500 steps. Accuracy evaluated by exact-match on 500 held-out test expressions.

Companion

Downloads last month

-

Downloads are not tracked for this model. How to track
Inference Providers NEW
This model isn't deployed by any Inference Provider. 馃檵 Ask for provider support