婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

主頁 > 知識庫 > Pytorch使用shuffle打亂數(shù)據(jù)的操作

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

熱門標(biāo)簽:武漢外呼系統(tǒng)平臺(tái) 外呼系統(tǒng)哪些好辦 江西省地圖標(biāo)注 沈陽人工外呼系統(tǒng)價(jià)格 池州外呼調(diào)研線路 如何申請400電話費(fèi)用 富錦商家地圖標(biāo)注 沈陽防封電銷卡品牌 沈陽外呼系統(tǒng)呼叫系統(tǒng)

這個(gè)東西算是我被這個(gè)shuffle坑了的一個(gè)總結(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

那你就會(huì)收獲一堆的混亂數(shù)據(jù),因?yàn)槭褂眠@種交換的方式對tensor類型的數(shù)據(jù)進(jìn)行操作,會(huì)導(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ù)頓時(shí)出現(xiàn)混亂。

正確的方式是先轉(zhuǎn)成numpy,再進(jìn)行交換數(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()
        #開始隨機(jī)置換
        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

補(bǔ)充: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]

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

您可能感興趣的文章:
  • Python深度學(xué)習(xí)之使用Pytorch搭建ShuffleNetv2
  • python 如何查看pytorch版本
  • 簡述python&pytorch 隨機(jī)種子的實(shí)現(xiàn)
  • 淺談pytorch、cuda、python的版本對齊問題
  • python、PyTorch圖像讀取與numpy轉(zhuǎn)換實(shí)例
  • 基于python及pytorch中乘法的使用詳解
  • python PyTorch參數(shù)初始化和Finetune
  • python PyTorch預(yù)訓(xùn)練示例
  • 我對PyTorch dataloader里的shuffle=True的理解
  • pytorch 帶batch的tensor類型圖像顯示操作
  • Python深度學(xué)習(xí)之Pytorch初步使用

標(biāo)簽:潛江 株洲 銅川 通遼 呂梁 常德 阿里 黑龍江

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Pytorch使用shuffle打亂數(shù)據(jù)的操作》,本文關(guān)鍵詞  Pytorch,使用,shuffle,打亂,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《Pytorch使用shuffle打亂數(shù)據(jù)的操作》相關(guān)的同類信息!
  • 本頁收集關(guān)于Pytorch使用shuffle打亂數(shù)據(jù)的操作的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    主站蜘蛛池模板: 洛宁县| 梅河口市| 新绛县| 甘南县| 庐江县| 乡宁县| 任丘市| 宜春市| 曲沃县| 海林市| 久治县| 若尔盖县| 礼泉县| 宿州市| 宝山区| 宾川县| 天柱县| 绥江县| 临洮县| 磐石市| 诸城市| 林口县| 昌乐县| 榆社县| 宁化县| 赣榆县| 凭祥市| 柏乡县| 望城县| 乌拉特前旗| 汪清县| 自治县| 五大连池市| 深泽县| 会同县| 宜君县| 白银市| 五常市| 乌鲁木齐县| 陕西省| 英吉沙县|