如何找到二维激活图(pytorch)的均值和协方差



我有一个形状为[h,w]的张量,它由一个归一化的二维激活图组成。考虑到这是一个分布,我想在pytorch中找到激活图中的均值和协方差。有有效的方法吗?

您可以使用以下代码,其中activation_map是形状为(h,w)的张量,具有非负元素,并且是归一化的(activation_map.sum()是1(:

activation_map = torch.tensor(
[[0.2, 0.1, 0.0],
[0.1, 0.2, 0.4]])
h, w = activation_map.shape
range_h = torch.arange(h)
range_w = torch.arange(w)
idxs = torch.stack([
range_w[None].repeat(h, 1),
range_h[:, None].repeat(1, w)
])
map_flat = activation_map.view(-1)
idxs_flat = idxs.reshape(2, -1).T
mean = (map_flat[:, None] * idxs_flat).sum(0)
mats = idxs_flat[:, :, None] @ idxs_flat[:, None, :]
second_moments = (map_flat[:, None, None] * mats).sum(0)
covariance = second_moments - mean[:, None] @ mean[None]
# mean:
# tensor([1.1000, 0.7000])
# covariance:
# tensor([[0.6900, 0.2300],
#         [0.2300, 0.2100]])

协方差矩阵的一种方法:

h,w = 3,5
def cov(X):
X = X/np.sqrt(X.size(0) - 1)
return X.T @ X
x = torch.randn(h,w)
print(x)
c = cov(x)
print(c)

输出:

tensor([[-1.5029e-01, -2.0626e-01, -7.7845e-01, -1.6811e+00,  5.0312e-01],
[ 4.4658e-01, -1.8570e+00, -6.2250e-01, -1.0989e+00,  1.6159e+00],
[ 6.8612e-01, -4.2650e-02, -9.5685e-01, -1.7947e-03,  2.1187e-01]])
tensor([[ 0.3464, -0.4138, -0.4088, -0.1197,  0.3957],
[-0.4138,  1.7464,  0.6787,  1.1938, -1.5568],
[-0.4088,  0.6787,  0.9545,  0.9972, -0.8001],
[-0.1197,  1.1938,  0.9972,  2.0169, -1.3110],
[ 0.3957, -1.5568, -0.8001, -1.3110,  1.4546]])

mean()应该是琐碎的,只需参考文档即可。

相关内容

  • 没有找到相关文章

最新更新