当前位置:网站首页>[Pytorch study notes] 11. Take a subset of the Dataset and shuffle the order of the Dataset (using Subset, random_split)
[Pytorch study notes] 11. Take a subset of the Dataset and shuffle the order of the Dataset (using Subset, random_split)
2022-08-05 05:42:00 【takedachia】
(pytorch版本:1.2)
我们在使用Dataset定义好数据集后,These problems are often encountered when dealing with datasets:如何把Dataset拆分成两个子集(as used to specify training and test sets、k折交叉验证等)?How to do random splits?How to scramble oneDataset内数据的顺序?
Dataset取子集、拆分
使用 torch.utils.data.Subset() Data sets can be subsetted.
传入一个Dataset,A sequence sliceindices,to get a subset.
1.我们可以传入一个range():
indices = range(18353) # Take the label as the first0个到第18352个数据
sub_imgs = torch.utils.data.Subset(imgs, indices)
len(imgs), len(sub_imgs)

2.interval can be taken:
indices = range(18353, 27153) # Take the label as the first18353个到第27152个数据
sub_imgs = torch.utils.data.Subset(imgs, indices)
len(imgs), len(sub_imgs)

3.可以传入一个List.有ListYou can use list comprehensions:
indices = [x for x in range(1234)]
sub_imgs = torch.utils.data.Subset(imgs, indices)
len(imgs), len(sub_imgs)

打乱Dataset内数据的顺序
We can pass in an out-of-order one directlyindexIt can achieve the purpose of out-of-order data set:
from torch import randperm
lenth = randperm(len(Leaf_dataset_train)).tolist() # Generate out-of-order indexes
rand_train = torch.utils.data.Subset(imgs, lenth)
# Show the first image、original label
X = rand_train[0]
plt.imshow(torch.transpose(X[0],0,2)), lenth[0]

After we shuffle the order, we can take subsets to perform on the datasetkfold cross-validation and other behaviors.
随机拆分Dataset
使用 torch.utils.data.random_split() The dataset can be split directly,Randomly divided into multiple portions.
可以传入一个List,注意传入的ListThe size of each subset is included in the sequence(数量),And the sum of these numbers must be等于传入Dataset的长度.
示例:
# 这里Leaf_dataset_trainmust be equal in size 17000+1353
train_set, test_set = torch.utils.data.random_split(Leaf_dataset_train, [17000, 1353])
print(len(train_set), len(test_set))

边栏推荐
- flink中文文档-目录v1.4
- My 的第一篇博客!!!
- 【零基础开发NFT智能合约】如何使用工具自动生成NFT智能合约带白名单可Mint无需写代码
- 关于基于若依框架的路由跳转
- SQL (2) - join window function view
- CVPR 2022 |节省70%的显存,训练速度提高2倍
- Lecture 3 Gradient Tutorial Gradient Descent and Stochastic Gradient Descent
- 【Pytorch学习笔记】9.分类器的分类结果如何评估——使用混淆矩阵、F1-score、ROC曲线、PR曲线等(以Softmax二分类为例)
- 【论文阅读-表情捕捉】ExpNet: Landmark-Free, Deep, 3D Facial Expressions
- CVPR2020 - 自校准卷积
猜你喜欢

flink中文文档-目录v1.4

基于STM32F407的一个温度传感器报警系统(用的是DS18B20温度传感器,4针0.96寸OLED显示屏,并且附带日期显示)

【数据库和SQL学习笔记】8.SQL中的视图(view)

沁恒MCU从EVT中提取文件建立MounRiver独立工程

Flink Distributed Cache 分布式缓存

【Pytorch学习笔记】10.如何快速创建一个自己的Dataset数据集对象(继承Dataset类并重写对应方法)

Detailed explanation of BroadCast Receiver (broadcast)

读论文 - Unpaired Portrait Drawing Generation via Asymmetric Cycle Mapping

记我的第一篇CCF-A会议论文|在经历六次被拒之后,我的论文终于中啦,耶!

It turns out that the MAE proposed by He Yuming is still a kind of data enhancement
随机推荐
[Practice 1] Diabetes Genetic Risk Detection Challenge [IFLYTEK Open Platform]
【22李宏毅机器学习】课程大纲概述
【论文阅读-表情捕捉】ExpNet: Landmark-Free, Deep, 3D Facial Expressions
【Pytorch学习笔记】8.训练类别不均衡数据时,如何使用WeightedRandomSampler(权重采样器)
TinyFlashDB:一种超轻量的可纠错的通用单片机flash存储方案
flink基本原理及应用场景分析
【数据库和SQL学习笔记】5.SELECT查询3:多表查询、连接查询
【Pytorch学习笔记】10.如何快速创建一个自己的Dataset数据集对象(继承Dataset类并重写对应方法)
CVPR 2022 | 70% memory savings, 2x faster training
【NFT开发】设计师无技术基础保姆级开发NFT教程在Opensea上全套开发一个NFT项目+构建Web3网站
2021电赛资源及经验总结
ECCV2022 | RU & Google propose zero-shot object detection with CLIP!
CVPR 2020 - 频谱正则化
MySql之索引
【Pytorch学习笔记】11.取Dataset的子集、给Dataset打乱顺序的方法(使用Subset、random_split)
MySQL
Flink Broadcast 广播变量
门徒Disciples体系:致力于成为“DAO世界”中的集大成者。
【论文精读】Rich Feature Hierarchies for Accurate Object Detection and Semantic Segmentation(R-CNN)
6k+ star,面向小白的深度学习代码库!一行代码实现所有Attention机制!