Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an efficient way to create a random bit mask in Pytorch?

Tags:

python

pytorch

I want to have a random bit mask that has some specified percent of 0s. The function I devised is:

def create_mask(shape, rate):
    """
    The idea is, you take a random permutations of numbers. You then mod then
    mod it by the [number of entries in the bitmask] / [percent of 0s you
    want]. The number of zeros will be exactly the rate of zeros need. You
    can clamp the values for a bitmask.
    """
    mask = torch.randperm(reduce(operator.mul, shape, 1)).float().cuda()
    # Mod it by the percent to get an even dist of 0s.
    mask = torch.fmod(mask, reduce(operator.mul, shape, 1) / rate)
    # Anything not zero should be put to 1
    mask = torch.clamp(mask, 0, 1)
    return mask.view(shape)

To illustrate:

>>> x = create_mask((10, 10), 10)
>>> x

    1     1     1     1     1     1     1     1     1     1
    1     1     1     1     1     1     0     1     1     1
    0     1     1     1     1     0     1     1     1     1
    0     1     1     1     1     1     1     1     1     1
    1     1     1     1     1     1     1     1     1     0
    1     1     1     1     1     1     1     1     1     1
    1     1     1     0     1     1     1     0     1     1
    0     1     1     1     1     1     1     1     1     1
    1     1     1     0     1     1     0     1     1     1
    1     1     1     1     1     1     1     1     1     1
[torch.cuda.FloatTensor of size 10x10 (GPU 0)]

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?

like image 412
Dair Avatar asked Mar 11 '18 04:03

Dair


People also ask

How do you make a random tensor in PyTorch?

We can create the PyTorch random tensor containing random values in the range of 0 to 1 simply by importing the torch library in your program and then use the rand function to create your tensor by passing the required size of the output tensor in the parameter.

How do you generate a random number in torch?

For initializing random number in PyTorch we have to use torch. rand() function in which the output will be a tensor with random numbers in it from a uniform distribution on the interval and the tensor shape is defined by the variable argument.

How do I use PyTorch mask?

Simply type-cast your boolean mask to an integer mask, followed by float to bring the mask to the same type as in img . Perform element-wise multiplication afterwards.


1 Answers

For anyone running into this, this will create a bitmask with approximately 80% zero's directly on GPU. (PyTorch 0.3)

torch.cuda.FloatTensor(10, 10).uniform_() > 0.8
like image 176
Wouter Avatar answered Nov 11 '22 20:11

Wouter