当前位置:网站首页>[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))

边栏推荐
- Spark ML学习相关资料整理
- flink on yarn 集群模式启动报错及解决方案汇总
- 《基于机器视觉的输电线路交叉点在线测量方法及技术方案》论文笔记
- Flink HA配置
- 11%的参数就能优于Swin,微软提出快速预训练蒸馏方法TinyViT
- 华科提出首个用于伪装实例分割的一阶段框架OSFormer
- 【MySQL】数据库多表链接的查询方式
- 【论文阅读-表情捕捉】High-quality Real Time Facial Capture Based on Single Camera
- 【22李宏毅机器学习】课程大纲概述
- 【论文阅读-表情捕捉】ExpNet: Landmark-Free, Deep, 3D Facial Expressions
猜你喜欢
![[Go through 4] 09-10_Classic network analysis](/img/f2/e6e71869b8ab014cc1eea0537fc2e7.png)
[Go through 4] 09-10_Classic network analysis
![[Pytorch study notes] 9. How to evaluate the classification results of the classifier - using confusion matrix, F1-score, ROC curve, PR curve, etc. (taking Softmax binary classification as an example)](/img/ac/884d8aba8b9d363e3b9ae6de33d5a4.png)
[Pytorch study notes] 9. How to evaluate the classification results of the classifier - using confusion matrix, F1-score, ROC curve, PR curve, etc. (taking Softmax binary classification as an example)

11%的参数就能优于Swin,微软提出快速预训练蒸馏方法TinyViT

关于基于若依框架的路由跳转

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

Flutter 3.0升级内容,该如何与小程序结合

AIDL detailed explanation

spingboot 容器项目完成CICD部署
![[After a 12] No record for a whole week](/img/05/df9aeb04274e308e1341020f836821.jpg)
[After a 12] No record for a whole week

ECCV2022 | RU&谷歌提出用CLIP进行zero-shot目标检测!
随机推荐
[Go through 9] Convolution
【Pytorch学习笔记】9.分类器的分类结果如何评估——使用混淆矩阵、F1-score、ROC曲线、PR曲线等(以Softmax二分类为例)
盘点关于发顶会顶刊论文,你需要知道写作上的这些事情!
MySQL
ES6基础语法
Lecture 3 Gradient Tutorial Gradient Descent and Stochastic Gradient Descent
Oracle压缩表修改字段的处理方法
[Remember 1] June 29, 2022 Brother and brother double pain
Spark ML学习相关资料整理
初识机器学习
读论文- pix2pix
Service
SQL (2) - join window function view
SSL 证书签发详细攻略
【Pytorch学习笔记】10.如何快速创建一个自己的Dataset数据集对象(继承Dataset类并重写对应方法)
[Practice 1] Diabetes Genetic Risk Detection Challenge [IFLYTEK Open Platform]
《基于机器视觉的输电线路交叉点在线测量方法及技术方案》论文笔记
数控直流电源
CVPR最佳论文得主清华黄高团队提出首篇动态网络综述
【Pytorch学习笔记】8.训练类别不均衡数据时,如何使用WeightedRandomSampler(权重采样器)