溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

python遍歷迭代器自動(dòng)鏈?zhǔn)教幚頂?shù)據(jù)的代碼怎么寫

發(fā)布時(shí)間:2022-01-10 16:30:49 來源:億速云 閱讀:165 作者:柒染 欄目:開發(fā)技術(shù)

python遍歷迭代器自動(dòng)鏈?zhǔn)教幚頂?shù)據(jù)的代碼怎么寫,相信很多沒有經(jīng)驗(yàn)的人對(duì)此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個(gè)問題。

python遍歷迭代器自動(dòng)鏈?zhǔn)教幚頂?shù)據(jù)

pytorch.utils.data可兼容迭代數(shù)據(jù)訓(xùn)練處理,在dataloader中使用提高訓(xùn)練效率:借助迭代器避免內(nèi)存溢出不足的現(xiàn)象、借助鏈?zhǔn)教幚硎沟脭?shù)據(jù)讀取利用更高效(可類比操作系統(tǒng)的資源調(diào)控)

書接上文,使用迭代器鏈?zhǔn)教幚頂?shù)據(jù),在Process類的__iter__方法中執(zhí)行掛載的預(yù)處理方法,可以嵌套包裹多層處理方法,類似KoaJs洋蔥模型,在for循環(huán)時(shí),自動(dòng)執(zhí)行預(yù)處理方法返回處理后的數(shù)據(jù)

分析下述示例中輸入數(shù)據(jù)依次執(zhí)行順序:travel -> deep -> shuffle -> sort -> batch,實(shí)際由于嵌套循環(huán)或設(shè)置緩存的存在,數(shù)據(jù)流式會(huì)有變化,具體如后圖分析

from torch.utils.data import IterableDataset
# ...

import random

class Process(IterableDataset):
    def __init__(self, data, f):
        self.data = data
        # 綁定處理函數(shù)
        self.f = f   
    def __iter__(self):
        # for循環(huán)遍歷時(shí),返回一個(gè)當(dāng)前環(huán)節(jié)處理的迭代器對(duì)象
        return self.f(iter(self.data)) 

a = ['a0', 'a1', 'a2', 'a3', 'a4', 'a5', 'a6', 'a7', 'a8', 'a9']
b = ['b0', 'b1', 'b2', 'b3', 'b4', 'b5', 'b6', 'b7', 'b8', 'b9']
c = ['c0', 'c1', 'c2', 'c3', 'c4', 'c5', 'c6', 'c7', 'c8', 'c9']
# data = [[j + str(i) for i in range(10)] for j in ['a','b', 'c'] ]
data = [a, b, c]
def travel(d):
    for i in d:
        # print('travel ', i)
        yield i
def deep(d):
    for arr in d:
        for item in arr:
            yield item

def shuffle(d, sf_size=5):
    buf = []
    for i in d:
        buf.append(i)
        if len(buf) >= sf_size:
            random.shuffle(buf)
            for j in buf:
                # print('shuffle', j)
                yield j
            buf = []
    for k in buf:
        yield k

def sort(d):
    buf = []
    for i in d:
        buf.append(i)
        if len(buf) >= 3:
            for i in buf:
                # print('sort', i)
                yield i
            buf = []
    for k in buf:
        yield k

def batch(d):
    buf = []
    for i in d:
        buf.append(i)
        if len(buf) >= 16:
            for i in buf:
                # print('batch', i)
                yield i
            buf = []
# 對(duì)訓(xùn)練數(shù)據(jù)進(jìn)行的多個(gè)預(yù)處理步驟
dataset = Process(data, travel)
dataset = Process(dataset , deep)
dataset = Process(dataset , shuffle)
dataset = Process(dataset , sort)
train_dataset = Process(p, batch)

# 可在此處斷點(diǎn)測(cè)試
for i in p:
    print(i, 'train')

# train_data_loader = DataLoader(train_dataset,num_workers=args.num_workers,prefetch_factor=args.prefetch)
# train(model , train_data_loader)

由上可以構(gòu)造數(shù)據(jù)流式方向 :batch(iter(sort(iter(shuffle(iter(deep(iter(travel(iter( d ))))))))))

根據(jù)數(shù)據(jù)流式抽取部分過程畫出時(shí)序圖如下:

python遍歷迭代器自動(dòng)鏈?zhǔn)教幚頂?shù)據(jù)的代碼怎么寫

附:python 手動(dòng)遍歷迭代器

想遍歷一個(gè)可迭代對(duì)象中的所有元素,但是卻不想使用for 循環(huán)

為了手動(dòng)的遍歷可迭代對(duì)象,使用next() 函數(shù)并在代碼中捕獲StopIteration 異常。比如,下面的例子手動(dòng)讀取一個(gè)文件中的所有行

def manual_iter():
    with open('/etc/passwd') as f:
        try:
            while True:
                line = next(f)
                print(line, end='')
        except StopIteration:
            pass

通常來講, StopIteration 用來指示迭代的結(jié)尾。然而,如果你手動(dòng)使用上面演示的next() 函數(shù)的話,你還可以通過返回一個(gè)指定值來標(biāo)記結(jié)尾,比如None 。下面是示例:

with open('/etc/passwd') as f:
    while True:
        line = next(f)
        if line is None:
            break
    print(line, end='')

大多數(shù)情況下,我們會(huì)使用for 循環(huán)語句用來遍歷一個(gè)可迭代對(duì)象。但是,偶爾也需要對(duì)迭代做更加精確的控制,這時(shí)候了解底層迭代機(jī)制就顯得尤為重要了。下面的交互示例向我們演示了迭代期間所發(fā)生的基本細(xì)節(jié):

>>> items = [1, 2, 3]
>>> # Get the iterator
>>> it = iter(items) # Invokes items.__iter__()
>>> # Run the iterator
>>> next(it) # Invokes it.__next__()
1
>>> next(it)
2
>>> next(it)
3
>>> next(it)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
StopIteration
>>>

看完上述內(nèi)容,你們掌握python遍歷迭代器自動(dòng)鏈?zhǔn)教幚頂?shù)據(jù)的代碼怎么寫的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI