Promote aux weight 1 model
Browse filesFiles changed (5)
- knot2vec.py+68-10
- model.safetensors+1-1
- results/eval.json+25-25
- results/map.png
- results/train.json+7-7
4 unmodified lines56789101112131413 unmodified lines2829303132333435369 unmodified lines464748495051212 unmodified lines26426526626726826927027127227327427575 unmodified lines35135235335435535635735811 unmodified lines3703713723733743753 unmodified lines37938038138238338438538638738815 unmodified lines4044054064074084094104114124131 unmodified line41541641741841942042142273 unmodified lines4964974984995005015025035045055065075082 unmodified lines5115125135145155164 unmodified lines# "datasets",# "huggingface-hub",# "jinja2",# "numpy",# "safetensors",# "snappy",# "torch",# ]# ///"""Knot2Vec: embed knot diagrams so that diagrams of the same knot land together."""13 unmodified linesimport datasetsimport huggingface_hub.utilsimport numpy as npimport snappyimport torchfrom database_knotinfo import link_listfrom datasets import Dataset, load_datasetfrom huggingface_hub import (9 unmodified linesREPO = "jgalego/knot2vec"DATA = "jgalego/knot2vec-diagrams"HERE = Path(__file__).parent# Progress bars redraw in place, which shows up as garbage in HF Jobs logs.PROGRESS = sys.stderr.isatty()212 unmodified linesreturn 0.5 * (1 + math.cos(math.pi * (step - warmup) / max(1, total - warmup)))def push_result(repo, name, result):"""Upload a result as results/<name>.json in the model repo."""HfApi().upload_file(path_or_fileobj=json.dumps(result, indent=1).encode(),path_in_repo=f"results/{name}.json",repo_id=repo,commit_message=f"Add {name} results",)75 unmodified lines(Path(args.output) / "README.md").unlink(missing_ok=True)if args.push:api = HfApi()api.upload_folder(folder_path=args.output, repo_id=args.repo, commit_message="Upload model")api.upload_file(path_or_fileobj=__file__, path_in_repo="knot2vec.py", repo_id=args.repo)push_result(args.repo,"train",11 unmodified lines"runtime_s": round(time.time() - start),"device": torch.cuda.get_device_name() if device == "cuda" else "cpu",},)3 unmodified linesreturn torch.cat([(queries[i : i + chunk] @ gallery.T).topk(k).indices for i in starts])def evaluate(args):"""Name held-out diagrams by nearest canonical diagram; check the invariant heads."""device = "cuda" if torch.cuda.is_available() else "cpu"model = Knot2Vec.from_pretrained(args.model).to(device).eval()knots = table(args.data, "knots", "train").to_dict()index = {n: i for i, n in enumerate(knots["name"])}gallery = model.embed(knots["pd"]).to(device)15 unmodified lines"n": int(mask.sum()),"top1": round(hits1[mask].float().mean().item(), 3),"top5": round(hits5[mask].float().mean().item(), 3),**{f"{key}_accuracy": round((out[key] == truth[key])[mask].float().mean().item(), 3)for key in ("signature", "determinant")},"volume_mae": round((out["volume"] - truth["volume"])[mask].abs().mean().item(), 3),"top1_by_crossings": {int(c): round(hits1[mask & (crossings == c)].float().mean().item(), 3)1 unmodified line},}print(json.dumps(result, indent=1))if args.push:push_result(args.repo, "eval", result)def embed(args):73 unmodified linestrain_parser.add_argument("--lr", type=float, default=3e-4)train_parser.add_argument("--warmup", type=int, default=1000)train_parser.add_argument("--temperature", type=float, default=0.05)train_parser.add_argument("--aux-weight", type=float, default=0.1)train_parser.add_argument("--dim", type=int, default=256)train_parser.add_argument("--depth", type=int, default=6)train_parser.add_argument("--max-crossings", type=int, default=64)train_parser.add_argument("--seed", type=int, default=0)train_parser.add_argument("--output", default="out/model")train_parser.add_argument("--repo", default=REPO)train_parser.add_argument("--push", action="store_true")train_parser.set_defaults(run=train)2 unmodified lineseval_parser.add_argument("--data", default=DATA)eval_parser.add_argument("--limit", type=int, default=None, help="number of test diagrams")eval_parser.add_argument("--repo", default=REPO, help="where --push stores the result")eval_parser.add_argument("--push", action="store_true")eval_parser.set_defaults(run=evaluate)4 unmodified lines567891011121314151613 unmodified lines30313233343536373839409 unmodified lines505152535455565758596061212 unmodified lines27427527627727827928028128228328428528675 unmodified lines36236336436536636736836937037137237337437537637737837938038111 unmodified lines3933943953963973983993 unmodified lines40340440540640740840941041141241341441541641741841942042142242342442542642742842943043143215 unmodified lines4484494504514524534544554564574584594601 unmodified line46246346446546646746846947047147247347447547647773 unmodified lines5515525535545555565575585595605615625635642 unmodified lines5675685695705715725735744 unmodified lines# "datasets",# "huggingface-hub",# "jinja2",# "matplotlib",# "numpy",# "safetensors",# "snappy",# "torch",# "umap-learn",# ]# ///"""Knot2Vec: embed knot diagrams so that diagrams of the same knot land together."""13 unmodified linesimport datasetsimport huggingface_hub.utilsimport matplotlib.pyplot as pltimport numpy as npimport snappyimport torchimport umapfrom database_knotinfo import link_listfrom datasets import Dataset, load_datasetfrom huggingface_hub import (9 unmodified linesREPO = "jgalego/knot2vec"DATA = "jgalego/knot2vec-diagrams"LANDMARKS = {"3_1": "trefoil","4_1": "figure-eight","10_124": "T(3,5)","11n_34": "Conway",}HERE = Path(__file__).parent# Progress bars redraw in place, which shows up as garbage in HF Jobs logs.PROGRESS = sys.stderr.isatty()212 unmodified linesreturn 0.5 * (1 + math.cos(math.pi * (step - warmup) / max(1, total - warmup)))def push_result(repo, name, result, revision=None):"""Upload a result as results/<name>.json in the model repo."""HfApi().upload_file(path_or_fileobj=json.dumps(result, indent=1).encode(),path_in_repo=f"results/{name}.json",repo_id=repo,revision=revision,commit_message=f"Add {name} results",)75 unmodified lines(Path(args.output) / "README.md").unlink(missing_ok=True)if args.push:api = HfApi()if args.revision:api.create_branch(args.repo, branch=args.revision, exist_ok=True)api.upload_folder(folder_path=args.output,repo_id=args.repo,revision=args.revision,commit_message="Upload model",)api.upload_file(path_or_fileobj=__file__,path_in_repo="knot2vec.py",repo_id=args.repo,revision=args.revision,)push_result(args.repo,"train",11 unmodified lines"runtime_s": round(time.time() - start),"device": torch.cuda.get_device_name() if device == "cuda" else "cpu",},args.revision,)3 unmodified linesreturn torch.cat([(queries[i : i + chunk] @ gallery.T).topk(k).indices for i in starts])def knot_map(gallery, knots, path):"""UMAP of the catalogue's embeddings, coloured by signature, with a few landmarks."""xy = umap.UMAP(metric="cosine", random_state=0).fit_transform(gallery.cpu().numpy())fig, ax = plt.subplots(figsize=(8, 7))points = ax.scatter(*xy.T, c=knots["signature"], cmap="Spectral", vmin=-MAX_SIGNATURE, vmax=MAX_SIGNATURE,s=2, linewidths=0,)fig.colorbar(points, ax=ax, label="Signature", shrink=0.7)for i, name in enumerate(knots["name"]):if name in LANDMARKS:ax.annotate(f"{name} {LANDMARKS[name]}", xy[i], xytext=(6, 6), textcoords="offset points",fontsize=9, arrowprops={"arrowstyle": "-", "color": "0.3"},)ax.set_axis_off()fig.savefig(path, dpi=150, bbox_inches="tight")plt.close(fig)def evaluate(args):"""Name held-out diagrams by nearest canonical diagram; check the invariant heads."""device = "cuda" if torch.cuda.is_available() else "cpu"model = Knot2Vec.from_pretrained(args.model, revision=args.revision).to(device).eval()knots = table(args.data, "knots", "train").to_dict()index = {n: i for i, n in enumerate(knots["name"])}gallery = model.embed(knots["pd"]).to(device)15 unmodified lines"n": int(mask.sum()),"top1": round(hits1[mask].float().mean().item(), 3),"top5": round(hits5[mask].float().mean().item(), 3),"signature_accuracy": round((out["signature"] == truth["signature"])[mask].float().mean().item(), 3),"determinant_within_10pct": round((abs(out["determinant"] - truth["determinant"]) <= 0.1 * truth["determinant"])[mask].float().mean().item(), 3),"volume_mae": round((out["volume"] - truth["volume"])[mask].abs().mean().item(), 3),"top1_by_crossings": {int(c): round(hits1[mask & (crossings == c)].float().mean().item(), 3)1 unmodified line},}print(json.dumps(result, indent=1))Path(args.output).mkdir(parents=True, exist_ok=True)knot_map(gallery, knots, Path(args.output) / "map.png")if args.push:push_result(args.repo, "eval", result, args.revision)HfApi().upload_file(path_or_fileobj=Path(args.output) / "map.png",path_in_repo="results/map.png",repo_id=args.repo,revision=args.revision,)def embed(args):73 unmodified linestrain_parser.add_argument("--lr", type=float, default=3e-4)train_parser.add_argument("--warmup", type=int, default=1000)train_parser.add_argument("--temperature", type=float, default=0.05)train_parser.add_argument("--aux-weight", type=float, default=1.0)train_parser.add_argument("--dim", type=int, default=256)train_parser.add_argument("--depth", type=int, default=6)train_parser.add_argument("--max-crossings", type=int, default=64)train_parser.add_argument("--seed", type=int, default=0)train_parser.add_argument("--output", default="out/model")train_parser.add_argument("--repo", default=REPO)train_parser.add_argument("--revision", default=None, help="branch to push to")train_parser.add_argument("--push", action="store_true")train_parser.set_defaults(run=train)2 unmodified lineseval_parser.add_argument("--data", default=DATA)eval_parser.add_argument("--limit", type=int, default=None, help="number of test diagrams")eval_parser.add_argument("--repo", default=REPO, help="where --push stores the result")eval_parser.add_argument("--revision", default=None, help="model branch")eval_parser.add_argument("--output", default="out/eval", help="where the knot map goes")eval_parser.add_argument("--push", action="store_true")eval_parser.set_defaults(run=evaluate)
123version https://git-lfs.github.com/spec/v1oid sha256:3c9782c590fdc438543af62d807a2ab69f7f4111a38ab8d18c8ce9ebe1acbff8size 19708148123version https://git-lfs.github.com/spec/v1oid sha256:786f8b9cb77a3b430caaa584312e3db9a5939c4f480c5b038963634b642d205asize 19708148
3 unmodified lines45678910111213141516171819202122232425262728293031323334353637383940414243443 unmodified lines"chance_top1": 7.7e-05,"seen": {"n": 46676,"top1": 0.849,"top5": 0.973,"signature_accuracy": 0.794,"determinant_within_10pct": 0.199,"volume_mae": 1.507,"top1_by_crossings": {"3": 1.0,"5": 0.75,"6": 1.0,"7": 0.714,"8": 0.764,"9": 0.835,"10": 0.76,"11": 0.879,"12": 0.809,"13": 0.858}},"unseen": {"n": 5184,"top1": 0.856,"top5": 0.969,"signature_accuracy": 0.781,"determinant_within_10pct": 0.207,"volume_mae": 1.583,"top1_by_crossings": {"4": 0.5,"5": 0.5,"6": 0.75,"8": 0.75,"9": 0.7,"10": 0.691,"11": 0.863,"12": 0.833,"13": 0.865}}}3 unmodified lines45678910111213141516171819202122232425262728293031323334353637383940414243443 unmodified lines"chance_top1": 7.7e-05,"seen": {"n": 46676,"top1": 0.881,"top5": 0.976,"signature_accuracy": 0.917,"determinant_within_10pct": 0.267,"volume_mae": 1.237,"top1_by_crossings": {"3": 1.0,"5": 0.75,"6": 1.0,"7": 0.821,"8": 0.833,"9": 0.756,"10": 0.769,"11": 0.844,"12": 0.864,"13": 0.889}},"unseen": {"n": 5184,"top1": 0.88,"top5": 0.976,"signature_accuracy": 0.891,"determinant_within_10pct": 0.248,"volume_mae": 1.39,"top1_by_crossings": {"4": 0.75,"5": 0.75,"6": 0.75,"8": 0.917,"9": 0.65,"10": 0.779,"11": 0.806,"12": 0.86,"13": 0.893}}}
12345678910111213141516171819{"step": 19999,"loss": 0.2043,"contrastive": 0.1145,"invariants": 0.8985,"pair_top1": 0.971,"minutes": 101.7,"data": "jgalego/knot2vec-diagrams","steps": 20000,"batch_size": 512,"learning_rate": 0.0003,"temperature": 0.05,"aux_weight": 0.1,"dim": 256,"depth": 6,"parameters": 4924943,"runtime_s": 6106,"device": "NVIDIA A10G"}12345678910111213141516171819{"step": 19999,"loss": 0.6226,"contrastive": 0.1426,"invariants": 0.48,"pair_top1": 0.961,"minutes": 101.8,"data": "jgalego/knot2vec-diagrams","steps": 20000,"batch_size": 512,"learning_rate": 0.0003,"temperature": 0.05,"aux_weight": 1.0,"dim": 256,"depth": 6,"parameters": 4924943,"runtime_s": 6109,"device": "NVIDIA A10G"}
knot2vec.py+68-10
model.safetensors+1-1
results/eval.json+25-25
results/map.png

Large File Pointer Details
- SHA256: 2657a24311fbf0c400401b0cb0d52133d46e2bfce1e5e429b5f80fb8c11aae99
- Pointer size: 131 Bytes
- Size of remote file: 266 kB

Large File Pointer Details
- SHA256: f2f6b79f47f5fbfcb1d57728cdca89cff4b230403a1f3cfce084206b7b8285d6
- Pointer size: 131 Bytes
- Size of remote file: 272 kB
results/train.json+7-7