9951 explained code solutions for 126 technologies


python-pytorchHow can I use a Python PyTorch DataLoader to load data?


A Python PyTorch DataLoader can be used to load data for machine learning models. To use the DataLoader, first you need to create a dataset object. This dataset object should contain the data that you want to load and a getitem() method that returns a single sample from the data.

Once the dataset object is created, you can use the DataLoader to load the data. The DataLoader takes the dataset object as an argument and can be used to batch, shuffle, and load the data.

Example code

from torch.utils.data import DataLoader

# Create dataset object
dataset = MyDataset()

# Create DataLoader
dataloader = DataLoader(dataset, batch_size=4, shuffle=True)

# Iterate over data samples
for data in dataloader:
    # Do something with the data
    pass

The code above creates a dataset object and a DataLoader object. The DataLoader object is then used to iterate over the data samples.

Code explanation

  1. from torch.utils.data import DataLoader: This imports the DataLoader class from the torch.utils.data module.

  2. dataset = MyDataset(): This creates a dataset object. The dataset object should contain the data that you want to load and a getitem() method that returns a single sample from the data.

  3. dataloader = DataLoader(dataset, batch_size=4, shuffle=True): This creates a DataLoader object. The DataLoader object takes the dataset object as an argument and can be used to batch, shuffle, and load the data.

  4. for data in dataloader:: This is used to iterate over the data samples.

Helpful links

Edit this code on GitHub