03_pytorch_computer_vision #225
Replies: 2 comments 1 reply
-
I think they've used it just to check the size of the input data |
Beta Was this translation helpful? Give feedback.
-
The DataLoader class in PyTorch returns a Python iterator. An iterator in Python is an object that allows you to iterate over a sequence of values. It is an object that implements the iterator protocol, which consists of the methods iter() and next(). The iter() method is called when an iterator is required for a container. It should return the iterator object itself. The next() method is used to retrieve the next item from the iterator. Below is an example of simple Python list converted to an iterator using my_list = [1, 2, 3, 4, 5]
# Create an iterator object from that list
my_iter = iter(my_list)
# Output: 1
print(next(my_iter))
# Output: 2
print(next(my_iter))
# Output: 3
print(next(my_iter))
# Output: 4
print(next(my_iter))
# Output: 5
print(next(my_iter))
# This will raise Error So, to answer your question, since DataLoader return type is an iterator, we can access items from it using 'next()'. |
Beta Was this translation helpful? Give feedback.
-
Check out what's inside the training dataloader
train_features_batch, train_labels_batch = next(iter(train_dataloader))
train_features_batch.shape, train_labels_batch.shape
Can someone explain why here need the next() and iter() functions? Thanks
Beta Was this translation helpful? Give feedback.
All reactions