选择佛山网站设计,域名 不做网站,自建站 外贸,快手推广网站PyTorch学习笔记#xff1a;data.RandomSampler——数据随机采样
torch.utils.data.RandomSampler(data_source, replacementFalse, num_samplesNone, generatorNone)功能#xff1a;随即对样本进行采样
输入#xff1a;
data_source#xff1a;被采样的数据集合replace…PyTorch学习笔记data.RandomSampler——数据随机采样
torch.utils.data.RandomSampler(data_source, replacementFalse, num_samplesNone, generatorNone)功能随即对样本进行采样
输入
data_source被采样的数据集合replacement采样策略如果为True则代表使用替换采样策略即可重复对一个样本进行采样如果为False则表示不用替换采样策略即一个样本最多只能被采一次num_samples所采样本的数量默认采全部样本当replacement规定为True时可指定采样数量即修改num_samples的大小如果replacement设置为False则该参数不可做修改generator采样过程中的生成器
代码案例
一般用法
from torch.utils.data import RandomSamplersampler RandomSampler(range(20))
print([i for i in sampler])输出
这里相当于对原数据做了打乱操作
[7, 17, 8, 1, 13, 9, 6, 4, 12, 18, 19, 14, 10, 3, 2, 16, 5, 15, 0, 11]replacement设为True与False的区别
from torch.utils.data import RandomSamplersampler_t RandomSampler(range(20), replacementTrue)
sampler_f RandomSampler(range(20), replacementFalse)
sampler_t_8 RandomSampler(range(20), replacementTrue, num_samples8)
print(sampler_t:, [i for i in sampler_t])
print(sampler_f:, [i for i in sampler_f])
print(sampler_t_8:, [i for i in sampler_t_8])输出
# replacement设为True时会对同一样本多次采样
sampler_t: [7, 3, 13, 17, 4, 5, 8, 18, 15, 8, 1, 3, 17, 4, 13, 13, 16, 14, 15, 11]
# 否则一个样本只采样一次
sampler_f: [3, 5, 19, 10, 6, 7, 13, 16, 15, 9, 14, 0, 4, 18, 12, 2, 11, 17, 1, 8]
# replacement设为True时可以规定采样数量如这里只采8个
sampler_t_8: [1, 9, 4, 5, 11, 18, 18, 4]官方文档 torch.utils.data.RandomSamplerhttps://pytorch.org/docs/stable/data.html?highlightrandomsampler#torch.utils.data.RandomSampler 初步完稿于2022年2月22日