我對PyTorch dataloader里的shuffle=True的理解
對shuffle=True的理解:
之前不了解shuffle的實際效果,假設(shè)有數(shù)據(jù)a,b,c,d,不知道batch_size=2后打亂,具體是如下哪一種情況:
1.先按順序取batch,對batch內(nèi)打亂,即先取a,b,a,b進行打亂;
2.先打亂,再取batch。
證明是第二種
shuffle (bool, optional): set to ``True`` to have the data reshuffled at every epoch (default: ``False``). if shuffle: sampler = RandomSampler(dataset) #此時得到的是索引
補充:簡單測試一下pytorch dataloader里的shuffle=True是如何工作的
看代碼吧~
import sys import torch import random import argparse import numpy as np import pandas as pd import torch.nn as nn from torch.nn import functional as F from torch.optim import lr_scheduler from torchvision import datasets, transforms from torch.utils.data import TensorDataset, DataLoader, Dataset class DealDataset(Dataset): def __init__(self): xy = np.loadtxt(open('./iris.csv','rb'), delimiter=',', dtype=np.float32) #data = pd.read_csv("iris.csv",header=None) #xy = data.values self.x_data = torch.from_numpy(xy[:, 0:-1]) self.y_data = torch.from_numpy(xy[:, [-1]]) self.len = xy.shape[0] def __getitem__(self, index): return self.x_data[index], self.y_data[index] def __len__(self): return self.len dealDataset = DealDataset() train_loader2 = DataLoader(dataset=dealDataset, batch_size=2, shuffle=True) #print(dealDataset.x_data) for i, data in enumerate(train_loader2): inputs, labels = data #inputs, labels = Variable(inputs), Variable(labels) print(inputs) #print("epoch:", epoch, "的第" , i, "個inputs", inputs.data.size(), "labels", labels.data.size())
簡易數(shù)據(jù)集
shuffle之后的結(jié)果,每次都是隨機打亂,然后分成大小為n的若干個mini-batch.
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
使用CodeMirror實現(xiàn)Python3在線編輯器的示例代碼
這篇文章主要介紹了使用CodeMirror實現(xiàn)Python3在線編輯器的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-01-01python循環(huán)神經(jīng)網(wǎng)絡(luò)RNN函數(shù)tf.nn.dynamic_rnn使用
這篇文章主要為大家介紹了python循環(huán)神經(jīng)網(wǎng)絡(luò)RNN的tf.nn.dynamic_rnn使用示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-05-05Python利用shutil模塊實現(xiàn)文件夾的復(fù)制刪除與裁剪
shutil模塊是對os模塊的補充,主要針對文件的拷貝、刪除、移動、壓縮和解壓操作。本文將利用shutil模塊實現(xiàn)文件夾的復(fù)制刪除與裁剪,需要的可以參考一下2022-05-05Python中的復(fù)雜數(shù)據(jù)類型(list、tuple)
這篇文章介紹了Python中的復(fù)雜數(shù)據(jù)類型(list、tuple),文中通過示例代碼介紹的非常詳細。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-05-05