overleaf写图对比学习的Notations and Preliminaries
时间: 2025-06-28 08:18:01 浏览: 19
### 图对比学习的符号表示与预备知识
#### 符号定义
为了便于理解图对比学习(Graph Contrastive Learning, GCL),以下是常用的一些符号及其含义:
- \( \mathcal{G}=(\mathcal{V}, \mathcal{E}) \) 表示一个图,其中 \( \mathcal{V}=\left\{v_{1}, v_{2}, \ldots, v_{|\mathcal{V}|}\right\} \) 是节点集合,\( \mathcal{E} \subseteq \mathcal{V} \times \mathcal{V} \) 是边集合[^1]。
- \( A \in \mathbb{R}^{N \times N} \) 或者 \( E \in \mathbb{R}^{N \times M} \),分别代表邻接矩阵和边特征矩阵。这里 \( N=|\mathcal{V}| \), \( M \) 则取决于具体的任务设定[^3]。
- \( X \in \mathbb{R}^{N \times F} \) 为节点特征矩阵,每一行对应于一个节点的特征向量,列数等于每个节点拥有的特征数量 \( F \)。
- 对于任意给定的图增强操作 \( T(\cdot) \),可以得到两个不同的视图 (views): \( \hat{\mathcal{G}}_i=T_i (\mathcal{G}), i={1,2} \),这两个视图为原始图的不同版本,通过特定的数据增强技术获得。
#### 预备知识
##### 数据增强机制
在图对比学习框架下,数据增强是指通过对原图施加某种变换从而生成新的图实例的过程。常见的增强手段包括但不限于节点删除(Node Dropping)、边缘移除(Edge Removing)以及子图采样(Subgraph Sampling)等。这些方法旨在保持图形的整体结构特性的同时引入一定的变化,以便让模型能够捕捉到更鲁棒性的表征信息[^2]。
##### 对比损失函数
对比损失通常用来度量同一张图经过不同转换后的相似程度。一种广泛使用的公式是InfoNCE Loss:
\[ l(i,j)=−log\frac{{exp(sim(z_i,\tilde z_j)/τ)}}{{∑_{k∈Ω_N(j)} exp(sim(z_k ,\tilde z_j )/τ)}} \]
这里的 \( sim(a,b) \) 可以选择余弦相似度或其他合适的距离测度;而温度参数 \( τ>0 \) 控制着分布的尖锐程度;\( Ω_N(j) \) 定义了一个负样本池,它包含了除了正样本外的所有可能的选择。
```python
import torch.nn.functional as F
def info_nce_loss(anchor_embeddings, positive_embeddings, negative_embeddings, temperature=0.5):
pos_scores = F.cosine_similarity(anchor_embeddings, positive_embeddings)
neg_scores = []
for neg_emb in negative_embeddings:
score = F.cosine_similarity(anchor_embeddings, neg_emb.unsqueeze(dim=0))
neg_scores.append(score)
logits = torch.cat([pos_scores.view(-1, 1)] + [torch.stack(neg_scores).T], dim=-1) / temperature
labels = torch.zeros(logits.shape[0]).long().to(logits.device)
loss = F.cross_entropy(logits, labels)
return loss
```
阅读全文
相关推荐




















