当前位置: 首页 > news >正文

PyTorch学习笔记:data.RandomSampler——数据随机采样

PyTorch学习笔记:data.RandomSampler——数据随机采样

torch.utils.data.RandomSampler(data_source, replacement=False, num_samples=None, generator=None)

功能:随即对样本进行采样

输入:

  • 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设为TrueFalse的区别

from torch.utils.data import RandomSamplersampler_t = RandomSampler(range(20), replacement=True)
sampler_f = RandomSampler(range(20), replacement=False)
sampler_t_8 = RandomSampler(range(20), replacement=True, num_samples=8)
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.RandomSampler:https://pytorch.org/docs/stable/data.html?highlight=randomsampler#torch.utils.data.RandomSampler

初步完稿于:2022年2月22日

http://www.lryc.cn/news/1246.html

相关文章:

  • 设计模式(七)----创建型模式之建造者模式
  • DCGAN
  • 【速通版】吴恩达机器学习笔记Part3
  • 【leetcode】跳跃游戏
  • 论文投稿指南——中文核心期刊推荐(冶金工业 2)
  • 【GPLT 二阶题目集】L2-044 大众情人
  • SpringBoot整合(二)MyBatisPlus技术详解
  • 导入importk8s集群,添加node节点,rancher agent,Rancher Agent设置选项
  • C++11--右值引用与移动语义
  • Python SQLAlchemy入门教程
  • 你是真的“C”——操作符详解【下篇】+整形提升+算术转换
  • 文本匹配SimCSE模型代码详解以及训练自己的中文数据集
  • Biotin-PEG-FITC 生物素聚乙二醇荧光素;FITC-PEG-Biotin 科研用生物试剂
  • FISCO BCOS 搭建区块链,在SpringBoot中调用合约
  • 面试官:int和Integer有什么区别?
  • MFC常用技巧
  • C++ —— 多态
  • java agent设计开发概要
  • node.js笔记-模块化(commonJS规范),包与npm(Node Package Manager)
  • Linux 磁盘坏块修复处理(错误:read error: Input/output error)
  • API 面试四连杀:接口如何设计?安全如何保证?签名如何实现?防重如何实现?
  • 操作系统题目收录(六)
  • 2023年十款开源测试开发工具推荐!
  • MySQL慢查询分析和性能优化
  • C++学习笔记(四)
  • 【4】深度学习之Pytorch——如何使用张量处理时间序列数据集(共享自行车数据集)
  • mulesoft MCIA 破釜沉舟备考 2023.02.10.01
  • 干货 | PCB拼板,那几条很讲究的规则!
  • 笔试题-2023-思远半导体-数字IC设计【纯净题目版】
  • canvas根据坐标点位画图形-canvas拖拽编辑单个图形形状