27

I have the following code:

import torch
import numpy as np
import pandas as pd
from torch.utils.data import TensorDataset, DataLoader

# Load dataset
df = pd.read_csv(r'../iris.csv')

# Extract features and target
data = df.drop('target',axis=1).values
labels = df['target'].values

# Create tensor dataset
iris = TensorDataset(torch.FloatTensor(data),torch.LongTensor(labels))

# Create random batches
iris_loader = DataLoader(iris, batch_size=105, shuffle=True)

next(iter(iris_loader))

What does next() and iter() do in the above code? I have went through PyTorch's documentation and still can't quite understand what is next() and iter() doing here. Can anyone help in explaining this? Many thanks in advance.

Jonas
  • 121,568
  • 97
  • 310
  • 388
Leockl
  • 1,906
  • 5
  • 18
  • 51

1 Answers1

32

These are built-in functions of python, they are used for working with iterables.

Basically iter() calls the __iter__() method on the iris_loader which returns an iterator. next() then calls the __next__() method on that iterator to get the first iteration. Running next() again will get the second item of the iterator, etc.

This logic often happens 'behind the scenes', for example when running a for loop. It calls the __iter__() method on the iterable, and then calls __next__() on the returned iterator until it reaches the end of the iterator. It then raises a stopIteration and the loop stops.

Please see the documentation for further details and some nuances: https://docs.python.org/3/library/functions.html#iter

ScootCork
  • 3,411
  • 12
  • 22
  • 2
    Thanks @ScootCork. So in simple terms, can I say `iter()` just iterates a new random batch and `next()` displays this new random batch in the output? – Leockl Jun 24 '20 at 08:04
  • 6
    Terminology is important here, `iris_loader` is a iterable, passing it to `iter()` returns an iterator which you can iterate trough. You could separate the two functions to better understand what is happening. `i = iter(iris_loader)` and then `next(i)`. If you're running this interactively in a notebook try running `next(i)` a few more times. Each time you run `next(i)` it will return the next batch of size 105 of the iterator until there are no batches left. – ScootCork Jun 24 '20 at 08:18
  • That’s great @ScootCork, this makes much more sense now. Thank you so much for your detailed explanation. – Leockl Jun 24 '20 at 09:09