python遍歷迭代器自動(dòng)鏈?zhǔn)教幚頂?shù)據(jù)的實(shí)例代碼
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ù)讀取利用更高效(可類(lèi)比操作系統(tǒng)的資源調(diào)控)
書(shū)接上文,使用迭代器鏈?zhǔn)教幚頂?shù)據(jù),在Process類(lèi)的__iter__方法中執(zhí)行掛載的預(yù)處理方法,可以嵌套包裹多層處理方法,類(lèi)似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ù)流式抽取部分過(guò)程畫(huà)出時(shí)序圖如下:
附: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
通常來(lái)講, StopIteration 用來(lái)指示迭代的結(jié)尾。然而,如果你手動(dòng)使用上面演示的next() 函數(shù)的話,你還可以通過(guò)返回一個(gè)指定值來(lái)標(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)語(yǔ)句用來(lái)遍歷一個(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 >>>
總結(jié)
到此這篇關(guān)于python遍歷迭代器自動(dòng)鏈?zhǔn)教幚頂?shù)據(jù)的文章就介紹到這了,更多相關(guān)python自動(dòng)鏈?zhǔn)教幚頂?shù)據(jù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章

python 爬取馬蜂窩景點(diǎn)翻頁(yè)文字評(píng)論的實(shí)現(xiàn)

解決Python出現(xiàn)_warn_unsafe_extraction問(wèn)題的方法

Python如何快速生成本項(xiàng)目的requeirments.txt實(shí)現(xiàn)

Python 12306搶火車(chē)票腳本 Python京東搶手機(jī)腳本

Python實(shí)現(xiàn)文件只讀屬性的設(shè)置與取消

在Pycharm中對(duì)代碼進(jìn)行注釋和縮進(jìn)的方法詳解