最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

pytorch中dataloader 的sampler 參數(shù)詳解

 更新時間:2022年09月01日 10:30:58   作者:mingqian_chu  
這篇文章主要介紹了pytorch中dataloader 的sampler 參數(shù)詳解,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價值,感興趣的小伙伴可以參考一下

1. dataloader() 初始化函數(shù)

 def __init__(self, dataset, batch_size=1, shuffle=False, sampler=None,
 batch_sampler=None, num_workers=0, collate_fn=None,
pin_memory=False, drop_last=False, timeout=0,
                 worker_init_fn=None, multiprocessing_context=None):

其中幾個常用的參數(shù):

  • dataset 數(shù)據(jù)集,map-style and iterable-style 可以用index取值的對象、
  • batch_size 大小
  • shuffle 取batch是否隨機(jī)取, 默認(rèn)為False
  • sampler 定義取batch的方法,是一個迭代器, 每次生成一個key 用于讀取dataset中的值
  • batch_sampler 也是一個迭代器, 每次生次一個batch_size的key
  • num_workers 參與工作的線程數(shù)collate_fn 對取出的batch進(jìn)行處理
  • drop_last 對最后不足batchsize的數(shù)據(jù)的處理方法

下面看兩段取自DataLoader中的__init__代碼, 幫助我們理解幾個常用參數(shù)之間的關(guān)系

2. shuffle 與sample 之間的關(guān)系

當(dāng)我們sampler有輸入時,shuffle的值就沒有意義,

	if sampler is None:  # give default samplers
	    if self._dataset_kind == _DatasetKind.Iterable:
	        # See NOTE [ Custom Samplers and IterableDataset ]
	        sampler = _InfiniteConstantSampler()
	    else:  # map-style
	        if shuffle:
	            sampler = RandomSampler(dataset)
	        else:
	            sampler = SequentialSampler(dataset)

當(dāng)dataset類型是map style時, shuffle其實就是改變sampler的取值

  • shuffle為默認(rèn)值 False時,sampler是SequentialSampler,就是按順序取樣,
  • shuffle為True時,sampler是RandomSampler, 就是按隨機(jī)取樣

3. sample 的定義方法

3.1 sampler 參數(shù)的使用

sampler 是用來定義取batch方法的一個函數(shù)或者類,返回的是一個迭代器。

我們可以看下自帶的RandomSampler類中最重要的iter函數(shù)

    def __iter__(self):
        n = len(self.data_source)
        # dataset的長度, 按順序索引
        if self.replacement:# 對應(yīng)的replace參數(shù)
            return iter(torch.randint(high=n, size=(self.num_samples,), dtype=torch.int64).tolist())
        return iter(torch.randperm(n).tolist())        

可以看出,其實就是生成索引,然后隨機(jī)的取值, 然后再迭代。

其實還有一些細(xì)節(jié)需要注意理解:

比如__len__函數(shù),包括DataLoader的len和sample的len, 兩者區(qū)別, 這部分代碼比較簡單,可以自行閱讀,其實參考著RandomSampler寫也不會出現(xiàn)問題。
比如,迭代器和生成器的使用, 以及區(qū)別

    if batch_size is not None and batch_sampler is None:
        # auto_collation without custom batch_sampler
        batch_sampler = BatchSampler(sampler, batch_size, drop_last)
        
    self.sampler = sampler
    self.batch_sampler = batch_sampler

BatchSampler的生成過程:

# 略去類的初始化
    def __iter__(self):
        batch = []
        for idx in self.sampler:
            batch.append(idx)
            if len(batch) == self.batch_size:
                yield batch
                batch = []
        if len(batch) > 0 and not self.drop_last:
            yield batch

就是按batch_size從sampler中讀取索引, 并形成生成器返回。

以上可以看出, batch_sampler和sampler, batch_size, drop_last之間的關(guān)系

  • 如果batch_sampler沒有定義的話且batch_size有定義, 會根據(jù)sampler, batch_size, drop_last生成一個batch_sampler
  • 自帶的注釋中對batch_sampler有一句話: Mutually exclusive with :attr:batch_size :attr:shuffle, :attr:sampler, and :attr:drop_last.
  • 意思就是b
  • atch_sampler 與這些參數(shù)沖突 ,即 如果你定義了batch_sampler, 其他參數(shù)都不需要有

4. batch 生成過程

每個batch都是由迭代器產(chǎn)生的:

# DataLoader中iter的部分
    def __iter__(self):
        if self.num_workers == 0:
            return _SingleProcessDataLoaderIter(self)
        else:
            return _MultiProcessingDataLoaderIter(self)

# 再看調(diào)用的另一個類
class _SingleProcessDataLoaderIter(_BaseDataLoaderIter):
    def __init__(self, loader):
        super(_SingleProcessDataLoaderIter, self).__init__(loader)
        assert self._timeout == 0
        assert self._num_workers == 0

        self._dataset_fetcher = _DatasetKind.create_fetcher(
            self._dataset_kind, self._dataset, self._auto_collation, self._collate_fn, self._drop_last)

    def __next__(self):
        index = self._next_index()  
        data = self._dataset_fetcher.fetch(index)  
        if self._pin_memory:
            data = _utils.pin_memory.pin_memory(data)
        return data

到此這篇關(guān)于pytorch中dataloader 的sampler 參數(shù)詳解的文章就介紹到這了,更多相關(guān)pytorch sampler 內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Python os.access()用法實例

    Python os.access()用法實例

    在本篇文章里小編給大家分享了關(guān)于Python os.access()用法實例內(nèi)容以及相關(guān)知識點(diǎn),需要的朋友們學(xué)習(xí)下。
    2019-02-02
  • python3通過udp實現(xiàn)組播數(shù)據(jù)的發(fā)送和接收操作

    python3通過udp實現(xiàn)組播數(shù)據(jù)的發(fā)送和接收操作

    這篇文章主要介紹了python3通過udp實現(xiàn)組播數(shù)據(jù)的發(fā)送和接收操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-05-05
  • python3安裝OCR識別庫tesserocr過程圖解

    python3安裝OCR識別庫tesserocr過程圖解

    這篇文章主要介紹了python3安裝OCR識別庫tesserocr過程圖解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-04-04
  • Python中變量的輸入輸出實例代碼詳解

    Python中變量的輸入輸出實例代碼詳解

    這篇文章主要介紹了Python中變量的輸入輸出問題,本文通過實例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價值 ,需要的朋友可以參考下
    2019-07-07
  • pandas如何統(tǒng)計某一列或某一行的缺失值數(shù)目

    pandas如何統(tǒng)計某一列或某一行的缺失值數(shù)目

    這篇文章主要介紹了pandas如何統(tǒng)計某一列或某一行的缺失值數(shù)目,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-05-05
  • 對django中render()與render_to_response()的區(qū)別詳解

    對django中render()與render_to_response()的區(qū)別詳解

    今天小編就為大家分享一篇對django中render()與render_to_response()的區(qū)別詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-10-10
  • 基于python使MUI登錄頁面的美化

    基于python使MUI登錄頁面的美化

    之前的文章Python用HBuilder創(chuàng)建交流社區(qū)APP我們已經(jīng)在HBuilder上創(chuàng)建的APP ,現(xiàn)HBuilder中已經(jīng)有了登錄頁面的相關(guān)的html文件,但是按照html已有的頁面來看,它缺少外觀的美化,本篇文章主要講的是MUI登錄頁面的美化。,需要的朋友可以參考一下
    2021-11-11
  • 在pycharm中debug 實時查看數(shù)據(jù)操作(交互式)

    在pycharm中debug 實時查看數(shù)據(jù)操作(交互式)

    這篇文章主要介紹了在pycharm中debug 實時查看數(shù)據(jù)操作(交互式),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-06-06
  • 運(yùn)行django項目指定IP和端口的方法

    運(yùn)行django項目指定IP和端口的方法

    今天小編就為大家分享一篇運(yùn)行django項目指定IP和端口的方法。具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-05-05
  • Windows安裝Anaconda并且配置國內(nèi)鏡像的詳細(xì)教程

    Windows安裝Anaconda并且配置國內(nèi)鏡像的詳細(xì)教程

    我們在學(xué)習(xí) Python 的時候需要不同的 Python 版本,關(guān)系到電腦環(huán)境變量配置換來換去很是麻煩,所以這個時候我們需要一個虛擬的 Python 環(huán)境變量,這篇文章主要介紹了Windows安裝Anaconda并且配置國內(nèi)鏡像教程,需要的朋友可以參考下
    2023-01-01

最新評論

来宾市| 合山市| 永定县| 白沙| 江源县| 靖江市| 宁国市| 简阳市| 德格县| 施秉县| 利川市| 鄯善县| 布拖县| 辽中县| 焦作市| 广德县| 泌阳县| 云霄县| 丰原市| 襄樊市| 绥江县| 澄迈县| 青河县| 澳门| 凭祥市| 汉源县| 五峰| 江华| 麻城市| 遂昌县| 榆林市| 剑河县| 昔阳县| 衡南县| 密山市| 东安县| 那坡县| 确山县| 隆德县| 香河县| 兴山县|