pytorch 自定義數(shù)據(jù)集加載方法
pytorch 官網(wǎng)給出的例子中都是使用了已經(jīng)定義好的特殊數(shù)據(jù)集接口來加載數(shù)據(jù),而且其使用的數(shù)據(jù)都是官方給出的數(shù)據(jù)。如果我們有自己收集的數(shù)據(jù)集,如何用來訓(xùn)練網(wǎng)絡(luò)呢?此時(shí)需要我們自己定義好數(shù)據(jù)處理接口。幸運(yùn)的是pytroch給出了一個(gè)數(shù)據(jù)集接口類(torch.utils.data.Dataset),可以方便我們繼承并實(shí)現(xiàn)自己的數(shù)據(jù)集接口。
torch.utils.data
torch的這個(gè)文件包含了一些關(guān)于數(shù)據(jù)集處理的類。
class torch.utils.data.Dataset: 一個(gè)抽象類, 所有其他類的數(shù)據(jù)集類都應(yīng)該是它的子類。而且其子類必須重載兩個(gè)重要的函數(shù):len(提供數(shù)據(jù)集的大?。?、getitem(支持整數(shù)索引)。
class torch.utils.data.TensorDataset: 封裝成tensor的數(shù)據(jù)集,每一個(gè)樣本都通過索引張量來獲得。
class torch.utils.data.ConcatDataset: 連接不同的數(shù)據(jù)集以構(gòu)成更大的新數(shù)據(jù)集。
class torch.utils.data.Subset(dataset, indices): 獲取指定一個(gè)索引序列對應(yīng)的子數(shù)據(jù)集。
class torch.utils.data.DataLoader(dataset, batch_size=1, shuffle=False, sampler=None, batch_sampler=None, num_workers=0, collate_fn=<function default_collate>, pin_memory=False, drop_last=False, timeout=0, worker_init_fn=None): 數(shù)據(jù)加載器。組合了一個(gè)數(shù)據(jù)集和采樣器,并提供關(guān)于數(shù)據(jù)的迭代器。
torch.utils.data.random_split(dataset, lengths): 按照給定的長度將數(shù)據(jù)集劃分成沒有重疊的新數(shù)據(jù)集組合。
class torch.utils.data.Sampler(data_source):所有采樣的器的基類。每個(gè)采樣器子類都需要提供 __iter__ 方法以方便迭代器進(jìn)行索引 和一個(gè) len方法 以方便返回迭代器的長度。
class torch.utils.data.SequentialSampler(data_source):順序采樣樣本,始終按照同一個(gè)順序。
class torch.utils.data.RandomSampler(data_source):無放回地隨機(jī)采樣樣本元素。
class torch.utils.data.SubsetRandomSampler(indices):無放回地按照給定的索引列表采樣樣本元素。
class torch.utils.data.WeightedRandomSampler(weights, num_samples, replacement=True): 按照給定的概率來采樣樣本。
class torch.utils.data.BatchSampler(sampler, batch_size, drop_last): 在一個(gè)batch中封裝一個(gè)其他的采樣器。
class torch.utils.data.distributed.DistributedSampler(dataset, num_replicas=None, rank=None):采樣器可以約束數(shù)據(jù)加載進(jìn)數(shù)據(jù)集的子集。
自定義數(shù)據(jù)集
自己定義的數(shù)據(jù)集需要繼承抽象類class torch.utils.data.Dataset,并且需要重載兩個(gè)重要的函數(shù):__len__ 和__getitem__。
整個(gè)代碼僅供參考。在__init__中是初始化了該類的一些基本參數(shù);__getitem__中是真正讀取數(shù)據(jù)的地方,迭代器通過索引來讀取數(shù)據(jù)集中數(shù)據(jù),因此只需要這一個(gè)方法中加入讀取數(shù)據(jù)的相關(guān)功能即可;__len__給出了整個(gè)數(shù)據(jù)集的尺寸大小,迭代器的索引范圍是根據(jù)這個(gè)函數(shù)得來的。
import torch class myDataset(torch.nn.data.Dataset): def __init__(self, dataSource) self.dataSource = dataSource def __getitem__(self, index): element = self.dataSource[index] return element def __len__(self): return len(self.dataSource) train_data = myDataset(dataSource)
自定義數(shù)據(jù)集加載器
class torch.utils.data.DataLoader(dataset, batch_size=1, shuffle=False, sampler=None, batch_sampler=None, num_workers=0, collate_fn=<function default_collate>, pin_memory=False, drop_last=False, timeout=0, worker_init_fn=None): 數(shù)據(jù)加載器。組合了一個(gè)數(shù)據(jù)集和采樣器,并提供關(guān)于數(shù)據(jù)的迭代器。
dataset (Dataset) – 需要加載的數(shù)據(jù)集(可以是自定義或者自帶的數(shù)據(jù)集)。
batch_size – batch的大?。蛇x項(xiàng),默認(rèn)值為1)。
shuffle – 是否在每個(gè)epoch中shuffle整個(gè)數(shù)據(jù)集, 默認(rèn)值為False。
sampler – 定義從數(shù)據(jù)中抽取樣本的策略. 如果指定了, shuffle參數(shù)必須為False。
num_workers – 表示讀取樣本的線程數(shù), 0表示只有主線程。
collate_fn – 合并一個(gè)樣本列表稱為一個(gè)batch。
pin_memory – 是否在返回?cái)?shù)據(jù)之前將張量拷貝到CUDA。
drop_last (bool, optional) – 設(shè)置是否丟棄最后一個(gè)不完整的batch,默認(rèn)為False。
timeout – 用來設(shè)置數(shù)據(jù)讀取的超時(shí)時(shí)間的,但超過這個(gè)時(shí)間還沒讀取到數(shù)據(jù)的話就會(huì)報(bào)錯(cuò)。應(yīng)該為非負(fù)整數(shù)。
train_loader=torch.utils.data.DataLoader(dataset=train_data, batch_size=64, shuffle=True)
以上這篇pytorch 自定義數(shù)據(jù)集加載方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
python 腳本生成隨機(jī) 字母 + 數(shù)字密碼功能
本文通過一小段簡單的代碼給大家分享基于python 腳本生成隨機(jī) 字母 + 數(shù)字密碼功能,感興趣的朋友跟隨腳本之家小編一起學(xué)習(xí)吧2018-05-05
完美解決python中ndarray 默認(rèn)用科學(xué)計(jì)數(shù)法顯示的問題
今天小編就為大家分享一篇完美解決python中ndarray 默認(rèn)用科學(xué)計(jì)數(shù)法顯示的問題,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-07-07
PyCharm利用pydevd-pycharm實(shí)現(xiàn)Python遠(yuǎn)程調(diào)試的詳細(xì)過程
這篇文章主要介紹了PyCharm利用pydevd-pycharm實(shí)現(xiàn)Python遠(yuǎn)程調(diào)試,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-09-09
Pyspark讀取parquet數(shù)據(jù)過程解析
這篇文章主要介紹了pyspark讀取parquet數(shù)據(jù)過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-03-03

