Hi Alpha!
If I understand what you are looking for, I think multinomial()
will
work just fine for you. As I understand it, you are not looking for any
kind of correlation between class values in different dimensions, so
if this is the case, you should be able to “factor” the random sampling
across dimensions.
Here is an example:
>>> import torch
>>> print (torch.__version__)
2.3.0
>>>
>>> _ = torch.manual_seed (2024)
>>>
>>> k = 5
>>> n = 10
>>>
>>> probs = (torch.ones (1, k) / k).expand (n, k) # uniform class probabilities for all dimensions
>>>
>>> num_samples = 2
>>> torch.multinomial (probs, num_samples = num_samples, replacement = True)
tensor([[1, 3],
[1, 3],
[4, 1],
[1, 2],
[2, 0],
[0, 1],
[4, 3],
[3, 1],
[4, 2],
[2, 4]])
Each column in the result tensor is a sample – in this example, there
are two samples. As you note, there are k**n
distinct (vector-valued)
values for a sample and when feeding uniform probabilities to
multinomial()
, each such value will be sampled with probability
1 / k**n
.
Note that this illustration does not depend on the probabilities being
uniform across classes or dimensions. For arbitrary probabilities, you
do need to materialize the full [n, k]
probability matrix, but that scales
as n*k
, rather than exponentially.
If this is not what you are looking for, could you post some concrete
code that generates your desired result (even if it doesn’t scale well)?
Best.
K. Frank