Download tasks/bibliographic_archive/solution/reference/checkpoint.py from Tokenwave-AI/REE: direct link, hf CLI and curl.
- Browser
- Download file 1.99 kB
-
https://huggingface.co/datasets/Tokenwave-AI/REE/resolve/main/tasks/bibliographic_archive/solution/reference/checkpoint.py
- Command line
-
hf download hf://datasets/Tokenwave-AI/REE/tasks/bibliographic_archive/solution/reference/checkpoint.py
-
curl -L -o checkpoint.py https://huggingface.co/datasets/Tokenwave-AI/REE/resolve/main/tasks/bibliographic_archive/solution/reference/checkpoint.py
1.99 kB
| """Self-contained, pickle-free SIGN checkpoints; no graph identities stored.""" | |
| from __future__ import annotations | |
| import json | |
| import os | |
| from pathlib import Path | |
| import numpy as np | |
| from sign_numpy import SIGN | |
| from training import HOPS, HIDDEN, LAYERS, DROPOUT, INPUT_DROPOUT | |
| def save(path, model, input_width, classes, training): | |
| path = Path(path) | |
| path.parent.mkdir(parents=True, exist_ok=True) | |
| metadata = { | |
| "format": 1, "input_width": input_width, "classes": classes, | |
| "hops": HOPS, "hidden": HIDDEN, "layers": LAYERS, | |
| "dropout": DROPOUT, "input_dropout": INPUT_DROPOUT, | |
| "training": training, | |
| } | |
| temporary = path.with_suffix(".tmp.npz") | |
| with temporary.open("wb") as stream: | |
| np.savez(stream, __metadata__=np.array(json.dumps(metadata)), | |
| **model.state_dict()) | |
| os.replace(temporary, path) | |
| def load(path): | |
| path = Path(path) | |
| if not path.is_file(): | |
| raise FileNotFoundError(f"Missing SIGN checkpoint: {path}. Run train.sh first.") | |
| with np.load(path, allow_pickle=False) as archive: | |
| metadata = json.loads(str(archive["__metadata__"])) | |
| if metadata["format"] != 1: | |
| raise ValueError("Unsupported SIGN checkpoint format") | |
| model = SIGN(metadata["input_width"], metadata["hidden"], | |
| metadata["classes"], metadata["hops"] + 1, | |
| metadata["layers"], metadata["dropout"], | |
| metadata["input_dropout"]) | |
| expected = {name for name, _ in model.named_parameters()} | |
| if set(archive.files) != expected | {"__metadata__"}: | |
| raise ValueError("Incomplete SIGN checkpoint") | |
| for name, parameter in model.named_parameters(): | |
| value = archive[name] | |
| if value.shape != parameter.value.shape or not np.all(np.isfinite(value)): | |
| raise ValueError(f"Invalid SIGN parameter: {name}") | |
| parameter.value[:] = value | |
| return model, metadata | |