Compare commits

...

11 Commits

Author SHA1 Message Date
9af36e7145 man python stinks... 2025-12-25 14:54:08 +00:00
23fddbe5b9 updated comment for PairwiseComparator 2025-12-25 14:53:35 +00:00
acbccebb2c renamed ADAMW_WiDECAY 2025-12-25 14:48:07 +00:00
edf8d46123 wut 2025-12-25 02:13:23 +00:00
4f500e8b4c write all data on training to training log 2025-12-25 02:07:49 +00:00
921e24b451 embeded # of hidden neurons in model save data
added more valves for options.
will update the console every 5 seconds when training now
2025-12-25 01:52:19 +00:00
d46712ff53 make the defaults more sane for the task
its still overkill
2025-12-25 01:23:54 +00:00
cd72cd7052 changes to figure sizes and add labels to embedding chart 2025-12-23 13:44:38 -05:00
755161c152 unused var 2025-12-23 13:29:52 -05:00
1d70935b64 general clean up & added help text
removed symbolic link calling path
2025-12-23 13:06:07 -05:00
0e2098ceec remove linked files 2025-12-23 12:30:18 -05:00
4 changed files with 120 additions and 54 deletions

View File

@@ -1 +0,0 @@
pairwise_compare.py

View File

@@ -3,7 +3,7 @@ from torch import nn
# 2) Number "embedding" network: R -> R^d
class NumberEmbedder(nn.Module):
def __init__(self, d=4, hidden=16):
def __init__(self, d=2, hidden=4):
super().__init__()
self.net = nn.Sequential(
nn.Linear(1, hidden),
@@ -14,9 +14,9 @@ class NumberEmbedder(nn.Module):
def forward(self, x):
return self.net(x)
# 3) Comparator head: takes (ea, eb, e) -> logit for "a > b"
# MLP Comparator head: takes (ea, eb, e) -> logit for "a > b"
class PairwiseComparator(nn.Module):
def __init__(self, d=4, hidden=16, k=0.5):
def __init__(self, d=2, hidden=4, k=0.5):
super().__init__()
self.log_k = nn.Parameter(torch.tensor([k]))
self.embed = NumberEmbedder(d, hidden)
@@ -29,7 +29,7 @@ class PairwiseComparator(nn.Module):
)
def forward(self, a, b):
# trying to force antisym here: h(a,b)=h(b,a)
# trying to force antisym here: h(a,b)=-h(b,a)
phi = self.head(self.embed(a-b))
phi_neg = self.head(self.embed(b-a))
logit = phi - phi_neg

View File

@@ -5,7 +5,6 @@ import random
import lzma
import torch
from torch import nn
from torch.nn import functional as F
import re
@@ -19,12 +18,14 @@ DEVICE = torch.accelerator.current_accelerator() if torch.accelerator.is_availab
# Valves
DIMENSIONS = 2
TRAIN_STEPS = 5000
HIDDEN_NEURONS = 4
ADAMW_LR = 5e-3
ADAMW_DECAY = 5e-4
TRAIN_STEPS = 2000
TRAIN_BATCHSZ = 8192
TRAIN_PROGRESS = 10
BATCH_LOWER = -100.0
BATCH_UPPER = 100.0
DO_VERBOSE_EARLY_TRAIN = False
# Files
MODEL_PATH = "./files/pwcomp.model"
@@ -32,13 +33,15 @@ LOGGING_PATH = "./files/output.log"
EMBED_CHART_PATH = "./files/embedding_chart.png"
EMBEDDINGS_DATA_PATH = "./files/embedding_data.csv"
TRAINING_LOG_PATH = "./files/training.log.xz"
LOSS_CHART_PATH = "./files/training_loss_v_step.png"
ACC_CHART_PATH = "./files/training_error_v_step.png"
# TODO: Move plotting into its own file
def parse_training_log(file_path: str) -> pd.DataFrame:
text: str = ""
with lzma.open(file_path, mode='rt') as f:
text = f.read()
pattern = re.compile(r"step=\s*(\d+)\s+loss=([0-9.]+)\s+acc=([0-9.]+)")
rows = [(int(s), float(l), float(a)) for s, l, a in pattern.findall(text)]
df = pd.DataFrame(rows, columns=["step", "loss", "acc"]).sort_values("step").reset_index(drop=True)
@@ -49,35 +52,38 @@ def parse_training_log(file_path: str) -> pd.DataFrame:
return df
# TODO: Move plotting into its own file
def plt_loss_tstep(df: pd.DataFrame) -> None:
# Plot 1: Loss
plt.figure(figsize=(8, 4))
plt.figure(figsize=(10, 6))
plt.plot(df["step"], df["loss_clamped"])
plt.yscale("log")
plt.xlabel("Step")
plt.ylabel("Loss (log scale)")
plt.title("Training Loss vs Step")
plt.tight_layout()
plt.savefig('./files/training_loss_v_step.png')
plt.savefig(LOSS_CHART_PATH)
plt.close()
return None
# TODO: Move plotting into its own file
def plt_acc_tstep(df: pd.DataFrame, eps=1e-10) -> None:
# Plot 2: Accuracy
df["err"] = (1.0 - df["acc"]).clip(lower=eps)
plt.figure(figsize=(8, 4))
plt.figure(figsize=(10, 6))
plt.plot(df["step"], df["err"])
plt.yscale("log")
plt.xlabel("Step")
plt.ylabel("Error rate (1 - accuracy) (log scale)")
plt.title("Training Error Rate vs Step")
plt.tight_layout()
plt.savefig('./files/training_error_v_step.png')
plt.savefig(ACC_CHART_PATH)
plt.close()
return None
# TODO: Move plotting into its own file
def plt_embeddings(model: comp_nn.PairwiseComparator) -> None:
import csv
@@ -95,10 +101,14 @@ def plt_embeddings(model: comp_nn.PairwiseComparator) -> None:
# move data back to CPU for plotting
embeddings = embeddings.cpu()
xs = xs.cpu()
# Plot 3: x vs h(x)
plt.figure(figsize=(10, 6))
for i in range(embeddings.shape[1]):
plt.plot(xs.squeeze(), embeddings[:, i], label=f"dim {i}")
plt.title("x vs h(x)")
plt.xlabel("x [input]")
plt.ylabel("h(x) [embedding]")
plt.legend()
plt.savefig(EMBED_CHART_PATH)
plt.close()
@@ -127,7 +137,8 @@ def set_seed(seed: int) -> None:
if torch.cuda.is_available():
torch.cuda.manual_seed_all(seed)
# Data: pairs (a, b) with label y = 1 if a > b else 0 -> (a,b,y)
# pairs (a, b) with label y = 1 if a > b else 0 -> (a,b,y)
# uses epsi to select the window in which a == b for equality training
def sample_batch(batch_size: int, low=BATCH_LOWER, high=BATCH_UPPER, epsi=1e-4) -> tuple[torch.Tensor, torch.Tensor, torch.Tensor]:
a = (high - low) * torch.rand(batch_size, 1) + low
b = (high - low) * torch.rand(batch_size, 1) + low
@@ -139,17 +150,21 @@ def sample_batch(batch_size: int, low=BATCH_LOWER, high=BATCH_UPPER, epsi=1e-4)
return a, b, y
def training_entry():
get_torch_info()
# all prng seeds to 0 for deterministic outputs durring testing
# the seed should initialized normally otherwise
set_seed(0)
model = comp_nn.PairwiseComparator(d=DIMENSIONS).to(DEVICE)
opt = torch.optim.AdamW(model.parameters(), lr=8e-4, weight_decay=1e-3)
model = comp_nn.PairwiseComparator(d=DIMENSIONS, hidden=HIDDEN_NEURONS).to(DEVICE)
opt = torch.optim.AdamW(model.parameters(), lr=ADAMW_LR, weight_decay=ADAMW_DECAY)
log.info(f"Using {TRAINING_LOG_PATH} as the logging destination for training...")
with lzma.open(TRAINING_LOG_PATH, mode='wt') as tlog:
# training loop
training_start_time = datetime.datetime.now()
last_ack = datetime.datetime.now()
for step in range(TRAIN_STEPS):
a, b, y = sample_batch(TRAIN_BATCHSZ)
a, b, y = a.to(DEVICE), b.to(DEVICE), y.to(DEVICE)
@@ -161,15 +176,17 @@ def training_entry():
loss_fn.backward()
opt.step()
if step % TRAIN_PROGRESS == 0:
with torch.no_grad():
pred = (torch.sigmoid(logits) > 0.5).float()
acc = (pred == y).float().mean().item()
tlog.write(f"step={step:5d} loss={loss_fn.item():.7f} acc={acc:.7f}\n")
with torch.no_grad():
pred = (torch.sigmoid(logits) > 0.5).float()
acc = (pred == y).float().mean().item()
tlog.write(f"step={step:5d} loss={loss_fn.item():.7f} acc={acc:.7f}\n")
# also print to normal text log occasionally to show some activity.
if step % 2500 == 0:
# also print to normal text log occasionally to show some activity.
# every 10 steps check if its been longer than 5 seconds since we've updated the user
if step % 10 == 0:
if (datetime.datetime.now() - last_ack).total_seconds() > 5:
log.info(f"still training... step={step} of {TRAIN_STEPS}")
last_ack = datetime.datetime.now()
training_end_time = datetime.datetime.now()
log.info(f"Training steps complete. Start time: {training_start_time} End time: {training_end_time}")
@@ -185,12 +202,14 @@ def training_entry():
log.info(f"Final test acc: {acc} errors: {errors}")
# embed model dimensions into the model serialization
torch.save({"state_dict": model.state_dict(), "d": DIMENSIONS}, MODEL_PATH)
torch.save({"state_dict": model.state_dict(), "d": DIMENSIONS, "h": HIDDEN_NEURONS}, MODEL_PATH)
log.info(f"Saved PyTorch Model State to {MODEL_PATH}")
def infer_entry():
get_torch_info()
model_ckpt = torch.load(MODEL_PATH, map_location=DEVICE)
model = comp_nn.PairwiseComparator(d=model_ckpt["d"]).to(DEVICE)
model = comp_nn.PairwiseComparator(d=model_ckpt["d"], hidden=model_ckpt["h"]).to(DEVICE)
model.load_state_dict(model_ckpt["state_dict"])
model.eval()
@@ -210,46 +229,95 @@ def infer_entry():
for (x, y), p in zip(pairs, probs):
log.info(f"P({x} > {y}) = {p.item():.3f}")
def graphs_entry():
get_torch_info()
model_ckpt = torch.load(MODEL_PATH, map_location=DEVICE)
model = comp_nn.PairwiseComparator(d=model_ckpt["d"], hidden=model_ckpt["h"]).to(DEVICE)
model.load_state_dict(model_ckpt["state_dict"])
model.eval()
plt_embeddings(model)
data = parse_training_log(TRAINING_LOG_PATH)
plt_loss_tstep(data)
plt_acc_tstep(data)
help_text = r"""
pairwise_compare.py — tiny pairwise "a > b?" neural comparator
USAGE
python3 pairwise_compare.py train
Train a PairwiseComparator on synthetic (a,b) pairs sampled uniformly from
[BATCH_LOWER, BATCH_UPPER]. Labels are:
1.0 if a > b + epsi
0.0 if a < b - epsi
0.5 otherwise (near-equality window)
Writes training metrics to:
./files/training.log.xz
Saves the trained model checkpoint to:
./files/pwcomp.model
python3 pairwise_compare.py infer
Load ./files/pwcomp.model and run inference on a built-in list of test pairs.
Prints probabilities as:
P(a > b) = sigmoid(model(a,b))
python3 pairwise_compare.py graphs
Load ./files/pwcomp.model and generate plots + exports:
./files/embedding_chart.png (embed(x) vs x for each embedding dimension)
./files/embedding_data.csv (x and embedding vectors)
./files/training_loss_v_step.png
./files/training_error_v_step.png (1 - acc, log scale)
Requires that ./files/training.log.xz exists (i.e., you ran "train" first).
FILES
./files/output.log General runtime log (info/errors)
./files/pwcomp.model Torch checkpoint: {"state_dict": ..., "d": DIMENSIONS}
./files/training.log.xz step/loss/acc trace used for plots
NOTES
- DEVICE is chosen via torch.accelerator if available, else CPU.
- Hyperparameters are controlled by the "Valves" constants near the top.
"""
if __name__ == '__main__':
import sys
import os
import datetime
# TODO: tidy up the paths to files and checking if the directory exists
if not os.path.exists("./files/"):
os.mkdir("./files")
log = logging.getLogger(__name__)
logging.basicConfig(filename=LOGGING_PATH, level=logging.INFO)
logging.basicConfig(level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s',
handlers=[
logging.FileHandler(LOGGING_PATH),
logging.StreamHandler(stream=sys.stdout)
])
log.info(f"Log opened {datetime.datetime.now()}")
get_torch_info()
log = logging.getLogger(__name__)
log.info(f"Log file {LOGGING_PATH} opened {datetime.datetime.now()}")
name = os.path.basename(sys.argv[0])
if name == 'train.py':
training_entry()
elif name == 'infer.py':
infer_entry()
else:
# alt call patern
# python3 pairwise_compare.py train
# python3 pairwise_compare.py infer
# python3 pairwise_compare.py graphs
if len(sys.argv) > 1:
mode = sys.argv[1].strip().lower()
if mode == "train":
# python3 pairwise_compare.py train
# python3 pairwise_compare.py infer
# python3 pairwise_compare.py graphs
if len(sys.argv) > 1:
match sys.argv[1].strip().lower():
case "train":
training_entry()
elif mode == "infer":
case "infer":
infer_entry()
elif mode == "graphs":
data = parse_training_log(TRAINING_LOG_PATH)
plt_loss_tstep(data)
plt_acc_tstep(data)
else:
case "graphs":
graphs_entry()
case "help":
log.info(help_text)
case mode:
log.error(f"Unknown operation: {mode}")
log.error("Invalid call syntax, call script as \"train.py\" or \"infer.py\" or as pairwise_compare.py <mode> where mode is \"train\" or \"infer\"")
else:
log.error("Not enough arguments passed to script; call as train.py or infer.py or as pairwise_compare.py <mode> where mode is \"train\" or \"infer\"")
log.error("valid options are one of [\"train\", \"infer\", \"graphs\", \"help\"]")
log.info(help_text)
log.info(f"Log closed {datetime.datetime.now()}")

View File

@@ -1 +0,0 @@
pairwise_compare.py