欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Pytorch使用shuffle打亂數(shù)據(jù)的操作

 更新時間:2021年05月20日 15:09:17   作者:永遠的小白蝦  
這篇文章主要介紹了Pytorch使用shuffle打亂數(shù)據(jù)的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

這個東西算是我被這個shuffle坑了的一個總結(jié)吧!

首先我得告訴你一件事,那就是pytorch中的tensor,如果直接使用random.shuffle打亂數(shù)據(jù),或者使用下面的方式,自己定義直接寫。

 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

那你就會收獲一堆的混亂數(shù)據(jù),因為使用這種交換的方式對tensor類型的數(shù)據(jù)進行操作,會導(dǎo)致里面的數(shù)據(jù)出現(xiàn)重復(fù)復(fù)制的問題。

比如我y中的數(shù)據(jù)為【0,1,0,1,0,1】

在經(jīng)過幾次shuffle,其中的數(shù)據(jù)就變成了【1,1,1,1,1,1】。

數(shù)據(jù)頓時出現(xiàn)混亂。

正確的方式是先轉(zhuǎn)成numpy,再進行交換數(shù)據(jù)

比如:

 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
        #轉(zhuǎn)成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]
        #轉(zhuǎn)回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對訓(xùn)練數(shù)據(jù)集shuffle(打亂)的一些方式

1.通過數(shù)組來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存的是讀取的特征數(shù)據(jù),而不是圖片路徑,不要注釋后面兩句(list無法索引內(nèi)部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]

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • python讀取txt文件中特定位置字符的方法

    python讀取txt文件中特定位置字符的方法

    今天小編就為大家分享一篇python讀取txt文件中特定位置字符的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-12-12
  • Python+Pandas 獲取數(shù)據(jù)庫并加入DataFrame的實例

    Python+Pandas 獲取數(shù)據(jù)庫并加入DataFrame的實例

    今天小編就為大家分享一篇Python+Pandas 獲取數(shù)據(jù)庫并加入DataFrame的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-07-07
  • python 如何設(shè)置柱狀圖參數(shù)

    python 如何設(shè)置柱狀圖參數(shù)

    這篇文章主要介紹了在python中設(shè)置柱狀圖參數(shù)的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-05-05
  • python之Socket網(wǎng)絡(luò)編程詳解

    python之Socket網(wǎng)絡(luò)編程詳解

    這篇文章主要為大家詳細介紹了python之Socket網(wǎng)絡(luò)編程,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-09-09
  • python+django+rest框架配置創(chuàng)建方法

    python+django+rest框架配置創(chuàng)建方法

    今天小編就為大家分享一篇python+django+rest框架配置創(chuàng)建方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-08-08
  • Jupyter Notebook/VSCode導(dǎo)出PDF中文不顯示的解決

    Jupyter Notebook/VSCode導(dǎo)出PDF中文不顯示的解決

    這篇文章主要介紹了Jupyter Notebook/VSCode導(dǎo)出PDF中文不顯示的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-06-06
  • Python Dataframe常見索引方式詳解

    Python Dataframe常見索引方式詳解

    這篇文章主要介紹了Python Dataframe常見索引方式詳解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-05-05
  • pandas缺失值np.nan, np.isnan, None, pd.isnull, pd.isna

    pandas缺失值np.nan, np.isnan, None, pd.isnull,&n

    本文主要介紹了pandas缺失值np.nan, np.isnan, None, pd.isnull, pd.isna
    2024-04-04
  • Python制作春聯(lián)的示例代碼

    Python制作春聯(lián)的示例代碼

    春聯(lián)是中國傳統(tǒng)文化中最具內(nèi)涵的元素之一,它以對仗工整、簡潔精巧的文字描繪美好形象,抒發(fā)美好愿望,是中國特有的文學(xué)形式,是華人們過年的重要習(xí)俗。本文將通過Python制作春聯(lián),需要的可以參考一下
    2022-01-01
  • python實現(xiàn)k-means聚類算法

    python實現(xiàn)k-means聚類算法

    這篇文章主要為大家詳細介紹了python實現(xiàn)k-means聚類算法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-02-02

最新評論