按照真实标签排序pair-wise相似度矩阵的Pytorch代码
本文仅作留档,用于输出可视化
- Inputs: Ground-truths Y ∈ R n × 1 \mathbf{Y}\in\mathbb R^{n\times 1} Y∈Rn×1, Similarity matrix A ∈ R n × n \mathbf{A}\in\mathbb R^{n\times n} A∈Rn×n
- Outputs: Block diagnal matrix of A \mathbf{A} A sorted by Y \mathbf{Y} Y for visualization.
示例:大规模预训练模型CLIP特征在CIFAR10数据集上的 ground truth similarity matrix
import torch
import matplotlib.pyplot as plt
Y = labels.argsort()
A = logits.mm(logits.t())
# ground truth similarity matrix
A_sorted = A[Y][:, Y]
# visualize
# A should be normalized to [0,1]
plt.imshow(A_sorted , cmap='Blues', vmin=0, vmax=1)
plt.colorbar()
plt.set_title('Entitle here')
plt.savefig('save_name.png')
plt.close()