方法1

可以使用 PyTorch 中的 torch.distributions 模块实现两点分布采样。具体来说,可以使用 Categorical 分布将数字1和数字2的概率分别设为0.2和0.8,然后调用 sample() 方法进行采样。

下面是实现上述功能的代码示例:

import torch
# 创建 Categorical 分布(数字1和数字2的概率分别设为0.2和0.8)
probs = torch.tensor([0.2, 0.8])
dist = torch.distributions.Categorical(probs)
# 从分布中采样100个样本
samples = dist.sample(torch.Size([100]))
# 统计样本中数字1和数字2的数量
count_1 = torch.sum(samples == 0)
count_2 = torch.sum(samples == 1)
print(f"数字1的数量:{count_1}")
print(f"数字2的数量:{count_2}")

输出结果类似于:

数字1的数量:22
数字2的数量:78

方法2

可以先使用 torch.ones()torch.zeros() 函数生成分别包含20个数字1和80个数字2的张量,然后使用 torch.cat() 函数将它们拼接在一起,再使用 torch.randperm() 函数对其进行打乱。

下面是实现上述功能的代码示例:

import torch
# 生成包含20个数字1和80个数字2的张量,并拼接在一起
ones_tensor = torch.ones(20)
zeros_tensor = torch.zeros(80)
data_tensor = torch.cat([ones_tensor, zeros_tensor], dim=0)
# 打乱张量中的元素顺序
shuffled_tensor = data_tensor[torch.randperm(data_tensor.shape[0])]
print(shuffled_tensor)

输出结果为:

tensor([0., 0., 0., 0., 0., 0., 0., 0., 1., 0., 1., 0., 0., 0., 1., 0., 0., 1.,
        1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1., 1., 0., 0., 0., 0.,
        0., 0., 0., 0., 0., 0., 0., 0., 0., 1., 1., 0., 0., 0., 0., 1., 0., 0.,
        0., 0., 0., 0., 1., 0., 0., 0., 1., 1., 0., 0., 1., 0., 0., 0., 0., 1.,
        1., 0., 0., 0., 0., 1., 1., 0., 0., 0., 0., 1., 0., 0., 0., 0., 0., 0.,
        0., 0., 0., 0., 0., 0., 0., 0., 0., 1.])

其中,数字1被表示为1.0,数字2被表示为2.0。