Is there an efficient way to create a random bit mask in Pytorch?(有没有一种在 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 0
s. 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 中创建随机位掩码的有效方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:有没有一种在 Pytorch 中创建随机位掩码的有效方


- YouTube API v3 返回截断的观看记录 2022-01-01
- ";find_element_by_name(';name';)";和&QOOT;FIND_ELEMENT(BY NAME,';NAME';)";之间有什么区别? 2022-01-01
- 使用 Cython 将 Python 链接到共享库 2022-01-01
- 如何使用PYSPARK从Spark获得批次行 2022-01-01
- 我如何卸载 PyTorch? 2022-01-01
- 我如何透明地重定向一个Python导入? 2022-01-01
- 使用公司代理使Python3.x Slack(松弛客户端) 2022-01-01
- CTR 中的 AES 如何用于 Python 和 PyCrypto? 2022-01-01
- 计算测试数量的Python单元测试 2022-01-01
- 检查具有纬度和经度的地理点是否在 shapefile 中 2022-01-01