业务问题:母婴 DTC 站在 Instagram 投广告,用户在手机看到广告、到电脑搜索品牌词下单
层次图神经网络跨设备用户匹配 - 无ID的跨端行为拼接
Skill-HGNN-Cross-Device-Matching
每分广告预算有明确 ROI 追踪,砍掉低效渠道后同等预算 ROAS 提升 30-50%
待企业数据验证 · 缺失:样本、基线、时间窗、币种、计算方法、证据来源
1. 解决的问题
业务问题:母婴 DTC 站在 Instagram 投广告,用户在手机看到广告、到电脑搜索品牌词下单
2. 核心算法逻辑
核心思想
将每台设备的 URL 访问序列 $\mathcal{S}_v = \{s_1, s_2, \ldots, s_n\}$ 构建为层次异构图:
- 细粒度节点(Fine Node)$f_i$:每个不同 URL 一个节点,有向边连接相邻访问的 URL 对,捕捉局部浏览序列结构;
- 粗粒度节点(Coarse Node)$c_j$:每 $K$ 个连续 URL 分配一个 coarse 节点(默认 $K=6$),通过无向边连接到对应的细节点,实现时序分组内的长距离信息汇聚,无需 TGCE 的随机游走生成大量噪声边。
双层消息传递完成后,对两台设备的细节点嵌入矩阵 $X_v \in \mathbb{R}^{m_v \times d}$ 和 $X_w \in \mathbb{R}^{m_w \times d}$ 做 Cross-Attention 配对打分,输出二分类(是否同一用户)。
数学直觉
细粒度消息传递(GRU 聚合 + 均值更新):
M_i^{(l)} = \Phi^{(l)}([x_{j_1}, x_{j_2}, \ldots, x_{j_\kappa}, x_i])x_i^{(l+1)} = \Psi^{(l)}(x_i^{(l)}, M_i^{(l)})其中 $\Phi$ 为 GRU 聚合函数,$\Psi$ 为均值更新函数。
粗粒度 → 细粒度注意力更新(attention-weighted aggregation):
\tilde{x}_j^{(l+1)} = \underset{i \in \tilde{\mathcal{N}}(c_j)}{\text{mean}}(W_1^{(i)} x_i)e_{i,j}^{(l)} = \phi(W_2^{(l)} x_i^{(l)},\; W_3^{(l)} \tilde{x}_j^{(l)}), \quad \alpha_{i,j}^{(l)} = \frac{\exp(e_{i,j}^{(l)})}{\sum_{j' \in \mathcal{N}(f_i)} \exp(e_{i,j'}^{(l)})}x_i^{(l+1)} = \xi\!\left(x_i^{(l)},\; \sum_{j \in \mathcal{N}(f_i)} \alpha_{i,j}^{(l)} \tilde{x}_j^{(l)}\right)Cross-Attention 配对打分(GraphER 启发):
\hat{\alpha}_{i,j} = \text{softmax}_j(\zeta(W_3 x_{v,i},\; W_3 x_{w,j}))L_{v,w} = [\text{diag}(\beta_v)(A_{v,w} X_w - X_v)] \odot [\text{diag}(\beta_v)(A_{v,w} X_w - X_v)]其中 $\beta_v = \text{sigmoid}(W_4 \tanh(W_5 X_v^T))$ 为特征过滤向量,$L_{v,w}$ 度量两设备嵌入的欧氏距离差异。最终 MLP + max-pooling → 拼接 → sigmoid 输出匹配概率。
关键假设
- 设备浏览序列具有时序局部性:时间上相邻的 URL 访问比远隔数周的访问信息相关性更高(这是 coarse 节点优于随机游走的关键)
- 同一用户的不同设备在 URL 访问模式(而非具体 URL)上存在可学习的相似性
- 数据集足够大以学习有意义的 URL 嵌入(CIKM Cup 2016 数据:14,148,535 条匿名 URL 日志,平均每设备 197 条)
- 无需任何用户 ID、登录信息、地理位置等显式身份标识
关键效果数字
| 方法 | Precision | Recall | F1 Score | 训练时间 |
|---|---|---|---|---|
| TF-IDF(人工特征) | 0.33 | 0.27 | 0.26 | — |
| TGCE(SOTA基线) | 0.49 | 0.44 | 0.46 | 60h |
| HGNN(无Cross-Att) | 0.48 | 0.43 | 0.45 | 10h(6×快) |
| HGNN+Cross-Att(本文) | 0.57 | 0.48 | 0.51 | 60h |
- 相对 TGCE 提升 F1 +5%(0.46→0.51),同等训练时间
- 仅用 HGNN 结构(无 Cross-Att):比 TGCE 快 6×,性能仅差 1%
- 在所有阈值下 HGNN+Cross-Att 的 Precision-Recall 曲线严格优于 TGCE
3. 业务应用场景
业务问题:母婴 DTC 站在 Instagram 投广告,用户在手机看到广告、到电脑搜索品牌词下单。设备级日志显示"手机曝光零转化""电脑无广告来源"——Sankey 归因图中跨设备链路断裂,平台报告 ROAS 严重失真。需要识别"这两台设备属于同一用户",恢复真实跨端转化路径。
| 字段 | 类型 | 示例 | |------|------|------| | `device_id` | str | `"mob_a3f9c2"` / `"pc_b71e44"` | | `url_sequence` | List[str] | `["instagram.com/reel/xxx", "google.com/search?q=品牌", "brand.com/product/abc"]` | | `timestamp_sequence` | List[datetime] | `[2025-03-01 10:15, 2025-03-01 10:16, ...]` | | `d
- 最小要求:每设备至少 20 条匿名 URL 日志(时间跨度 2-4 周) - 数据来源:独立站服务端日志(不依赖第三方 Cookie),通过 IP + User-Agent 初步聚类生成 device_id - 隐私合规:URL 仅保留 domain+path 前两级,不含查询参数中的个人信息
4. 输入数据要求
- TikTok 端:device_id + 视频播放 URL 序列(tiktok.com/video/xxx)
- Amazon 端:device_id + 商品浏览 URL 序列(amazon.com/dp/ASIN)
- 两端共享 IP 地址哈希作为弱信号初始化 URL 嵌入(不作为匹配特征,仅用于特征初始化)
5. 输出结果
- 设备对匹配概率矩阵(score > 0.5 判定为同一用户)
- 识别率:在 CIKM 数据集基准上 F1=0.51,实际品牌数据集(URL 重叠度更高)预估可达 0.55-0.65
- 每 1000 个设备对中,预计正确识别 300-450 个跨端用户关联
- 跨平台 device pair 匹配 → TikTok 渠道真实归因权重 → 复算 TikTok ROI
6. 业务价值与 ROI
未自动抽取;请查看原始 Skill 卡片。
7. 代码模板
代码块数量:2
"""
HGNN Cross-Device Matching — 完整实现
arXiv:2304.03215 (NVIDIA, 2023)
依赖: torch>=2.0, numpy, scikit-learn
说明:原文用 torch_geometric(HeteroData/MessagePassing) + torch_scatter;
本实现改为**手写图传播**——用稠密行归一化邻接矩阵做 `A_norm @ H @ W`、
用 index_add_ 手写 scatter_mean,保持层次图结构与各层聚合方式(GRU 聚合 +
均值更新 / coarse 均值 + attention 回注)不变,不退化成无图结构的 MLP。
"""
import torch
import torch.nn as nn
import torch.nn.functional as F
import numpy as np
from typing import List, Tuple, Dict, Optional
from sklearn.metrics import f1_score, precision_score, recall_score
# ============================================================
# 1. 层次图构建(手写容器,替代 HeteroData)
# ============================================================
class DeviceGraph:
"""单台设备的层次异构图(Fine + Coarse 节点)轻量容器。
字段与原 torch_geometric 版本一一对应:
fine_ids ← data['fine'].x(URL id,供 embedding lookup)
n_fine ← data['fine'].num_nodes
n_coarse ← data['coarse'].num_nodes
ff_edge ← data['fine','next','fine'].edge_index [2,E] (fine, fine)
cf_edge ← data['coarse','groups','fine'].edge_index [2,E] (coarse, fine)
fc_edge ← data['fine','grouped_by','coarse'].edge_index[2,E](fine, coarse)
"""
def __init__(self, fine_ids, n_coarse, ff_edge, cf_edge, fc_edge):
self.fine_ids = fine_ids
self.n_fine = int(fine_ids.shape[0])
self.n_coarse = int(n_coarse)
self.ff_edge = ff_edge
self.cf_edge = cf_edge
self.fc_edge = fc_edge
def build_hierarchical_graph(
url_sequence: List[int],
K: int = 6,
url_embed_dim: int = 64,
url_vocab_size: int = 10000,
) -> DeviceGraph:
"""
将 URL 访问序列转换为层次图(Fine + Coarse 节点)。
Args:
url_sequence: URL ID 列表(已编码为整数)
K: 每 K 个连续 URL 分配一个 coarse 节点
url_embed_dim: URL embedding 维度(保留以兼容原签名)
url_vocab_size: URL 词表大小(保留以兼容原签名)
Returns:
DeviceGraph,包含 fine 节点、coarse 节点及其连边
"""
# --- Fine 节点 ---
# 去重 URL,保留首次出现顺序
unique_urls = list(dict.fromkeys(url_sequence))
url_to_idx = {url: i for i, url in enumerate(unique_urls)}
fine_node_ids = torch.tensor(unique_urls, dtype=torch.long)
# Fine → Fine 有向边(相邻 URL 对)
src, dst = [], []
for i in range(len(url_sequence) - 1):
src.append(url_to_idx[url_sequence[i]])
dst.append(url_to_idx[url_sequence[i + 1]])
ff_edge = torch.tensor([src, dst], dtype=torch.long) if src \
else torch.zeros((2, 0), dtype=torch.long)
# --- Coarse 节点:每 K 个连续 URL 分一组 ---
n_coarse = (len(url_sequence) + K - 1) // K
coarse_src, fine_dst = [], []
for j in range(n_coarse):
for pos in range(j * K, min((j + 1) * K, len(url_sequence))):
coarse_src.append(j)
fine_dst.append(url_to_idx[url_sequence[pos]])
cf_edge = torch.tensor([coarse_src, fine_dst], dtype=torch.long)
fc_edge = torch.tensor([fine_dst, coarse_src], dtype=torch.long)
return DeviceGraph(fine_node_ids, n_coarse, ff_edge, cf_edge, fc_edge)
def normalized_adjacency(n_nodes: int, edge_index: torch.Tensor,
device: torch.device) -> torch.Tensor:
"""行归一化邻接矩阵 A_norm(含自环),用于手写 `A_norm @ H @ W` 传播。"""
A = torch.zeros(n_nodes, n_nodes, device=device)
if edge_index.shape[1] > 0:
s, d = edge_index[0], edge_index[1]
A[d, s] = 1.0
A = A + torch.eye(n_nodes, device=device)
deg = A.sum(dim=1, keepdim=True).clamp(min=1.0)
return A / deg
def scatter_mean(src: torch.Tensor, index: torch.Tensor, dim_size: int) -> torch.Tensor:
"""手写 scatter_mean(替代 torch_scatter.scatter_mean)。src:[E,d] index:[E]。"""
out = torch.zeros(dim_size, src.shape[-1], device=src.device, dtype=src.dtype)
cnt = torch.zeros(dim_size, device=src.device, dtype=src.dtype)
out = out.index_add_(0, index, src)
cnt = cnt.index_add_(0, index, torch.ones_like(index, dtype=src.dtype))
return out / cnt.clamp(min=1.0).unsqueeze(-1)
# ============================================================
# 2. Fine-Level GRU 消息传递
# ============================================================
class FineGRUConv(nn.Module):
"""
细粒度 URL 节点消息传递(手写,替代 MessagePassing):
- 聚合函数 Φ:GRU(输入为 A_norm @ H 的邻域聚合结果)
- 更新函数 Ψ:均值
"""
def __init__(self, hidden_dim: int):
super().__init__()
self.hidden_dim = hidden_dim
self.gru = nn.GRUCell(hidden_dim, hidden_dim)
self.norm = nn.LayerNorm(hidden_dim)
def forward(self, x: torch.Tensor, a_norm: torch.Tensor) -> torch.Tensor:
# 邻接矩阵传播:聚合邻居特征(含自环、行归一化)
agg = a_norm @ x
# GRU 聚合 + 均值更新
out = (x + self.gru(agg, x)) / 2.0
return self.norm(out)
# ============================================================
# 3. Coarse-Fine 异构注意力更新
# ============================================================
class CoarseFineAttention(nn.Module):
"""
粗粒度 → 细粒度注意力消息传递:
- 先用 mean 更新 coarse 节点
- 再用 attention 将 coarse 信息注回 fine 节点
"""
def __init__(self, hidden_dim: int):
super().__init__()
self.W1 = nn.Linear(hidden_dim, hidden_dim, bias=False)
self.W2 = nn.Linear(hidden_dim, hidden_dim, bias=False)
self.W3 = nn.Linear(hidden_dim, hidden_dim, bias=False)
self.update = nn.Linear(hidden_dim * 2, hidden_dim)
self.norm = nn.LayerNorm(hidden_dim)
def forward(
self,
fine_x: torch.Tensor, # [n_fine, d]
coarse_x: torch.Tensor, # [n_coarse, d]
cf_edge_index: torch.Tensor, # coarse→fine [2, E]
fc_edge_index: torch.Tensor, # fine→coarse [2, E]
) -> Tuple[torch.Tensor, torch.Tensor]:
# 1. 更新 coarse 节点:fine → coarse 的均值聚合(fc_edge = [fine, coarse])
fine_row, coarse_row = fc_edge_index[0], fc_edge_index[1]
new_coarse = scatter_mean(
self.W1(fine_x)[fine_row], coarse_row, coarse_x.shape[0]
)
# 2. 计算 fine → coarse attention 权重
coarse_idx, fine_idx = cf_edge_index[0], cf_edge_index[1]
e = (self.W2(fine_x[fine_idx]) * self.W3(new_coarse[coarse_idx])).sum(-1)
# softmax 归一化(按 fine 节点分组)
alpha = torch.zeros_like(e)
for fi in fine_idx.unique():
mask = (fine_idx == fi)
alpha[mask] = F.softmax(e[mask], dim=0)
# 3. 更新 fine 节点:attention 加权后的 coarse 信息回注
agg_coarse = scatter_mean(
alpha.unsqueeze(-1) * new_coarse[coarse_idx], fine_idx, fine_x.shape[0]
)
new_fine = self.norm(self.update(torch.cat([fine_x, agg_coarse], dim=-1)))
return new_fine, new_coarse
# ============================================================
# 4. Cross-Attention 匹配模块(GraphER 启发)
# ============================================================
class CrossAttentionMatcher(nn.Module):
"""
双设备图 Cross-Attention 匹配:
实现论文 Section 3.4 的 Cross-Encoding + Feature Filtering
"""
def __init__(self, hidden_dim: int, output_dim: int = 64):
super().__init__()
self.W3 = nn.Linear(hidden_dim, hidden_dim, bias=False)
self.W4 = nn.Linear(hidden_dim, 1, bias=False)
self.W5 = nn.Linear(hidden_dim, hidden_dim, bias=False)
self.mlp = nn.Sequential(
nn.Linear(hidden_dim, output_dim),
nn.ReLU(),
nn.Dropout(0.3),
)
self.classifier = nn.Sequential(
nn.Linear(output_dim * 2, 64),
nn.ReLU(),
nn.Linear(64, 1),
nn.Sigmoid(),
)
def forward(
self,
Xv: torch.Tensor, # [mv, d]
Xw: torch.Tensor, # [mw, d]
) -> torch.Tensor:
"""
Args:
Xv, Xw: 两设备细节点嵌入矩阵
Returns:
匹配概率 [0, 1]
"""
# Cross-Encoding: Xv → Xw
Av_w = F.softmax(
torch.mean(self.W3(Xv).unsqueeze(1) * self.W3(Xw).unsqueeze(0), dim=-1),
dim=1
) # [mv, mw]
Aw_v = F.softmax(
torch.mean(self.W3(Xw).unsqueeze(1) * self.W3(Xv).unsqueeze(0), dim=-1),
dim=1
) # [mw, mv]
# Feature Filtering:β_v = sigmoid(W4 · tanh(W5 · x_v)),逐节点 [mv, 1]
beta_v = torch.sigmoid(self.W4(torch.tanh(self.W5(Xv)))) # [mv, 1]
beta_w = torch.sigmoid(self.W4(torch.tanh(self.W5(Xw)))) # [mw, 1]
# L 矩阵:cross-encoded 差异
Lv_w = (beta_v * (Av_w @ Xw - Xv)) ** 2 # [mv, d]
Lw_v = (beta_w * (Aw_v @ Xv - Xw)) ** 2 # [mw, d]
# MLP + max-pooling → 固定维度向量
rv_w = self.mlp(Lv_w).max(dim=0).values # [output_dim]
rw_v = self.mlp(Lw_v).max(dim=0).values # [output_dim]
# 最终分类
score = self.classifier(torch.cat([rv_w, rw_v], dim=-1))
return score.squeeze()
# ============================================================
# 5. 完整 HGNN 模型
# ============================================================
class HGNNCrossDeviceModel(nn.Module):
"""
完整 HGNN + Cross-Attention 跨设备用户匹配模型。
"""
def __init__(
self,
url_vocab_size: int = 10000,
embed_dim: int = 64,
hidden_dim: int = 64,
n_fine_layers: int = 2,
n_coarse_layers: int = 1,
K: int = 6,
):
super().__init__()
self.K = K
self.url_embed = nn.Embedding(url_vocab_size + 1, embed_dim, padding_idx=0)
# Fine-level GRU 层
self.fine_convs = nn.ModuleList([
FineGRUConv(hidden_dim) for _ in range(n_fine_layers)
])
# Coarse-Fine 注意力层
self.coarse_fine_attn = CoarseFineAttention(hidden_dim)
# Cross-Attention 匹配
self.cross_attn = CrossAttentionMatcher(hidden_dim)
# 输入维度对齐
self.input_proj = nn.Linear(embed_dim, hidden_dim)
def encode_device(self, graph: DeviceGraph) -> torch.Tensor:
"""将单台设备图编码为细节点嵌入矩阵"""
# URL embedding
fine_x = self.input_proj(self.url_embed(graph.fine_ids))
coarse_x = torch.zeros(
graph.n_coarse, fine_x.shape[-1],
device=fine_x.device
)
# Fine-level 消息传递:预计算归一化邻接矩阵 A_norm,逐层手写 A_norm @ H @ W
a_norm = normalized_adjacency(graph.n_fine, graph.ff_edge, fine_x.device)
for conv in self.fine_convs:
fine_x = conv(fine_x, a_norm)
# Coarse-Fine 异构注意力
fine_x, coarse_x = self.coarse_fine_attn(
fine_x, coarse_x, graph.cf_edge, graph.fc_edge)
return fine_x # [n_fine, hidden_dim]
def forward(
self,
graph_v: DeviceGraph,
graph_w: DeviceGraph,
) -> torch.Tensor:
"""
Args:
graph_v, graph_w: 两台设备的层次图
Returns:
匹配概率 [0, 1]
"""
Xv = self.encode_device(graph_v)
Xw = self.encode_device(graph_w)
return self.cross_attn(Xv, Xw)
# ============================================================
# 6. 模拟数据生成 + 测试用例
# ============================================================
def simulate_device_url_logs(
n_user_pairs: int = 200,
n_negative_pairs: int = 200,
url_vocab_size: int = 500,
avg_sequence_len: int = 30,
same_user_overlap: float = 0.4,
seed: int = 2026,
) -> List[Tuple[List[int], List[int], int]]:
"""
模拟跨设备 URL 日志数据集。
Args:
n_user_pairs: 同一用户的设备对数量(正样本)
n_negative_pairs: 不同用户的设备对数量(负样本)
url_vocab_size: URL 词表大小
avg_sequence_len: 平均序列长度
same_user_overlap: 同一用户两设备的 URL 重叠比例
seed: 随机种子
Returns:
List of (seq_v, seq_w, label),label=1 表示同一用户
"""
rng = np.random.default_rng(seed)
pairs = []
# 正样本:同一用户,两设备共享部分 URL 模式
for _ in range(n_user_pairs):
# 用户的核心 URL 偏好(模拟个人浏览习惯)
n_shared = int(avg_sequence_len * same_user_overlap)
shared_urls = rng.choice(url_vocab_size, size=n_shared, replace=False).tolist()
# 设备 V:核心 URL + 随机 URL
n_extra_v = avg_sequence_len - n_shared
extra_v = rng.choice(url_vocab_size, size=n_extra_v, replace=True).tolist()
seq_v = shared_urls + extra_v
rng.shuffle(seq_v)
# 设备 W:核心 URL + 不同随机 URL
n_extra_w = avg_sequence_len - n_shared
extra_w = rng.choice(url_vocab_size, size=n_extra_w, replace=True).tolist()
seq_w = shared_urls + extra_w
rng.shuffle(seq_w)
pairs.append((seq_v, seq_w, 1))
# 负样本:不同用户,URL 模式不重叠
for _ in range(n_negative_pairs):
seq_v = rng.choice(url_vocab_size, size=avg_sequence_len, replace=True).tolist()
seq_w = rng.choice(url_vocab_size, size=avg_sequence_len, replace=True).tolist()
pairs.append((seq_v, seq_w, 0))
rng.shuffle(pairs)
return pairs
def run_quick_test():
"""
快速功能测试:验证模型前向传播 + 训练 2 个 epoch。
"""
print("=" * 60)
print("HGNN Cross-Device Matching — 快速测试")
print("=" * 60)
# --- 参数 ---
URL_VOCAB = 500
HIDDEN_DIM = 32
K = 6
N_EPOCHS = 2
BATCH_SIZE = 16
# --- 数据生成 ---
print("\n[1] 生成模拟数据...")
all_pairs = simulate_device_url_logs(
n_user_pairs=100, n_negative_pairs=100,
url_vocab_size=URL_VOCAB, avg_sequence_len=25, seed=42
)
train_pairs = all_pairs[:160]
test_pairs = all_pairs[160:]
print(f" 训练集: {len(train_pairs)} 对 | 测试集: {len(test_pairs)} 对")
# --- 模型 ---
print("\n[2] 初始化模型...")
model = HGNNCrossDeviceModel(
url_vocab_size=URL_VOCAB,
embed_dim=HIDDEN_DIM,
hidden_dim=HIDDEN_DIM,
K=K,
)
optimizer = torch.optim.Adam(model.parameters(), lr=1e-3)
criterion = nn.BCELoss()
total_params = sum(p.numel() for p in model.parameters())
print(f" 参数量: {total_params:,}")
# --- 训练 ---
print(f"\n[3] 训练 {N_EPOCHS} 个 epoch...")
model.train()
for epoch in range(N_EPOCHS):
epoch_loss = 0.0
n_batches = 0
for i in range(0, len(train_pairs), BATCH_SIZE):
batch = train_pairs[i:i + BATCH_SIZE]
optimizer.zero_grad()
batch_loss = torch.tensor(0.0)
for seq_v, seq_w, label in batch:
graph_v = build_hierarchical_graph(seq_v, K=K, url_vocab_size=URL_VOCAB)
graph_w = build_hierarchical_graph(seq_w, K=K, url_vocab_size=URL_VOCAB)
pred = model(graph_v, graph_w)
target = torch.tensor(float(label))
batch_loss = batch_loss + criterion(pred, target)
batch_loss = batch_loss / len(batch)
batch_loss.backward()
optimizer.step()
epoch_loss += batch_loss.item()
n_batches += 1
print(f" Epoch {epoch+1}/{N_EPOCHS} — avg loss: {epoch_loss/n_batches:.4f}")
# --- 评估 ---
print("\n[4] 测试集评估...")
model.eval()
y_true, y_pred = [], []
with torch.no_grad():
for seq_v, seq_w, label in test_pairs:
graph_v = build_hierarchical_graph(seq_v, K=K, url_vocab_size=URL_VOCAB)
graph_w = build_hierarchical_graph(seq_w, K=K, url_vocab_size=URL_VOCAB)
prob = model(graph_v, graph_w).item()
y_true.append(label)
y_pred.append(int(prob > 0.5))
f1 = f1_score(y_true, y_pred, zero_division=0)
prec = precision_score(y_true, y_pred, zero_division=0)
rec = recall_score(y_true, y_pred, zero_division=0)
print(f" Precision: {prec:.3f} | Recall: {rec:.3f} | F1: {f1:.3f}")
print("\n✅ 快速测试通过(未完整训练,F1 指标仅作结构验证)")
return model
def run_business_demo():
"""
业务场景演示:输出设备对匹配分数。
"""
print("\n" + "=" * 60)
print("业务场景演示:母婴 DTC 跨设备匹配")
print("=" * 60)
# 词表覆盖演示数据里出现的最大 URL id(510),否则 embedding 索引越界
model = HGNNCrossDeviceModel(url_vocab_size=1000, embed_dim=32, hidden_dim=32)
model.eval()
# 模拟:同一用户(手机+电脑)
# 手机:Instagram → Google → 品牌站
phone_urls = [101, 102, 103, 104, 105, 106, 107, 108, 109, 110,
201, 202, 203, 204, 205, 206, 207, 208, 209, 210,
301, 302, 303, 304, 305] # 共享品牌站 URL 301-305
# 电脑:Google → 品牌站(无 Instagram)
desktop_urls = [401, 402, 403, 404, 405, 406, 407, 408, 409, 410,
501, 502, 503, 504, 505, 506, 507, 508, 509, 510,
301, 302, 303, 304, 305] # 共享品牌站 URL 301-305
# 不同用户(两台无关设备)
unrelated_urls = [11, 22, 33, 44, 55, 66, 77, 88, 99, 100,
111, 122, 133, 144, 155, 166, 177, 188, 199, 200,
211, 222, 233, 244, 255]
with torch.no_grad():
g_phone = build_hierarchical_graph(phone_urls)
g_desktop = build_hierarchical_graph(desktop_urls)
g_other = build_hierarchical_graph(unrelated_urls)
score_same = model(g_phone, g_desktop).item()
score_diff = model(g_phone, g_other).item()
print(f"\n 手机 vs 电脑(同一用户,URL重叠):匹配概率 = {score_same:.3f}")
print(f" 手机 vs 无关设备(不同用户): 匹配概率 = {score_diff:.3f}")
print("\n 注:上述分数为未训练模型的随机初始化结果,")
print(" 完整训练后(CIKM数据集,20 epochs)预期 F1=0.51")
# 输出业务摘要
print("\n --- 业务输出示例 ---")
threshold = 0.5
decisions = [
("mob_a3f9c2", "pc_b71e44", score_same),
("mob_a3f9c2", "pc_c82f55", score_diff),
]
print(f" {'设备V':<15} {'设备W':<15} {'匹配分数':<10} {'判定':<10}")
print(f" {'-'*50}")
for dev_v, dev_w, score in decisions:
verdict = "同一用户 ✓" if score > threshold else "不同用户 ✗"
print(f" {dev_v:<15} {dev_w:<15} {score:<10.3f} {verdict}")
if __name__ == "__main__":
# 固定 torch 随机源:模型初始化与 Dropout 必须跨进程可复现
torch.manual_seed(42)
# 快速功能测试
run_quick_test()
# 业务场景演示
run_business_demo()
print("[✓] HGNN Cross Device Matchin 测试通过")8. 论文来源
论文链接用于核验方法来源,不自动证明本页商业结果。