有没有一种在 Pytorch 中创建随机位掩码的有效方法?
Is there an efficient way to create a random bit mask in Pytorch?
我想要一个具有特定百分比 0
的随机位掩码。我设计的函数是:
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)
举例说明:
>>> 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)]
我对这种方法的主要问题是它需要 rate
来划分 shape
。我想要一个接受任意小数并在位掩码中给出大约 rate
% 的 0 的函数。此外,我正在尝试找到一种相对有效的方法。因此,我宁愿不将 numpy
数组从 CPU 移动到 GPU。有没有一种有效的方法允许小数 rate
?
使用 NumPy 和 cudamat:
import numpy as np
import cudamat as cm
gpuMask = cm.CUDAMatrix(np.random.choice([0, 1], size=(10,10), p=[1./2, 1./2]))
其中列表的元素是 1 和 0 概率的分数表示。
对于任何 运行 参与其中的人,这将直接在 GPU 上创建一个大约 80% 为零的位掩码。 (PyTorch 0.3)
torch.cuda.FloatTensor(10, 10).uniform_() > 0.8
使用 Pytorch 直接在 GPU 上创建位掩码的正确方法是:
import torch
tensor = torch.randn((3, 5), device=torch.device("cuda")) < 0.9
# tensor([[ True, True, False, True, True, True, True, True, True, False],
# [ True, True, True, True, True, True, True, False, False, True],
# [ True, False, False, True, True, True, True, True, True, False],
# [ True, True, True, True, True, True, True, True, True, True],
# [ True, True, False, True, True, True, True, False, True, True],
# [ True, True, False, False, True, True, True, False, True, True],
# [ True, True, True, False, True, True, True, True, True, True],
# [ True, True, True, True, True, True, False, False, True, True],
# [ True, False, True, True, True, True, True, True, True, True],
# [ True, True, True, True, True, True, True, True, False, True]],
# device='cuda:0')
我想要一个具有特定百分比 0
的随机位掩码。我设计的函数是:
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)
举例说明:
>>> 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)]
我对这种方法的主要问题是它需要 rate
来划分 shape
。我想要一个接受任意小数并在位掩码中给出大约 rate
% 的 0 的函数。此外,我正在尝试找到一种相对有效的方法。因此,我宁愿不将 numpy
数组从 CPU 移动到 GPU。有没有一种有效的方法允许小数 rate
?
使用 NumPy 和 cudamat:
import numpy as np
import cudamat as cm
gpuMask = cm.CUDAMatrix(np.random.choice([0, 1], size=(10,10), p=[1./2, 1./2]))
其中列表的元素是 1 和 0 概率的分数表示。
对于任何 运行 参与其中的人,这将直接在 GPU 上创建一个大约 80% 为零的位掩码。 (PyTorch 0.3)
torch.cuda.FloatTensor(10, 10).uniform_() > 0.8
使用 Pytorch 直接在 GPU 上创建位掩码的正确方法是:
import torch
tensor = torch.randn((3, 5), device=torch.device("cuda")) < 0.9
# tensor([[ True, True, False, True, True, True, True, True, True, False],
# [ True, True, True, True, True, True, True, False, False, True],
# [ True, False, False, True, True, True, True, True, True, False],
# [ True, True, True, True, True, True, True, True, True, True],
# [ True, True, False, True, True, True, True, False, True, True],
# [ True, True, False, False, True, True, True, False, True, True],
# [ True, True, True, False, True, True, True, True, True, True],
# [ True, True, True, True, True, True, False, False, True, True],
# [ True, False, True, True, True, True, True, True, True, True],
# [ True, True, True, True, True, True, True, True, False, True]],
# device='cuda:0')