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个分量构成。计算任意两点之间的向量余弦值:
在这里插入图片描述
可以看出余弦为0时,两个向量垂直,相似度低。余弦为1时,向量平行,相似度高。
这样就可以使用矩阵乘法来计算相似度矩阵,对于一幅大小为(W,H)的图片,先将图片变成一维向量(HW,1),然后将这个一维向量乘以它的转置(1,HW),这样就得到长为WH,宽也为WH的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)#进行维度置换(14,14,3)
    # [width, height]
    #创建一个H*W的方阵,作为亲和矩阵的初始阵,为何是H*W上文已说明
    affinity = torch.zeros(img.shape[0]*img.shape[1], img.shape[0]*img.shape[1])
    print(affinity.shape)
    #将图片变为一维,但是3通道。所以img1.shape为(196,3)
    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]):
        #continue
        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))#缩放到16*16
    plt.imshow(img)
    plt.savefig("tmp.jpg")#保存缩放以后的图
    img = tfs.ToTensor()(img)#将图片转为Tensor
    device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")#划分一个位置来放置Tensor
    img = img.to(device)#将Tensor放到划分好的GPU上
    return img#注意得到的是(3,14,14)的Tensor
img="gcA1.jpg"#图像路径
img = process(img)
affinity = getAffinity_Matrix(img)
display(affinity)

原图一栋建筑
得到的相似度矩阵
例2: 谱聚类方法来构建亲和矩阵。常用构建方法是:自表达方法(self-expression method)。通过以下优化问题得到:
在这里插入图片描述
其中,C就是描述数据之间相似性的affinity矩阵。R©表示一个关于​C的先验正则,且满足Cii=0。

  • Affinity Matix 是怎么样的形式?
    相似矩阵一般都是对称矩阵,且良好的矩阵应该是在对角线处值最大。即对应的数据相似性最高。
    在这里插入图片描述
    如图,第三个得到的亲和矩阵是最好的,具有更好的块对角特性和更少的噪声。
    在这里插入图片描述
    参考:
    1.使用pytorch计算图像的Affinity Matrix
    2.相似度矩阵的几种构造方式(附代码)
    3.Deep Subspace Clustering Networks DOI:10.48550/ARXIV.1709.02508
    4.Multi-view Deep Subspace Clustering Networks DOI:10.48550/ARXIV.1908.01978
  • 5
    点赞
  • 51
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值