跳到主内容
@wquguru
精选88MarkTechPost(RSS)技巧与观点

NVIDIA结构化数据模型零训练表格预测实操指南

How to Make Zero-Training Tabular Predictions with NVIDIA Structured Data Models

原文
发到 X
推荐理由

做表格预测的同学必看,这篇手把手教你用NVIDIA新库实现零训练推理,避坑细节很全,直接照做就能跑通。

In this tutorial, we implement Structured Data Models, NVIDIA’s new library that puts several pretrained foundation models for tables behind one PyTorch interface, and use it to make predictions without training. The library wraps TabICLv2 and KumoTabular for single tables and KumoRelational for whole relational databases, and each of them answers by in-context learning: we hand the model a set of labeled rows and the rows we want predicted, and it returns probabilities or a full predictive distribution in one forward pass. We start with the library’s typed table tensors, compare the two tabular models against trained scikit-learn baselines, reuse an encoded context across many queries, read calibrated prediction intervals off a regression, and scale to twenty classes. We then look at the places where the defaults quietly do the wrong thing: a preprocessing recipe that drops timestamp and text columns and an explainer with two traps in it, before building a small three-table shop database and predicting customer activity from it directly, including the one argument that separates an honest relational model from a leaky one. Check out the Full CODES here.

在本教程中,我们实现 Structured Data Models,这是 NVIDIA 的新库,它将多个用于表格的预训练基础模型置于一个 PyTorch 接口之后,并用于无需训练即可进行预测。该库封装了 TabICLv2 和 KumoTabular(用于单表)以及 KumoRelational(用于整个关系数据库),它们均通过上下文学习(in-context learning)给出答案:我们将一组带标签的行和我们希望预测的行交给模型,它会在一次前向传播中返回概率或完整的预测分布。我们从库的类型化表张量开始,将两个表格模型与经过训练的 scikit-learn 基线进行比较,在多次查询中重用编码上下文,从回归结果中读取校准后的预测区间,并扩展到二十个类别。然后,我们观察默认设置悄然出错的地方:一个会丢弃时间戳和文本列的预处理配方,以及一个包含两个陷阱的解释器,接着构建一个小型三表商店数据库,并直接从中预测客户活动,包括区分诚实的关系模型与存在数据泄露模型的唯一论点。在此查看完整代码。

代码 · 61 行
import os
import sys
import time
import textwrap
import traceback
import subprocess
RESULTS = {}
def banner(title):
    print("\n" + "=" * 78)
    print(title)
    print("=" * 78)
def section(name):
    def wrap(fn):
        def run(*a, **kw):
            banner(name)
            try:
                out = fn(*a, **kw)
                RESULTS[name] = out if isinstance(out, str) else "ok"
                return out
            except Exception as e:
                RESULTS[name] = f"SKIPPED / FAILED -> {type(e).__name__}: {e}"
                print(f"\n[!] {name} did not complete: {type(e).__name__}: {e}")
                traceback.print_exc(limit=3)
                return None
        return run
    return wrap
banner("1. Install SDM, and the one wheel URL its own error message gets wrong")
subprocess.run([sys.executable, "-m", "pip", "install", "-q", "git+https://github.com/NVIDIA/structured-data-models.git@b982fbf4188ea8db17249fe81338723106100b63"], check=True)
import torch
# The relational sampler needs pyg-lib on CPU. Its wheels are published per MINOR torch version
# (torch-2.14.0+cpu.html), but SDM's own ImportError builds the URL from the exact patch version
# (torch-2.14.1+cpu.html), which is an empty page. Build it from MAJOR.MINOR.0 instead.
major, minor = torch.__version__.split("+")[0].split(".")[:2]
flavour = "cpu" if torch.version.cuda is None else "cu" + torch.version.cuda.replace(".", "")
PYG_INDEX = f"https://data.pyg.org/whl/torch-{major}.{minor}.0+{flavour}.html"
subprocess.run([sys.executable, "-m", "pip", "install", "-q", "pyg-lib", "-f", PYG_INDEX], check=True)
import numpy as np
import pandas as pd
import pyg_lib
import sdm
import sdm.processing as sp
from sklearn.datasets import load_breast_cancer, load_diabetes, make_classification
from sklearn.ensemble import HistGradientBoostingClassifier, HistGradientBoostingRegressor
from sklearn.linear_model import LogisticRegression, RidgeCV
from sklearn.metrics import roc_auc_score
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
DEVICE = "cpu"   # verified end to end on CPU; on a T4 runtime set "cuda" for the tabular models
print(f"  sdm {sdm.__version__}  |  torch {torch.__version__}  |  pyg-lib {pyg_lib.__version__}"
      f"  |  device {DEVICE}")
print(f"  pyg-lib wheels from {PYG_INDEX}")
print("\n  One interface over several pretrained structured-data foundation models:")
for name, what, weights in [
    ("TabICLv2", "tabular, in-context learning (ICML '26)", "~110 MB per task head, BSD-3"),
    ("KumoTabular", "tabular, small / medium / large", "small ~110 MB, large ~855 MB, OpenMDW"),
    ("KumoRelational", "multi-table, KumoRFM-2", "~120 MB, OpenMDW"),
    ("TabFM, TimesFM 3", "Google tabular / time series", "6.5 GB / 1.3 GB, non-commercial: not used here"),
]:
    print(f"    {name:18s} {what:42s} {weights}")
print("\n  No model below is trained or fine-tuned. Every prediction is a forward pass that reads")
print("  labelled 'context' rows and answers 'query' rows, like a prompt with examples in it.")

We install Structured Data Models from the GitHub repository, pinned to the commit this notebook was verified against, since the package on PyPI is only a placeholder. The relational sampler also needs pyg-lib, and that is where the first trap lies. pyg-lib wheels are published per minor PyTorch release, on pages named like torch-2.14.0+cpu, but the library’s own ImportError builds the URL from the exact patch version, torch-2.14.1+cpu, which is an empty page, so following its advice fails. We build the index URL from the major and minor version instead. We run everything on the CPU, which is the path verified end to end here, and print the models the library exposes together with their checkpoint sizes and licenses; the two Google models it also wraps, TabFM and TimesFM 3, are large and non-commercial and are left out. Check out the Full CODES here.

我们从 GitHub 仓库安装 Structured Data Models,锁定为此笔记本验证时使用的提交版本,因为 PyPI 上的包仅占位符。关系采样器还需要 pyg-lib,而第一个陷阱就在这里。pyg-lib 的 wheel 包随每个次要 PyTorch 版本发布,页面名称类似 torch-2.14.0+cpu,但该库自身的 ImportError 会根据精确的补丁版本构建 URL,如 torch-2.14.1+cpu,这是一个空页面,因此遵循其建议会失败。我们改用主版本和次版本来构建索引 URL。我们在 CPU 上运行所有内容,这也是此处端到端验证的路径,并打印库暴露的模型及其检查点大小和许可证;它还封装的两个 Google 模型 TabFM 和 TimesFM 3 体积庞大且非商用,故被排除。在此查看完整代码。

更进一步:量化金融体系

看懂新闻只是起点——沿量化金融路径,把它变成能交付的工程能力

进入量化体系 →

相似阅读

关联信息,但可能不是同一事件