How to build a vector of marginal probabilities, given a tensor in PyTorch
Solution 1:
You can use torch.unique
and torch.nonzero
:
T1 = ...
values, inverse, counts = T1.unique(dim=0, return_inverse=True, return_counts=True)
ps = torch.zeros(inverse.numel())
for i, (v, c) in enumerate(zip(values, counts)):
first_occurence = torch.nonzero(inverse == i)[0].item()
ps[first_occurence] = c
ps /= ps.sum()