有没有一种在 Pytorch 中创建随机位掩码的有效方法? [英] Is there an efficient way to create a random bit mask in Pytorch?

查看:46
本文介绍了有没有一种在 Pytorch 中创建随机位掩码的有效方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个随机位掩码,它具有指定的 0 百分比.我设计的功能是:

def create_mask(shape, rate):"""这个想法是,你对数字进行随机排列.然后你再修改通过 [位掩码中的条目数]/[0 的百分比你想].零的数量将恰好是零所需的比率.你可以限制位掩码的值."""mask = torch.randperm(reduce(operator.mul, shape, 1)).float().cuda()# 按百分比修改它以获得 0 的均匀分布.mask = torch.fmod(mask, reduce(operator.mul, shape, 1)/rate)# 任何非零都应置为 1面具 = torch.clamp(面具,0, 1)返回 mask.view(shape)

举例说明:

<预><代码>>>>x = create_mask((10, 10), 10)>>>X1 1 1 1 1 1 1 1 1 11 1 1 1 1 1 0 1 1 10 1 1 1 1 0 1 1 1 10 1 1 1 1 1 1 1 1 11 1 1 1 1 1 1 1 1 01 1 1 1 1 1 1 1 1 11 1 1 0 1 1 1 0 1 10 1 1 1 1 1 1 1 1 11 1 1 0 1 1 0 1 1 11 1 1 1 1 1 1 1 1 1[torch.cuda.FloatTensor 大小为 10x10 (GPU 0)]

我使用这种方法的主要问题是它需要 rate 来划分 shape.我想要一个接受任意十进制数并在位掩码中给出大约 rate 0 百分比的函数.此外,我正在尝试找到一种相对有效的方法.因此,我宁愿不将 numpy 数组从 CPU 移动到 GPU.是否有一种有效的方法可以允许使用十进制 rate?

解决方案

对于遇到此问题的任何人,这将直接在 GPU 上创建一个大约 80% 为零的位掩码.(PyTorch 0.3)

torch.cuda.FloatTensor(10, 10).uniform_() >0.8

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?

解决方案

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

这篇关于有没有一种在 Pytorch 中创建随机位掩码的有效方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆