How to Solve PyTorch AttributeError: ‘_MultiProcessingDataLoaderIter’ object has no attribute ‘next’

by | Machine Learning, Programming, PyTorch

Introduction

If you’ve encountered the error AttributeError: '_MultiProcessingDataLoaderIter' object has no attribute 'next' or AttributeError: '_SingleProcessDataLoaderIter' object has no attribute 'next' while working with PyTorch’s DataLoader, this guide will explain why this occurs and how to fix it.

Why Does This Error Occur?

The error can manifest in two variations depending on how the DataLoader is configured:

  • For num_workers > 0 (multiprocessing): AttributeError: '_MultiProcessingDataLoaderIter' object has no attribute 'next'
  • For num_workers=0 (single-process): AttributeError: '_SingleProcessDataLoaderIter' object has no attribute 'next'

Both errors occur because the internal iterator classes in PyTorch no longer expose a .next() method. Instead, they rely on Python’s iterator protocol via the __next__() method, accessed using Python’s built-in next() function.

Replicating the Error

The following examples demonstrate how the AttributeError occurs in both single-processing (num_workers=0) and multi-processing (num_workers > 0) configurations of PyTorch’s DataLoader.

Single-Processing (num_workers=0)

Single-Processing Error Example
import torch
from torch.utils.data import DataLoader, TensorDataset

# Sample dataset
data = torch.arange(20).view(-1, 2)  # 10 samples of shape (2,)
dataset = TensorDataset(data)

# Single-process DataLoader
loader = DataLoader(dataset, batch_size=3, num_workers=0)

# Attempt to fetch the next batch
data_iter = iter(loader)
batch = data_iter.next()  # This will cause an AttributeError
Output:
AttributeError: '_SingleProcessDataLoaderIter' object has no attribute 'next'

Multi-Processing (num_workers > 0)

Multi-Processing Error Example
import torch
from torch.utils.data import DataLoader, TensorDataset

# Sample dataset
data = torch.arange(20).view(-1, 2)  # 10 samples of shape (2,)
dataset = TensorDataset(data)

# Multi-process DataLoader
loader = DataLoader(dataset, batch_size=3, num_workers=2)

# Attempt to fetch the next batch
data_iter = iter(loader)
batch = data_iter.next()  # This will cause an AttributeError
Output:
AttributeError: '_MultiProcessingDataLoaderIter' object has no attribute 'next'

Solution

💡The solution is to use the Python built-in next() function instead of calling .next() directly on the iterator.

Using next()

Replace:

data_iter.next()

With:

next(data_iter)

Example Solution Code

With num_workers=0 (Single-Process)

Single-Process DataLoader Example
import torch
from torch.utils.data import DataLoader, TensorDataset

# Sample dataset
data = torch.arange(20).view(-1, 2)  # 10 samples of shape (2,)
dataset = TensorDataset(data)

# Single-process DataLoader
loader = DataLoader(dataset, batch_size=3, num_workers=0)

# Correct way to fetch the next batch
data_iter = iter(loader)
batch = next(data_iter)
print(batch)

With num_workers > 0 (Multiprocessing)

Multiprocessing DataLoader Example
import torch
from torch.utils.data import DataLoader, TensorDataset

# Sample dataset
data = torch.arange(20).view(-1, 2)  # 10 samples of shape (2,)
dataset = TensorDataset(data)

# Multiprocessing DataLoader
loader = DataLoader(dataset, batch_size=3, num_workers=2)

# Correct way to fetch the next batch
data_iter = iter(loader)
batch = next(data_iter)
print(batch)

By using the built-in next() function, you can safely iterate through batches from a PyTorch DataLoader, regardless of whether it is configured for single-processing or multi-processing.

Summary

To avoid the AttributeError, use Python’s built-in next() function instead of directly calling .next() on a DataLoader iterator. This approach works for both single-process and multiprocessing configurations of the DataLoader.

For more information, visit the PyTorch DataLoader documentation.

Congratulations on reading to the end of this tutorial! For further reading on PyTorch, go to the Deep Learning Frameworks page.

Have fun and happy researching!

Profile Picture
Senior Advisor, Data Science | [email protected] | + posts

Suf is a senior advisor in data science with deep expertise in Natural Language Processing, Complex Networks, and Anomaly Detection. Formerly a postdoctoral research fellow, he applied advanced physics techniques to tackle real-world, data-heavy industry challenges. Before that, he was a particle physicist at the ATLAS Experiment of the Large Hadron Collider. Now, he’s focused on bringing more fun and curiosity to the world of science and research online.

Buy Me a Coffee ✨