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

PyTorch中torch.utils.data.DataLoader實(shí)例詳解

 更新時(shí)間:2022年09月27日 09:46:31   作者:進(jìn)擊的程小白  
torch.utils.data.DataLoader主要是對數(shù)據(jù)進(jìn)行batch的劃分,下面這篇文章主要給大家介紹了關(guān)于PyTorch中torch.utils.data.DataLoader的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下

1、dataset:(數(shù)據(jù)類型 dataset)

輸入的數(shù)據(jù)類型,這里是原始數(shù)據(jù)的輸入。PyTorch內(nèi)也有這種數(shù)據(jù)結(jié)構(gòu)。

2、batch_size:(數(shù)據(jù)類型 int)

批訓(xùn)練數(shù)據(jù)量的大小,根據(jù)具體情況設(shè)置即可(默認(rèn):1)。PyTorch訓(xùn)練模型時(shí)調(diào)用數(shù)據(jù)不是一行一行進(jìn)行的(這樣太沒效率),而是一捆一捆來的。這里就是定義每次喂給神經(jīng)網(wǎng)絡(luò)多少行數(shù)據(jù),如果設(shè)置成1,那就是一行一行進(jìn)行(個(gè)人偏好,PyTorch默認(rèn)設(shè)置是1)。每次是隨機(jī)讀取大小為batch_size。如果dataset中的數(shù)據(jù)個(gè)數(shù)不是batch_size的整數(shù)倍,這最后一次把剩余的數(shù)據(jù)全部輸出。若想把剩下的不足batch size個(gè)的數(shù)據(jù)丟棄,則將drop_last設(shè)置為True,會將多出來不足一個(gè)batch的數(shù)據(jù)丟棄。

3、shuffle:(數(shù)據(jù)類型 bool)

洗牌。默認(rèn)設(shè)置為False。在每次迭代訓(xùn)練時(shí)是否將數(shù)據(jù)洗牌,默認(rèn)設(shè)置是False。將輸入數(shù)據(jù)的順序打亂,是為了使數(shù)據(jù)更有獨(dú)立性,但如果數(shù)據(jù)是有序列特征的,就不要設(shè)置成True了。

4、collate_fn:(數(shù)據(jù)類型 callable,沒見過的類型)

將一小段數(shù)據(jù)合并成數(shù)據(jù)列表,默認(rèn)設(shè)置是False。如果設(shè)置成True,系統(tǒng)會在返回前會將張量數(shù)據(jù)(Tensors)復(fù)制到CUDA內(nèi)存中。

5、batch_sampler:(數(shù)據(jù)類型 Sampler)

批量采樣,默認(rèn)設(shè)置為None。但每次返回的是一批數(shù)據(jù)的索引(注意:不是數(shù)據(jù))。其和batch_size、shuffle 、sampler and drop_last參數(shù)是不兼容的。我想,應(yīng)該是每次輸入網(wǎng)絡(luò)的數(shù)據(jù)是隨機(jī)采樣模式,這樣能使數(shù)據(jù)更具有獨(dú)立性質(zhì)。所以,它和一捆一捆按順序輸入,數(shù)據(jù)洗牌,數(shù)據(jù)采樣,等模式是不兼容的。

6、sampler:(數(shù)據(jù)類型 Sampler)

采樣,默認(rèn)設(shè)置為None。根據(jù)定義的策略從數(shù)據(jù)集中采樣輸入。如果定義采樣規(guī)則,則洗牌(shuffle)設(shè)置必須為False。

7、num_workers:(數(shù)據(jù)類型 Int)

工作者數(shù)量,默認(rèn)是0。使用多少個(gè)子進(jìn)程來導(dǎo)入數(shù)據(jù)。設(shè)置為0,就是使用主進(jìn)程來導(dǎo)入數(shù)據(jù)。注意:這個(gè)數(shù)字必須是大于等于0的,負(fù)數(shù)估計(jì)會出錯(cuò)。

8、pin_memory:(數(shù)據(jù)類型 bool)

內(nèi)存寄存,默認(rèn)為False。在數(shù)據(jù)返回前,是否將數(shù)據(jù)復(fù)制到CUDA內(nèi)存中。

9、drop_last:(數(shù)據(jù)類型 bool)

丟棄最后數(shù)據(jù),默認(rèn)為False。設(shè)置了 batch_size 的數(shù)目后,最后一批數(shù)據(jù)未必是設(shè)置的數(shù)目,有可能會小些。這時(shí)你是否需要丟棄這批數(shù)據(jù)。

10、timeout:(數(shù)據(jù)類型 numeric)

超時(shí),默認(rèn)為0。是用來設(shè)置數(shù)據(jù)讀取的超時(shí)時(shí)間的,但超過這個(gè)時(shí)間還沒讀取到數(shù)據(jù)的話就會報(bào)錯(cuò)。 所以,數(shù)值必須大于等于0。

11、worker_init_fn(數(shù)據(jù)類型 callable,沒見過的類型)

子進(jìn)程導(dǎo)入模式,默認(rèn)為Noun。在數(shù)據(jù)導(dǎo)入前和步長結(jié)束后,根據(jù)工作子進(jìn)程的ID逐個(gè)按順序?qū)霐?shù)據(jù)。

對batch_size舉例分析:

"""
    批訓(xùn)練,把數(shù)據(jù)變成一小批一小批數(shù)據(jù)進(jìn)行訓(xùn)練。
    DataLoader就是用來包裝所使用的數(shù)據(jù),每次拋出一批數(shù)據(jù)
"""
import torch
import torch.utils.data as Data
 
BATCH_SIZE = 5
 
x = torch.linspace(1, 11, 11)
y = torch.linspace(11, 1, 11)
print(x)
print(y)
# 把數(shù)據(jù)放在數(shù)據(jù)庫中
torch_dataset = Data.TensorDataset(x, y)
loader = Data.DataLoader(
    # 從數(shù)據(jù)庫中每次抽出batch size個(gè)樣本
    dataset=torch_dataset,
    batch_size=BATCH_SIZE,
    shuffle=True,
    # num_workers=2,
)
 
def show_batch():
    for epoch in range(3):
        for step, (batch_x, batch_y) in enumerate(loader):
            # training
            print("steop:{}, batch_x:{}, batch_y:{}".format(step, batch_x, batch_y))
 
if __name__ == '__main__':
    show_batch()

輸出為:

tensor([ 1.,  2.,  3.,  4.,  5.,  6.,  7.,  8.,  9., 10., 11.])
tensor([11., 10.,  9.,  8.,  7.,  6.,  5.,  4.,  3.,  2.,  1.])
steop:0, batch_x:tensor([ 3.,  2.,  8., 11.,  1.]), batch_y:tensor([ 9., 10.,  4.,  1., 11.])
steop:1, batch_x:tensor([ 5.,  6.,  7.,  4., 10.]), batch_y:tensor([7., 6., 5., 8., 2.])
steop:2, batch_x:tensor([9.]), batch_y:tensor([3.])
steop:0, batch_x:tensor([ 9.,  7., 10.,  2.,  4.]), batch_y:tensor([ 3.,  5.,  2., 10.,  8.])
steop:1, batch_x:tensor([ 5., 11.,  3.,  6.,  8.]), batch_y:tensor([7., 1., 9., 6., 4.])
steop:2, batch_x:tensor([1.]), batch_y:tensor([11.])
steop:0, batch_x:tensor([10.,  5.,  7.,  4.,  2.]), batch_y:tensor([ 2.,  7.,  5.,  8., 10.])
steop:1, batch_x:tensor([3., 9., 1., 8., 6.]), batch_y:tensor([ 9.,  3., 11.,  4.,  6.])
steop:2, batch_x:tensor([11.]), batch_y:tensor([1.])
 
Process finished with exit code 0

若drop_last=True

"""
    批訓(xùn)練,把數(shù)據(jù)變成一小批一小批數(shù)據(jù)進(jìn)行訓(xùn)練。
    DataLoader就是用來包裝所使用的數(shù)據(jù),每次拋出一批數(shù)據(jù)
"""
import torch
import torch.utils.data as Data
 
BATCH_SIZE = 5
 
x = torch.linspace(1, 11, 11)
y = torch.linspace(11, 1, 11)
print(x)
print(y)
# 把數(shù)據(jù)放在數(shù)據(jù)庫中
torch_dataset = Data.TensorDataset(x, y)
loader = Data.DataLoader(
    # 從數(shù)據(jù)庫中每次抽出batch size個(gè)樣本
    dataset=torch_dataset,
    batch_size=BATCH_SIZE,
    shuffle=True,
    # num_workers=2,
    drop_last=True,
)
 
def show_batch():
    for epoch in range(3):
        for step, (batch_x, batch_y) in enumerate(loader):
            # training
            print("steop:{}, batch_x:{}, batch_y:{}".format(step, batch_x, batch_y))
 
if __name__ == '__main__':
    show_batch()

對應(yīng)的輸出為:

tensor([ 1.,  2.,  3.,  4.,  5.,  6.,  7.,  8.,  9., 10., 11.])
tensor([11., 10.,  9.,  8.,  7.,  6.,  5.,  4.,  3.,  2.,  1.])
steop:0, batch_x:tensor([ 9.,  2.,  7.,  4., 11.]), batch_y:tensor([ 3., 10.,  5.,  8.,  1.])
steop:1, batch_x:tensor([ 3.,  5., 10.,  1.,  8.]), batch_y:tensor([ 9.,  7.,  2., 11.,  4.])
steop:0, batch_x:tensor([ 5., 11.,  6.,  1.,  2.]), batch_y:tensor([ 7.,  1.,  6., 11., 10.])
steop:1, batch_x:tensor([ 3.,  4., 10.,  8.,  9.]), batch_y:tensor([9., 8., 2., 4., 3.])
steop:0, batch_x:tensor([10.,  4.,  9.,  8.,  7.]), batch_y:tensor([2., 8., 3., 4., 5.])
steop:1, batch_x:tensor([ 6.,  1., 11.,  2.,  5.]), batch_y:tensor([ 6., 11.,  1., 10.,  7.])
 
Process finished with exit code 0

總結(jié)

到此這篇關(guān)于PyTorch中torch.utils.data.DataLoader的文章就介紹到這了,更多相關(guān)PyTorch torch.utils.data.DataLoader內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 詳解pandas df.iloc[]的典型用法

    詳解pandas df.iloc[]的典型用法

    本文主要介紹了詳解pandas df.iloc[]的典型用法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-08-08
  • 淺析form標(biāo)簽中的GET和POST提交方式區(qū)別

    淺析form標(biāo)簽中的GET和POST提交方式區(qū)別

    在HTML中,form表單的作用是收集標(biāo)簽中的內(nèi)容<form>...</form> 中間可以由訪問者添加類似于文本,選擇,或者一些控制模塊等等.然后這些內(nèi)容將會被送到服務(wù)端
    2021-09-09
  • 在CentOS6上安裝Python2.7的解決方法

    在CentOS6上安裝Python2.7的解決方法

    在CentOS6上yum安裝工具是基于Python2.6.6的,所以在CentOS6上默認(rèn)安裝的是Python2.6.6,因?yàn)橐诜?wù)器系統(tǒng)為CentOS6上部署生產(chǎn)環(huán)境,但是代碼都是基于Python2.7寫的,所有遇到了問題,下面通過本文給大家介紹下在CentOS6上安裝Python2.7的解決方法,一起看看吧
    2018-01-01
  • Python六大開源框架對比

    Python六大開源框架對比

    在這篇文章里,我們將為Python Web開發(fā)者回顧基于Python的6大Web應(yīng)用框架。無論你是出于愛好還是需求,這六大框架都可能會成為你工作上不錯(cuò)的得力助手。
    2015-10-10
  • Python處理浮點(diǎn)數(shù)的實(shí)用技巧分享

    Python處理浮點(diǎn)數(shù)的實(shí)用技巧分享

    四舍五入是一種常見的數(shù)學(xué)操作,它用于將數(shù)字舍入到指定的精度,Python?提供了多種方法來實(shí)現(xiàn)四舍五入操作,本文將詳細(xì)介紹這些方法,希望對大家有所幫助
    2024-12-12
  • Python中函數(shù)帶括號和不帶括號的區(qū)別及說明

    Python中函數(shù)帶括號和不帶括號的區(qū)別及說明

    這篇文章主要介紹了Python中函數(shù)帶括號和不帶括號的區(qū)別及說明,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-11-11
  • 詳解在SpringBoot如何優(yōu)雅的使用多線程

    詳解在SpringBoot如何優(yōu)雅的使用多線程

    這篇文章主要帶大家快速了解一下@Async注解的用法,包括異步方法無返回值、有返回值,最后總結(jié)了@Async注解失效的幾個(gè)坑,感興趣的小伙伴可以了解一下
    2023-02-02
  • python將logging模塊封裝成單獨(dú)模塊并實(shí)現(xiàn)動態(tài)切換Level方式

    python將logging模塊封裝成單獨(dú)模塊并實(shí)現(xiàn)動態(tài)切換Level方式

    這篇文章主要介紹了python將logging模塊封裝成單獨(dú)模塊并實(shí)現(xiàn)動態(tài)切換Level方式,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-05-05
  • pygame實(shí)現(xiàn)雷電游戲雛形開發(fā)

    pygame實(shí)現(xiàn)雷電游戲雛形開發(fā)

    這篇文章主要為大家詳細(xì)介紹了pygame實(shí)現(xiàn)雷電游戲開發(fā)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-11-11
  • 教你使用Python連接oracle

    教你使用Python連接oracle

    今天教各位小伙伴怎么用Python連接oracle,文中附帶非常詳細(xì)的圖文示例,對正在學(xué)習(xí)的小伙伴們很有幫助喲,需要的朋友可以參考下
    2021-05-05

最新評論

鄂尔多斯市| 灵丘县| 玉田县| 宁海县| 米林县| 微山县| 离岛区| 武清区| 武穴市| 会东县| 宜州市| 达孜县| 明溪县| 霍城县| 岐山县| 娱乐| 旅游| 伊川县| 江永县| 本溪市| 金寨县| 新田县| 巴林右旗| 高唐县| 区。| 西畴县| 松江区| 郴州市| 永兴县| 新干县| 博爱县| 昭苏县| 崇文区| 兴宁市| 霍邱县| 高雄市| 射阳县| 光山县| 松潘县| 临汾市| 阳朔县|