I want to create a zero-one tensor of dimension (n, n)
. The ones should be placed randomly, with a cap on the number of ones in each row. Let us say I have a list of length n that has the value of cap for each of the n rows.
One way very close to what I want is to create a random tensor and mask it using torch.topk
. However, in torch.topk
, k
is an integer and remains same for the dimension specified. I need different value of k
for every row, so that I can sample variable number of top k elements and turn them into 1s.