Pytorch如何使用夹子打乱数据的操作

  介绍

这篇文章主要介绍Pytorch如何使用夹子打乱数据的操作,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

首先我得告诉你一件事,那就是Pytorch中的张量,如果直接使用随机的。洗牌打乱数据,或者使用下面的方式,自己定义直接写。

, def 洗牌(自我,,x,, y,随机=None, int=int):   ,,,,,,,,if  random  is 没有:   ,,,,,,,,,,,random =self.random   ,,,,,,,,,,,,,,,,for 小姐:拷贝范围(len (x)):   ,,,,,,,,,,,j =, int(随机(),*,(小姐:+,- 1))   ,,,,,,,,,,,if  j

那你就会收获一堆的混乱数据,因为使用这种交换的方式对张量类型的数据进行操作,会导致里面的数据出现重复复制的问题。

比如我y中的数据为【0,- 1,0,1,0,1】

在经过几次洗牌,其中的数据就变成了【1,1,1,1,1,1】。

数据顿时出现混乱。

正确的方式是先转成numpy,再进行交换数据

比如:

, def 洗牌(自我,,x,, y,随机=None, int=int):   ,,,,,,,,,,x,,随机=random.random →, shuffle  list  x 拷贝,return 没有一个。   ,,,,,,,Optional  arg  random  is  a  0-argument  function  returning  a 随机的   ,,,,,,,float 拷贝(0.0,1.0);,by 默认情况下,,,standard  random.random。   ,,,,,,,,,,   ,,,,,,,if  random  is 没有:   ,,,,,,,,,,,random =, self.random 随机=random.random #   ,,,,,,,#转成numpy   ,,,,,,,if  torch.is_tensor (x)==True:   ,,,,,,,,,,,if  self.use_cuda==True:   ,,,,,,,,,,,,,,x=x.cpu () .numpy ()   ,,,,,,,,,,,其他的:   ,,,,,,,,,,,,,,x=x.numpy ()   ,,,,,,,if  torch.is_tensor (y),==,真的:   ,,,,,,,,,,,if  self.use_cuda==True:   ,,,,,,,,,,,,,,y=y.cpu () .numpy ()   ,,,,,,,,,,,其他的:   ,,,,,,,,,,,,,,y=y.numpy ()   ,,,,,,,#开始随机置换   ,,,,,,,for 小姐:拷贝范围(len (x)):   ,,,,,,,,,,,j =, int(随机(),*,(小姐:+,- 1))   ,,,,,,,,,,,if  j

<强>补充:python对训练数据集shuffle(打乱)的一些方式

1。通过数组来洗牌

image_list=[],,,,,,,,,,, #, list  of 图像   label_list=[],,,,,,,,,,, #, list  of 标签   ,   时间=temp  np.array ([image_list, label_list])   时间=temp  temp.transpose ()   np.random.shuffle(临时)   ,   null   null   null   null   null   null   null   null   null   null   null   null   null

Pytorch如何使用夹子打乱数据的操作