DataLoader not callable

My custom dataset has the following in the init()

...
self.train_loader = torch.utils.data.DataLoader(
    self.train_set,
    batch_size=batch_size, shuffle=shuffle, batch_sampler=batch_sampler,
)

now I assume, that when in my modeling process I gonna use this DataLoader, I should be able to give batch_size to the arguments

train_loader = dataset.train_loader(batch_size=self.batch_size)

where dataset is my custom Dataset. But I get

train_loader = dataset.train_loader(batch_size=self.batch_size)
TypeError: 'DataLoader' object is not callable

Why and how to fix it?

The TypeError occurs because you are trying to call the train_loader object like a function. Instead, you should create a method within your custom dataset class to return a new DataLoader with the desired batch size. Here’s how you can do it:

  1. In your custom dataset class, remove the initialization of self.train_loader from the __init__ method.
  2. Add a new method to the custom dataset class that returns a DataLoader with the given batch size:
class MyCustomDataset(torch.utils.data.Dataset):
    def __init__(self, ...):
        # Your dataset initialization code here

    def get_train_loader(self, batch_size, shuffle=True, batch_sampler=None):
        train_loader = torch.utils.data.DataLoader(
            self.train_set,
            batch_size=batch_size, shuffle=shuffle, batch_sampler=batch_sampler,
        )
        return train_loader

  1. Now, in your modeling process, you can get a train_loader with the desired batch size like this:
    train_loader = dataset.get_train_loader(batch_size=self.batch_size)
train_loader = dataset.get_train_loader(batch_size=self.batch_size)

By using this approach, you can create a DataLoader with the specified batch size when needed without getting the TypeError