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

Pytorch技巧:DataLoader的collate_fn參數(shù)使用詳解

 更新時間:2020年01月08日 09:43:09   作者:jmjackyrj  
今天小編就為大家分享一篇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ù)集上提供單進程或多進程的迭代器

幾個關(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)文章

  • Python3 pickle模塊的使用方法詳細(xì)介紹

    Python3 pickle模塊的使用方法詳細(xì)介紹

    pickle提供了一個簡單的持久化功能。可以將對象以文件的形式存放在磁盤上,pickle序列化后的數(shù)據(jù),可讀性差,人一般無法識別,本文詳細(xì)介紹了pickle模塊的使用方法
    2021-10-10
  • Python使用PyNmap進行網(wǎng)絡(luò)掃描的詳細(xì)步驟

    Python使用PyNmap進行網(wǎng)絡(luò)掃描的詳細(xì)步驟

    使用 PyNmap 進行網(wǎng)絡(luò)掃描是一個非常有效的方式,PyNmap 是 Nmap 工具的一個 Python 封裝,它允許你在 Python 腳本中使用 Nmap 的強大功能,本文介紹了如何使用 PyNmap 進行網(wǎng)絡(luò)掃描的詳細(xì)步驟,需要的朋友可以參考下
    2024-08-08
  • python數(shù)據(jù)處理實戰(zhàn)(必看篇)

    python數(shù)據(jù)處理實戰(zhàn)(必看篇)

    下面小編就為大家?guī)硪黄猵ython數(shù)據(jù)處理實戰(zhàn)(必看篇)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-06-06
  • 基于Python的OpenCV骨架化圖像并顯示(skeletonize)

    基于Python的OpenCV骨架化圖像并顯示(skeletonize)

    這篇文章主要介紹了基于Python的OpenCV骨架化圖像并顯示(skeletonize),文中附含詳細(xì)的示例代碼,教大家來實現(xiàn)完成,有需要的可以參考下
    2021-08-08
  • python版飛機大戰(zhàn)代碼分享

    python版飛機大戰(zhàn)代碼分享

    這篇文章主要為大家詳細(xì)介紹了python版飛機大戰(zhàn)的實現(xiàn)代碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-11-11
  • python目標(biāo)檢測yolo2詳解及預(yù)測代碼復(fù)現(xiàn)

    python目標(biāo)檢測yolo2詳解及預(yù)測代碼復(fù)現(xiàn)

    這篇文章主要為大家介紹了python目標(biāo)檢測yolo2詳解及其預(yù)測代碼復(fù)現(xiàn),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-05-05
  • Centos7下源碼安裝Python3 及shell 腳本自動安裝Python3的教程

    Centos7下源碼安裝Python3 及shell 腳本自動安裝Python3的教程

    這篇文章主要介紹了Centos7下源碼安裝Python3 shell 腳本自動安裝Python3的相關(guān)知識,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-03-03
  • python實現(xiàn)mean-shift聚類算法

    python實現(xiàn)mean-shift聚類算法

    這篇文章主要為大家詳細(xì)介紹了python實現(xiàn)mean-shift聚類算法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-06-06
  • Python 正則表達式(?=...)和(?<=...)符號的使用

    Python 正則表達式(?=...)和(?<=...)符號的使用

    本文主要介紹Python 正則表達式(?=...)和(?<=...)符號的使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-05-05
  • python 正則表達式參數(shù)替換實例詳解

    python 正則表達式參數(shù)替換實例詳解

    這篇文章主要介紹了python 正則表達式參數(shù)替換,本文通過實例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-01-01

最新評論

博罗县| 千阳县| 盐津县| 涞水县| 孝感市| 赤壁市| 义马市| 明溪县| 唐河县| 东山县| 焦作市| 临澧县| 全州县| 乾安县| 石嘴山市| 锡林浩特市| 曲沃县| 霍林郭勒市| 德钦县| 巫山县| 普兰店市| 志丹县| 孝昌县| 沧源| 桃江县| 新营市| 石首市| 习水县| 石楼县| 沂水县| 大邑县| 富蕴县| 永清县| 海口市| 黑水县| 阿克陶县| 集安市| 老河口市| 迭部县| 建湖县| 普兰店市|