Affinity Mattrix 亲和矩阵总结

  • *什么是Affinity Matrix?

An Affinity Matrix, also called a Similarity Matrix, is an essential statistical technique used to organize the mutual similarities between a set of data points. Similarity is similar to distance, however, it does not satisfy the properties of a metric, two points that are the same will have a similarity score of 1, whereas computing the metric will result in zero. Typical examples of similarity measures are the cosine similarity and the Jaccard similarity. These similarity measures can be interpreted as the probability that that two points are related. hor example, if two data points have coordinates that are close, then their cosine similarity score ( or respective “affinity” score) will be much closer to 1 than two data points with a lot of space between them.

翻译:
亲和矩阵,也称为相似矩阵,是一种基本的统计技术,用于组织一组数据点之间的相互相似性。 相似度类似于距离,但是,它不满足度量的属性,相同的两个点的相似度得分为 1,而计算度量的结果为零。 相似度度量的典型例子是余弦相似度和 Jaccard 相似度。 这些相似性度量可以解释为两个点相关的概率。 例如,如果两个数据点的坐标接近,那么它们的余弦相似度得分(或各自的”亲和力”得分)将比两个数据点之间有很大空间的数据点更接近 1。

  • *Affinity Matrix 可以用来干什么?

智能信息检索:亲和矩阵是搜索引擎的驱动力,它为你提取你甚至不知道你需要的额外相关信息。
数据挖掘:相似矩阵可以快速准确地在充满未标记信息的数据库中识别任何的隐藏关系模式。
无监督学习:如果没有相似度矩阵来确保网络自学内容的最低准确性标准,那么创建能够从原始无组织数据中获取结构和含义的机器学习算法是不可能的。

  • *怎么计算Affinity Matix?

亲和矩阵需要自我构建。构建Affinity矩阵的方法有多种。但基本是构造一个度量值使得两个变量相近时更接近于1。与距离度量矩阵相反,距离很紧基本为零时,相似矩阵值接近于1。
例1: 我们使用余弦值来评价任意两个点之间的相似性。假如有一个三通道图像,shape为(H,W,3)。这样每个像素点都是3个分量构成。计算任意两点之间的向量余弦值:

Affinity Mattrix 亲和矩阵总结
可以看出余弦为0时,两个向量垂直,相似度低。余弦为1时,向量平行,相似度高。
这样就可以使用矩阵乘法来计算相似度矩阵,对于一幅大小为(W,H)的图片,先将图片变成一维向量(H _W,1),然后将这个一维向量乘以它的转置(1,H_W),这样就得到长为W _H,宽也为W_H的Affinity Matrix,这个矩阵是对称的。
import torch
import matplotlib.pyplot as plt
import torchvision.transforms as tfs
from PIL import Image
import time

def getAffinity_Matrix(img):
    img = img.permute(1,2,0)

    affinity = torch.zeros(img.shape[0]*img.shape[1], img.shape[0]*img.shape[1])
    print(affinity.shape)

    img1 = img.reshape(-1, img.shape[-1])

    img_ = torch.sqrt((img1[:,:]**2).sum(dim=-1))
    img2 = img.reshape(-1, img.shape[-1])
    for idx in range(affinity.shape[1]):
        affinity[idx, :] = torch.mul(img1[idx, :], img2[:, :]).sum(dim=-1)
        affinity[idx, :] = affinity[idx, :]/img_[idx]
    for idx in range(affinity.shape[0]):

        affinity[:, idx] = affinity[:, idx]/img_[idx]
    print(affinity)
    return affinity

def display(affinity):
    plt.imshow(affinity)
    plt.colorbar()
    plt.savefig("affinity.jpg")
    plt.show()

def process(img_root, rate=16):
    img = Image.open(img_root)
    size = 224//rate
    img = img.resize((size,size))
    plt.imshow(img)
    plt.savefig("tmp.jpg")
    img = tfs.ToTensor()(img)
    device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
    img = img.to(device)
    return img
img="gcA1.jpg"
img = process(img)
affinity = getAffinity_Matrix(img)
display(affinity)

Affinity Mattrix 亲和矩阵总结
Affinity Mattrix 亲和矩阵总结
例2: 谱聚类方法来构建亲和矩阵。常用构建方法是:自表达方法(self-expression method)。通过以下优化问题得到:
Affinity Mattrix 亲和矩阵总结
其中,C就是描述数据之间相似性的affinity矩阵。R©表示一个关于​C的先验正则,且满足Cii=0。
  • Affinity Matix 是怎么样的形式?
    相似矩阵一般都是对称矩阵,且良好的矩阵应该是在对角线处值最大。即对应的数据相似性最高。
    Affinity Mattrix 亲和矩阵总结
    如图,第三个得到的亲和矩阵是最好的,具有更好的块对角特性和更少的噪声。
    Affinity Mattrix 亲和矩阵总结
    参考:
  • 使用pytorch计算图像的Affinity Matrix
  • 相似度矩阵的几种构造方式(附代码)
    3.Deep Subspace Clustering Networks DOI:10.48550/ARXIV.1709.02508
    4.Multi-view Deep Subspace Clustering Networks DOI:10.48550/ARXIV.1908.01978

Original: https://blog.csdn.net/weixin_42228166/article/details/124419595
Author: dlut0427
Title: Affinity Mattrix 亲和矩阵总结

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/697636/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球