I have a tensor of shape [h, w], which consists of a normalized, 2-dimensional activation map. Considering this to be some distribution, I want to find the mean and the covariance within this activation map in pytorch. Is there an efficient way to do that?
0
votes
2 Answers
2
votes
You can use the following code, where activation_map
is a tensor of shape (h,w)
, with non-negative elements, and is normalised (activation_map.sum()
is 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]])
0
votes
One way for the covariance matrix:
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)
Out:
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]])
The mean()
should be trivial just refer the documentation.