Pytorch技巧:DataLoader的collate_fn參數(shù)使用詳解
DataLoader完整的參數(shù)表如下:
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)
DataLoader在數(shù)據(jù)集上提供單進(jìn)程或多進(jìn)程的迭代器
幾個關(guān)鍵的參數(shù)意思:
- shuffle:設(shè)置為True的時候,每個世代都會打亂數(shù)據(jù)集
- collate_fn:如何取樣本的,我們可以定義自己的函數(shù)來準(zhǔn)確地實現(xiàn)想要的功能
- drop_last:告訴如何處理數(shù)據(jù)集長度除于batch_size余下的數(shù)據(jù)。True就拋棄,否則保留
一個測試的例子
import torch import torch.utils.data as Data import numpy as np test = np.array([0,1,2,3,4,5,6,7,8,9,10,11]) inputing = torch.tensor(np.array([test[i:i + 3] for i in range(10)])) target = torch.tensor(np.array([test[i:i + 1] for i in range(10)])) torch_dataset = Data.TensorDataset(inputing,target) batch = 3 loader = Data.DataLoader( dataset=torch_dataset, batch_size=batch, # 批大小 # 若dataset中的樣本數(shù)不能被batch_size整除的話,最后剩余多少就使用多少 collate_fn=lambda x:( torch.cat( [x[i][j].unsqueeze(0) for i in range(len(x))], 0 ).unsqueeze(0) for j in range(len(x[0])) ) ) for (i,j) in loader: print(i) print(j)
輸出結(jié)果:
tensor([[[ 0, 1, 2], [ 1, 2, 3], [ 2, 3, 4]]], dtype=torch.int32) tensor([[[ 0], [ 1], [ 2]]], dtype=torch.int32) tensor([[[ 3, 4, 5], [ 4, 5, 6], [ 5, 6, 7]]], dtype=torch.int32) tensor([[[ 3], [ 4], [ 5]]], dtype=torch.int32) tensor([[[ 6, 7, 8], [ 7, 8, 9], [ 8, 9, 10]]], dtype=torch.int32) tensor([[[ 6], [ 7], [ 8]]], dtype=torch.int32) tensor([[[ 9, 10, 11]]], dtype=torch.int32) tensor([[[ 9]]], dtype=torch.int32)
如果不要collate_fn的值,輸出變成
tensor([[ 0, 1, 2], [ 1, 2, 3], [ 2, 3, 4]], dtype=torch.int32) tensor([[ 0], [ 1], [ 2]], dtype=torch.int32) tensor([[ 3, 4, 5], [ 4, 5, 6], [ 5, 6, 7]], dtype=torch.int32) tensor([[ 3], [ 4], [ 5]], dtype=torch.int32) tensor([[ 6, 7, 8], [ 7, 8, 9], [ 8, 9, 10]], dtype=torch.int32) tensor([[ 6], [ 7], [ 8]], dtype=torch.int32) tensor([[ 9, 10, 11]], dtype=torch.int32) tensor([[ 9]], dtype=torch.int32)
所以collate_fn就是使結(jié)果多一維。
看看collate_fn的值是什么意思。我們把它改為如下
collate_fn=lambda x:x
并輸出
for i in loader: print(i)
得到結(jié)果
[(tensor([ 0, 1, 2], dtype=torch.int32), tensor([ 0], dtype=torch.int32)), (tensor([ 1, 2, 3], dtype=torch.int32), tensor([ 1], dtype=torch.int32)), (tensor([ 2, 3, 4], dtype=torch.int32), tensor([ 2], dtype=torch.int32))] [(tensor([ 3, 4, 5], dtype=torch.int32), tensor([ 3], dtype=torch.int32)), (tensor([ 4, 5, 6], dtype=torch.int32), tensor([ 4], dtype=torch.int32)), (tensor([ 5, 6, 7], dtype=torch.int32), tensor([ 5], dtype=torch.int32))] [(tensor([ 6, 7, 8], dtype=torch.int32), tensor([ 6], dtype=torch.int32)), (tensor([ 7, 8, 9], dtype=torch.int32), tensor([ 7], dtype=torch.int32)), (tensor([ 8, 9, 10], dtype=torch.int32), tensor([ 8], dtype=torch.int32))] [(tensor([ 9, 10, 11], dtype=torch.int32), tensor([ 9], dtype=torch.int32))]
每個i都是一個列表,每個列表包含batch_size個元組,每個元組包含TensorDataset的單獨數(shù)據(jù)。所以要將重新組合成每個batch包含1*3*3的input和1*3*1的target,就要重新解包并打包。 看看我們的collate_fn:
collate_fn=lambda x:( torch.cat( [x[i][j].unsqueeze(0) for i in range(len(x))], 0 ).unsqueeze(0) for j in range(len(x[0])) )
j取的是兩個變量:input和target。i取的是batch_size。然后通過unsqueeze(0)方法在前面加一維。torch.cat(,0)將其打包起來。然后再通過unsqueeze(0)方法在前面加一維。 完成。
以上這篇Pytorch技巧:DataLoader的collate_fn參數(shù)使用詳解就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python使用PyNmap進(jìn)行網(wǎng)絡(luò)掃描的詳細(xì)步驟
使用 PyNmap 進(jìn)行網(wǎng)絡(luò)掃描是一個非常有效的方式,PyNmap 是 Nmap 工具的一個 Python 封裝,它允許你在 Python 腳本中使用 Nmap 的強(qiáng)大功能,本文介紹了如何使用 PyNmap 進(jìn)行網(wǎng)絡(luò)掃描的詳細(xì)步驟,需要的朋友可以參考下2024-08-08
python數(shù)據(jù)處理實戰(zhàn)(必看篇)
下面小編就為大家?guī)硪黄猵ython數(shù)據(jù)處理實戰(zhàn)(必看篇)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-06-06
基于Python的OpenCV骨架化圖像并顯示(skeletonize)
這篇文章主要介紹了基于Python的OpenCV骨架化圖像并顯示(skeletonize),文中附含詳細(xì)的示例代碼,教大家來實現(xiàn)完成,有需要的可以參考下2021-08-08
python目標(biāo)檢測yolo2詳解及預(yù)測代碼復(fù)現(xiàn)
這篇文章主要為大家介紹了python目標(biāo)檢測yolo2詳解及其預(yù)測代碼復(fù)現(xiàn),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05
Centos7下源碼安裝Python3 及shell 腳本自動安裝Python3的教程
這篇文章主要介紹了Centos7下源碼安裝Python3 shell 腳本自動安裝Python3的相關(guān)知識,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下2020-03-03
Python 正則表達(dá)式(?=...)和(?<=...)符號的使用
本文主要介紹Python 正則表達(dá)式(?=...)和(?<=...)符號的使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-05-05

