I want to have a random bit mask that has some specified percent of 0
s. The function I devised is:
JavaScript
x
14
14
1
def create_mask(shape, rate):
2
"""
3
The idea is, you take a random permutations of numbers. You then mod then
4
mod it by the [number of entries in the bitmask] / [percent of 0s you
5
want]. The number of zeros will be exactly the rate of zeros need. You
6
can clamp the values for a bitmask.
7
"""
8
mask = torch.randperm(reduce(operator.mul, shape, 1)).float().cuda()
9
# Mod it by the percent to get an even dist of 0s.
10
mask = torch.fmod(mask, reduce(operator.mul, shape, 1) / rate)
11
# Anything not zero should be put to 1
12
mask = torch.clamp(mask, 0, 1)
13
return mask.view(shape)
14
To illustrate:
JavaScript
1
15
15
1
>>> x = create_mask((10, 10), 10)
2
>>> x
3
4
1 1 1 1 1 1 1 1 1 1
5
1 1 1 1 1 1 0 1 1 1
6
0 1 1 1 1 0 1 1 1 1
7
0 1 1 1 1 1 1 1 1 1
8
1 1 1 1 1 1 1 1 1 0
9
1 1 1 1 1 1 1 1 1 1
10
1 1 1 0 1 1 1 0 1 1
11
0 1 1 1 1 1 1 1 1 1
12
1 1 1 0 1 1 0 1 1 1
13
1 1 1 1 1 1 1 1 1 1
14
[torch.cuda.FloatTensor of size 10x10 (GPU 0)]
15
The main issue I have with this method is it requires the rate
to divide the shape
. I want a function that accepts an arbitrary decimal and gives approximately rate
percent of 0s in the bitmask. Furthermore, I am trying to find a relatively efficient way of doing so. Hence, I would rather not move a numpy
array from the CPU to the GPU. Is there an effiecient way of doing so that allows for a decimal rate
?
Advertisement
Answer
For anyone running into this, this will create a bitmask with approximately 80% zero’s directly on GPU. (PyTorch 0.3)
JavaScript
1
2
1
torch.cuda.FloatTensor(10, 10).uniform_() > 0.8
2