Download run_hf_self_train.py from LIJINGHAI111/symbolic_regression_DeepSets_LSTM_Generator: direct link, hf CLI and curl.
- Browser
- Download file 7.88 kB
-
https://huggingface.co/LIJINGHAI111/symbolic_regression_DeepSets_LSTM_Generator/resolve/main/run_hf_self_train.py
- Command line
-
hf download hf://LIJINGHAI111/symbolic_regression_DeepSets_LSTM_Generator/run_hf_self_train.py
-
curl -L -o run_hf_self_train.py https://huggingface.co/LIJINGHAI111/symbolic_regression_DeepSets_LSTM_Generator/resolve/main/run_hf_self_train.py
7.88 kB
| """run_hf_self_train —— 用 HuggingFace 风格模型做推理 + 主动自我训练示例。 | |
| 演示三件事: | |
| 1. 把已有根目录 checkpoint 转成 HF 格式(convert_original_checkpoint)。 | |
| 2. 用 HF 模型直接推理:给定 (x, y) 点集,generate() 出候选表达式。 | |
| 3. 用 HF 模型做主动自我训练:输入用户 x/y 向量 → 拆 train/test → | |
| 自我评估 → 高 R² 提前停止。 | |
| 用法: | |
| # 第 1 步:先把预训练 checkpoint 转成 HF 目录(只会做一次) | |
| python huggingface/convert_original_checkpoint.py | |
| # 推理 demo(自动 try 转换) | |
| python huggingface/run_hf_self_train.py demo | |
| # 自我训练:喂入用户(x, y)(这里放了一个 sin 示例) | |
| python huggingface/run_hf_self_train.py selftrain --x "0.1,0.6,1.1,1.6,2.1,2.6,3.0" \ | |
| --y "0.60,1.07,1.34,1.40,1.27,0.96,0.64" --rounds 15 --candidates 96 | |
| 注意: | |
| 工程根目录 config.py / vocab.py / self_train.py 是共享依赖, | |
| 本脚本会自动把它们所在目录加入 sys.path,因此可从任意位置运行。 | |
| """ | |
| from __future__ import annotations | |
| import argparse | |
| import os | |
| import sys | |
| if hasattr(sys.stdout, "reconfigure"): | |
| sys.stdout.reconfigure(line_buffering=True) | |
| # ------------------------------------------------------------------ # | |
| # 方案 B 自包含路径引导: | |
| # 1) 本脚本所在目录(huggingface/)加入 sys.path —— 让平铺依赖 | |
| # config.py / vocab.py / model.py / self_train.py 可从任意 cwd 找到 | |
| # 2) 父目录也加入 —— 让 `huggingface` 这个包名可从任意 cwd 解析 | |
| # ------------------------------------------------------------------ # | |
| _HERE = os.path.dirname(os.path.abspath(__file__)) | |
| _PARENT = os.path.dirname(_HERE) | |
| for _p in (_HERE, _PARENT): | |
| if _p not in sys.path: | |
| sys.path.insert(0, _p) | |
| import numpy as np | |
| import torch | |
| import config as default_config | |
| from tokenization_symbolic_regression import SymbolicRegressionTokenizer | |
| from modeling_symbolic_regression import SymbolicRegressionModel | |
| HF_DIR = os.path.join(default_config.CHECKPOINT_DIR, "hf_symbolic_regression") | |
| ROOT_CKPT = os.path.join(default_config.CHECKPOINT_DIR, "model_final.pt") | |
| # ---------------------------------------------------------------------- # | |
| # 1. 模型加载:优先 from_pretrained 远程仓库,回退到本地 HF 权重目录 | |
| # ---------------------------------------------------------------------- # | |
| HF_REPO = "LIJINGHAI111/symbolic_regression_DeepSets_LSTM_Generator" | |
| def download_remote_model(): | |
| """本地无权重时,从 HuggingFace 远程仓库下载三件套到包本地目录。""" | |
| local_dir = os.path.join(_HERE, "hf_symbolic_regression") | |
| os.makedirs(local_dir, exist_ok=True) | |
| try: | |
| from huggingface_hub import hf_hub_download | |
| except ImportError: | |
| raise RuntimeError( | |
| "本地无 HF 权重且未安装 huggingface_hub。" | |
| "请先 `pip install huggingface_hub`,或补全本地权重目录。" | |
| ) | |
| print(f"[setup] 从远程仓库下载权重: {HF_REPO}") | |
| for f in ("config.json", "pytorch_model.bin", "tokenizer.json"): | |
| hf_hub_download(repo_id=HF_REPO, filename=f, local_dir=local_dir, force_download=True) | |
| return local_dir | |
| def ensure_hf_model(force_rebuild: bool = False): | |
| """返回可供 from_pretrained() 加载的目录或仓库 id。 | |
| 策略(依次尝试,命中即返回): | |
| 1) huggingface/ 包本地 HF 权重目录 _HERE/hf_symbolic_regression | |
| 2) 工程根目录的 HF 权重目录 _PARENT/hf_symbolic_regression | |
| 3) 回退到 HuggingFace 远程仓库 id(需联网) | |
| """ | |
| candidates = [ | |
| os.path.join(_HERE, "hf_symbolic_regression"), | |
| os.path.join(_PARENT, "hf_symbolic_regression"), | |
| HF_DIR, | |
| ] | |
| for c in candidates: | |
| if c and os.path.exists(os.path.join(c, "pytorch_model.bin")): | |
| return c | |
| # 回退:远程仓库下载到包本地,之后按本地目录加载 | |
| return download_remote_model() | |
| # ---------------------------------------------------------------------- # | |
| # 2. 直接推理 demo:给出一堆点集,让模型生成候选表达式 | |
| # ---------------------------------------------------------------------- # | |
| def demo_inference(): | |
| hf_dir = ensure_hf_model() | |
| tok = SymbolicRegressionTokenizer() | |
| model = SymbolicRegressionModel.from_pretrained(hf_dir, tokenizer=tok) | |
| dc = model.config | |
| print(f"[demo] 已从 {hf_dir} 加载 HF 模型 | 词表 {dc.vocab_size}," | |
| f"隐维 {dc.lstm_hidden_dim},D_MAX={dc.d_max}") | |
| # 用 y ≈ sin(x) 的采样点喂入,看模型能否"猜"出表达式族 | |
| x = np.linspace(0.1, 3.0, 30) | |
| y = np.sin(x) + 0.2 | |
| N = len(x) | |
| X = np.zeros((1, N, dc.d_max), dtype=np.float32) | |
| X[0, :, 0] = x | |
| yt = torch.from_numpy(y.astype(np.float32).reshape(1, N)) | |
| n_samples = torch.tensor([N], dtype=torch.long) | |
| Xt = torch.from_numpy(X) | |
| exprs = model.generate(Xt, yt, n_samples, temperature=1.5, | |
| num_return_sequences=5, do_sample=True) | |
| print("\n[demo] 模型生成的候选表达式:") | |
| for e in exprs: | |
| print(" •", e) | |
| # ---------------------------------------------------------------------- # | |
| # 3. 主动自我训练(核心) | |
| # ---------------------------------------------------------------------- # | |
| def run_selftrain(x_str: str, y_str: str, rounds: int, candidates: int, | |
| split: float = 0.7, seed: int = 456): | |
| hf_dir = ensure_hf_model() | |
| tok = SymbolicRegressionTokenizer() | |
| model = SymbolicRegressionModel.from_pretrained(hf_dir, tokenizer=tok) | |
| device = "cuda" if torch.cuda.is_available() else "cpu" | |
| model = model.to(device) | |
| def _parse(text: str) -> np.ndarray: | |
| parts = [p for p in text.replace(";", " ").replace(",", " ").split() if p] | |
| return np.array([float(p) for p in parts]) | |
| X = _parse(x_str).reshape(-1, 1) # 单变量示例 => [N, 1] | |
| y = _parse(y_str) | |
| if X.shape[0] != y.shape[0]: | |
| raise ValueError(f"x/y 长度不一致: x={X.shape[0]}, y={y.shape[0]}") | |
| from self_train import self_train_on_user_data | |
| print(f"[self-train] 数据 N={X.shape[0]}, D={X.shape[1]} → 拆 train/{split}·test/{1 - split}") | |
| result = self_train_on_user_data( | |
| model, X, y, device, | |
| split=split, max_rounds=rounds, candidates=candidates, seed=seed, verbose=True, | |
| ) | |
| print("\n" + "=" * 60) | |
| print(" 自我训练完成!") | |
| print(f" 最佳表达式 : {result['best_expr']}") | |
| print(f" 最佳测试 R² : {result['best_r2']:.6f}") | |
| print(f" 是否收敛 : {'是' if result['converged'] else '否(达到最大轮数)'}") | |
| print(f" 实际轮数 : {result['rounds']}") | |
| print("=" * 60) | |
| def main(): | |
| p = argparse.ArgumentParser(description="HF 符号回归模型:推理 demo + 自我训练") | |
| sub = p.add_subparsers(dest="cmd") | |
| pc = sub.add_parser("demo", help="显示一次推理候选") | |
| pc.set_defaults(func=lambda a: demo_inference()) | |
| ps = sub.add_parser("selftrain", help="对用户 (x,y) 做主动自我训练") | |
| ps.add_argument("--x", required=True, help="x 向量,逗号/空格分隔") | |
| ps.add_argument("--y", required=True, help="y 向量,逗号/空格分隔") | |
| ps.add_argument("--rounds", type=int, default=15) | |
| ps.add_argument("--candidates", type=int, default=96) | |
| ps.add_argument("--split", type=float, default=0.7) | |
| ps.add_argument("--seed", type=int, default=456) | |
| ps.set_defaults(func=lambda a: run_selftrain(a.x, a.y, a.rounds, | |
| a.candidates, a.split, a.seed)) | |
| args = p.parse_args() | |
| if not args.cmd: | |
| p.print_help() | |
| return | |
| args.func(args) | |
| if __name__ == "__main__": | |
| main() | |