欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

PyTorch中torch.utils.data.DataLoader簡單介紹與使用方法

 更新時間:2022年06月22日 11:10:09   作者:想變厲害的大白菜  
DataLoader是PyTorch中讀取數(shù)據(jù)的一個重要接口,基本上用PyTorch訓(xùn)練模型都會用到,下面這篇文章主要給大家介紹了關(guān)于PyTorch中torch.utils.data.DataLoader簡單介紹與使用方法的相關(guān)資料,需要的朋友可以參考下

一、torch.utils.data.DataLoader 簡介

作用:torch.utils.data.DataLoader 主要是對數(shù)據(jù)進(jìn)行 batch 的劃分。

數(shù)據(jù)加載器,結(jié)合了數(shù)據(jù)集和取樣器,并且可以提供多個線程處理數(shù)據(jù)集。

在訓(xùn)練模型時使用到此函數(shù),用來 把訓(xùn)練數(shù)據(jù)分成多個小組 ,此函數(shù) 每次拋出一組數(shù)據(jù) 。直至把所有的數(shù)據(jù)都拋出。就是做一個數(shù)據(jù)的初始化。

好處:

使用DataLoader的好處是,可以快速的迭代數(shù)據(jù)。

用于生成迭代數(shù)據(jù)非常方便。

注意:

除此之外,特別要注意的是輸入進(jìn)函數(shù)的數(shù)據(jù)一定得是可迭代的。如果是自定的數(shù)據(jù)集的話可以在定義類中用def__len__、def__getitem__定義。

二、實例

BATCH_SIZE 剛好整除數(shù)據(jù)量

"""
    批訓(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ùn)練的數(shù)據(jù)個數(shù)

x = torch.linspace(1, 10, 10)   # 訓(xùn)練數(shù)據(jù)
print(x)
y = torch.linspace(10, 1, 10)   # 標(biāo)簽
print(y)
# 把數(shù)據(jù)放在數(shù)據(jù)庫中
torch_dataset = Data.TensorDataset(x, y)  # 對給定的 tensor 數(shù)據(jù),將他們包裝成 dataset

loader = Data.DataLoader(
    # 從數(shù)據(jù)庫中每次抽出batch size個樣本
    dataset=torch_dataset,       # torch TensorDataset format
    batch_size=BATCH_SIZE,       # mini batch size
    shuffle=True,                # 要不要打亂數(shù)據(jù) (打亂比較好)
    num_workers=2,               # 多線程來讀數(shù)據(jù)
)

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))

show_batch()

輸出結(jié)果:

tensor([ 1.,  2.,  3.,  4.,  5.,  6.,  7.,  8.,  9., 10.])
tensor([10.,  9.,  8.,  7.,  6.,  5.,  4.,  3.,  2.,  1.])
steop:0, batch_x:tensor([10.,  1.,  3.,  7.,  6.]), batch_y:tensor([ 1., 10.,  8.,  4.,  5.])
steop:1, batch_x:tensor([8., 5., 4., 9., 2.]), batch_y:tensor([3., 6., 7., 2., 9.])
steop:0, batch_x:tensor([ 9.,  3., 10.,  1.,  5.]), batch_y:tensor([ 2.,  8.,  1., 10.,  6.])
steop:1, batch_x:tensor([2., 6., 8., 4., 7.]), batch_y:tensor([9., 5., 3., 7., 4.])
steop:0, batch_x:tensor([ 2., 10.,  9.,  6.,  1.]), batch_y:tensor([ 9.,  1.,  2.,  5., 10.])
steop:1, batch_x:tensor([8., 3., 4., 7., 5.]), batch_y:tensor([3., 8., 7., 4., 6.])

說明:共有 10 條數(shù)據(jù),設(shè)置 BATCH_SIZE 為 5 來進(jìn)行劃分,能劃分為 2 組(steop 為 0 和 1)。這兩組數(shù)據(jù)互斥。

BATCH_SIZE 不整除數(shù)據(jù)量:會輸出余下所有數(shù)據(jù)

將上述代碼中的 BATCH_SIZE 改為 4 :

"""
    批訓(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 = 4       # 批訓(xùn)練的數(shù)據(jù)個數(shù)

x = torch.linspace(1, 10, 10)   # 訓(xùn)練數(shù)據(jù)
print(x)
y = torch.linspace(10, 1, 10)   # 標(biāo)簽
print(y)
# 把數(shù)據(jù)放在數(shù)據(jù)庫中
torch_dataset = Data.TensorDataset(x, y)  # 對給定的 tensor 數(shù)據(jù),將他們包裝成 dataset

loader = Data.DataLoader(
    # 從數(shù)據(jù)庫中每次抽出batch size個樣本
    dataset=torch_dataset,       # torch TensorDataset format
    batch_size=BATCH_SIZE,       # mini batch size
    shuffle=True,                # 要不要打亂數(shù)據(jù) (打亂比較好)
    num_workers=2,               # 多線程來讀數(shù)據(jù)
)

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))

show_batch()

輸出結(jié)果:

tensor([ 1.,  2.,  3.,  4.,  5.,  6.,  7.,  8.,  9., 10.])
tensor([10.,  9.,  8.,  7.,  6.,  5.,  4.,  3.,  2.,  1.])
steop:0, batch_x:tensor([1., 5., 3., 2.]), batch_y:tensor([10.,  6.,  8.,  9.])
steop:1, batch_x:tensor([7., 8., 4., 6.]), batch_y:tensor([4., 3., 7., 5.])
steop:2, batch_x:tensor([10.,  9.]), batch_y:tensor([1., 2.])
steop:0, batch_x:tensor([ 7., 10.,  5.,  2.]), batch_y:tensor([4., 1., 6., 9.])
steop:1, batch_x:tensor([9., 1., 6., 4.]), batch_y:tensor([ 2., 10.,  5.,  7.])
steop:2, batch_x:tensor([8., 3.]), batch_y:tensor([3., 8.])
steop:0, batch_x:tensor([10.,  3.,  2.,  8.]), batch_y:tensor([1., 8., 9., 3.])
steop:1, batch_x:tensor([1., 7., 5., 9.]), batch_y:tensor([10.,  4.,  6.,  2.])
steop:2, batch_x:tensor([4., 6.]), batch_y:tensor([7., 5.])

說明:共有 10 條數(shù)據(jù),設(shè)置 BATCH_SIZE 為 4 來進(jìn)行劃分,能劃分為 3 組(steop 為 0 、1、2)。分別有 4、4、2 條數(shù)據(jù)。

參考鏈接

  1. torch.utils.data.DataLoader使用方法
  2. 【Pytorch基礎(chǔ)】torch.utils.data.DataLoader方法的使用

總結(jié)

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

相關(guān)文章

  • python中pywifi的具體使用

    python中pywifi的具體使用

    本文主要介紹了python中pywifi的具體使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-03-03
  • Python3安裝pip工具的詳細(xì)步驟

    Python3安裝pip工具的詳細(xì)步驟

    這篇文章主要介紹了Python3安裝pip工具的詳細(xì)步驟,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-10-10
  • python3中join和格式化的用法小結(jié)

    python3中join和格式化的用法小結(jié)

    這篇文章主要介紹了python中os庫的使用,本篇文章記錄下python中os庫的一些函數(shù)使用,對python?os庫使用感興趣的朋友跟隨小編一起看看吧
    2022-10-10
  • python數(shù)據(jù)預(yù)處理之將類別數(shù)據(jù)轉(zhuǎn)換為數(shù)值的方法

    python數(shù)據(jù)預(yù)處理之將類別數(shù)據(jù)轉(zhuǎn)換為數(shù)值的方法

    下面小編就為大家?guī)硪黄猵ython數(shù)據(jù)預(yù)處理之將類別數(shù)據(jù)轉(zhuǎn)換為數(shù)值的方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-07-07
  • 利用Python3實現(xiàn)統(tǒng)計大量單詞中各字母出現(xiàn)的次數(shù)和頻率的方法

    利用Python3實現(xiàn)統(tǒng)計大量單詞中各字母出現(xiàn)的次數(shù)和頻率的方法

    這篇文章主要介紹了利用Python3實現(xiàn)統(tǒng)計大量單詞中各字母出現(xiàn)的次數(shù)和頻率,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-03-03
  • Centos 升級到python3后pip 無法使用的解決方法

    Centos 升級到python3后pip 無法使用的解決方法

    今天小編就為大家分享一篇Centos 升級到python3后pip 無法使用的解決方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-06-06
  • 信號生成及DFT的python實現(xiàn)方式

    信號生成及DFT的python實現(xiàn)方式

    今天小編就為大家分享一篇信號生成及DFT的python實現(xiàn)方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-02-02
  • Python素數(shù)檢測實例分析

    Python素數(shù)檢測實例分析

    這篇文章主要介紹了Python素數(shù)檢測方法,實例分析了Python判定素數(shù)的相關(guān)技巧,需要的朋友可以參考下
    2015-06-06
  • python實現(xiàn)三階魔方還原的示例代碼

    python實現(xiàn)三階魔方還原的示例代碼

    這篇文章主要介紹了python實現(xiàn)三階魔方還原的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-04-04
  • PyQt5實現(xiàn)下載進(jìn)度條效果

    PyQt5實現(xiàn)下載進(jìn)度條效果

    這篇文章主要為大家詳細(xì)介紹了PyQt5實現(xiàn)下載進(jìn)度條效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-04-04

最新評論