Pytorch使用shuffle打乱数据的操作
作者:永远的小白虾 时间:2021-10-03 08:33:31
这个东西算是我被这个shuffle坑了的一个总结吧!
首先我得告诉你一件事,那就是pytorch中的tensor,如果直接使用random.shuffle打乱数据,或者使用下面的方式,自己定义直接写。
def Shuffle(self, x, y,random=None, int=int):
if random is None:
random = self.random
for i in range(len(x)):
j = int(random() * (i + 1))
if j<=len(x)-1:
x[i],x[j]=x[j],x[i]
y[i],y[j]=y[j],y[i]
retrun x,y
那你就会收获一堆的混乱数据,因为使用这种交换的方式对tensor类型的数据进行操作,会导致里面的数据出现重复复制的问题。
比如我y中的数据为【0,1,0,1,0,1】
在经过几次shuffle,其中的数据就变成了【1,1,1,1,1,1】。
数据顿时出现混乱。
正确的方式是先转成numpy,再进行交换数据
比如:
def Shuffle(self, x, y,random=None, int=int):
"""x, random=random.random -> shuffle list x in place; return None.
Optional arg random is a 0-argument function returning a random
float in [0.0, 1.0); by default, the standard random.random.
"""
if random is None:
random = self.random #random=random.random
#转成numpy
if torch.is_tensor(x)==True:
if self.use_cuda==True:
x=x.cpu().numpy()
else:
x=x.numpy()
if torch.is_tensor(y) == True:
if self.use_cuda==True:
y=y.cpu().numpy()
else:
y=y.numpy()
#开始随机置换
for i in range(len(x)):
j = int(random() * (i + 1))
if j<=len(x)-1:#交换
x[i],x[j]=x[j],x[i]
y[i],y[j]=y[j],y[i]
#转回tensor
if self.use_cuda == True:
x=torch.from_numpy(x).cuda()
y=torch.from_numpy(y).cuda()
else:
x = torch.from_numpy(x)
y = torch.from_numpy(y)
return x,y
补充:python对训练数据集shuffle(打乱)的一些方式
1.通过数组来shuffle
image_list=[] # list of images
label_list=[] # list of labels
temp = np.array([image_list, label_list])
temp = temp.transpose()
np.random.shuffle(temp)
images = temp[:, 0] # array of images (N,)
labels = temp[:, 1]
2.通过索引 Index 来 shuffle
image_list=[] # list of images
label_list=[] # list of labels
##如果image_list存的是读取的特征数据,而不是图片路径,不要注释后面两句(list无法索引内部list)
#[list indices must be integers or slices, not list]
#image_list = np.array(image_list)
#label_list = np.array(label_list)
index = [i for i in range(len(image_list))]
np.random.shuffle(index)
images = image_list[index]
labels = label_list[index]
来源:https://blog.csdn.net/qq_41487299/article/details/107424432
标签:Pytorch,shuffle,打乱,数据
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
pytest allure添加环境信息实例讲解
2023-07-19 17:51:45
![](https://img.aspxhome.com/file/2023/8/89098_0s.png)
scrapy-redis的安装部署步骤讲解
2023-03-18 11:53:41
DHTML+XML+ASP+CSS的树形目录
2008-10-24 14:29:00
详解PyQt5 事件处理机制
2023-04-06 00:09:34
![](https://img.aspxhome.com/file/2023/8/81878_0s.gif)
Python实现在线批量美颜功能过程解析
2023-10-27 10:38:34
![](https://img.aspxhome.com/file/2023/1/79731_0s.jpg)
asp分段插入数据库
2010-07-02 13:13:00
python time.strptime格式化实例详解
2022-03-25 19:13:02
使用Javascript实现选择下拉菜单互移并排序
2023-09-07 18:14:45
![](https://img.aspxhome.com/file/2023/2/56302_0s.png)
在生成的静态页面中统计点击次数
2009-11-19 13:20:00
Python制作当年第一款手机游戏-贪吃蛇游戏(练习)
2022-06-20 02:37:35
![](https://img.aspxhome.com/file/2023/0/79540_0s.gif)
归纳整理Python中的控制流语句的知识点
2021-03-04 12:07:12
redux-saga 初识和使用
2023-07-15 16:20:17
使用Python生成XML的方法实例
2022-10-01 19:20:15
如何在windows下安装Pycham2020软件(方法步骤详解)
2023-05-19 14:07:30
![](https://img.aspxhome.com/file/2023/0/89300_0s.png)
python用pandas数据加载、存储与文件格式的实例
2021-09-05 03:44:47
python中heapq堆排算法的实现
2022-10-13 23:11:40
[关注细节的最佳方案]有效期时间格式的展现
2009-10-30 18:51:00
![](https://img.aspxhome.com/file/UploadPic/200910/30/validity1-63s.gif)
超详细注释之OpenCV制作图像Mask
2021-10-20 14:15:34
![](https://img.aspxhome.com/file/2023/3/98793_0s.jpg)
Pytorch学习笔记DCGAN极简入门教程
2022-05-28 17:29:02
Python数据类型详解(三)元祖:tuple
2021-05-17 07:49:14