程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python magic method__ iter__

編輯:Python

Python One of the magic methods __iter__

A vivid scientific explanation

Last time __getitem__ Time is rough 、 Plain 、 Simply say what is an iteratable object . Here is a better one found science explain :

Python Objects that can be iterated in (Iterable) It's not a specific data type , It refers to a that stores elements Container object , And the elements in the container can pass through __iter__( ) Method or __getitem__( ) Method access .

  1. __iter__ Method is used to make objects available for … in obj Loop traversal ,__getitem__( ) The way to do this is to make the object pass through Instance name [index] To access the elements in the instance . The old ape believes that the purpose of these two methods is Python Implement a general external interface that can access the internal data of the iteratable object .

  2. An iteratable object cannot iterate independently ,Python in , Iteration is through for … in obj To complete . All iteratable objects can be used directly for… in obj Loop access , This statement actually does two things : The first thing is to call __iter__() Get an iterator , The second thing is the loop call __next__().

  3. Common iteratible objects include :
    a) Collection data type , Such as list、tuple、dict、set、str etc. ;
    b) generator (generator), Including generator and belt yield The generator function of (generator function), The next section is devoted to .

  4. How to judge whether an object is an iterative object ? The specific judgment methods are as follows :

    • utilize numpy Of iterable Method

      from numpy import iterable
      print(iterable( Instance name ))
      
    • utilize collections Modular Iterable class

      from collections import Iterable
      isinstance( Instance name , Iterable)
      

    The above contents are from CSDN Of Old ape Python, It's really the work of a master , Speak briefly 、 The key to clarity is to make people understand , bosses !

A typical example

  • Define any object , Does not define __iter__ Method :

    from numpy import iterable
    class MyList:
    def __init__(self, len: int):
    self.list = [i for i in range(len)]
    self.length = len
    def __repr__(self) -> str:
    return f"MyList({
    self.length}):{
    self.list}"
    x = MyList(10)
    for i in x:
    print(i)
    

    Running results :

    Show MyList Instances are not iterative

    • Definition __iter__ After the method

      • The following example simply implements a range(n)
      from numpy import iterable
      class MyList:
      def __init__(self, len: int):
      self.cursor = -1
      self.length = len
      def __iter__(self):
      return self
      def __next__(self):
      if self.cursor+1 < self.length:
      self.cursor += 1
      return self.cursor
      else:
      exit(1)
      def __repr__(self) -> str:
      return f"MyList({
      self.length})"
      x = MyList(10)
      print(iterable(x))
      for i in x:
      print(i)
      

      Output is :

      True
      0
      1
      2
      3
      4
      5
      6
      7
      8
      9
      

      Use next() Step by step iteration can be seen more clearly :

      from numpy import iterable
      class MyList:
      def __init__(self, len: int):
      self.cursor = -1
      self.length = len
      def __iter__(self):
      return self
      def __next__(self):
      if self.cursor+1 < self.length:
      self.cursor += 1
      return self.cursor
      else:
      exit(1)
      def __repr__(self) -> str:
      return f"MyList({
      self.length})"
      x = MyList(10)
      print(iter(x))
      print(next(x))
      print(next(x))
      print(next(x))
      print(next(x))
      for i in x:
      print(i)
      

      The output is :

      MyList(10)
      0
      1
      2
      3
      4
      5
      6
      7
      8
      9
      

Typical use of iteratable objects PyTorch Data loader :Dataloader

Why is it PyTorch The trace of common iterations in ?

  • Probably because deep learning model training often requires a large data set , Sometimes even with TB(1024GB) To calculate , as everyone knows , Most personal computers have no more than 32GB, One time handle 1TB Such large data is loaded into memory for use , It's obviously unrealistic .

  • In this case , Put the required data set Batch by batch Loading into memory for use is obviously a better and more realistic solution , This coincides with the concept and positioning of iteratable objects , It can be asserted that , Precisely because Python Iterations are naturally supported , therefore Python No other language can shake its position in the field of deep learning .

Dataloader Loading instances of data :

from cgi import test
from torch.utils.data import DataLoader
from torchvision.transforms import ToTensor
from torchvision.datasets import CIFAR10
from numpy import iterable
# Use CIFAR10 Reference data set 
test_set = CIFAR10(root='./torch2Learn/dataset/cifar10_data',
train=False, download=True, transform=ToTensor())
# Load datasets in batches , Each batch 64 individual 
test_loader = DataLoader(dataset=test_set, batch_size=64,
shuffle=True, drop_last=False)
print(type(test_set))
print(type(test_loader))
print(iterable(test_set))
print(iterable(test_loader))
print(iter(test_loader))
for step, item in enumerate(test_loader):
imgs_arr, kinds_arr = item


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved