The return format of torch.utils.data.DataLoader?

        train_transform = transforms.Compose([transforms.RandomHorizontalFlip(), transforms.ToTensor()])
        train_set = torchvision.datasets.CIFAR10(root='./data', train=True, download=True, transform=train_transform)
        self.train_loader = torch.utils.data.DataLoader(dataset=train_set,

how to see train_loader ? why can we do for batch_num, (data, target) in enumerate(self.train_loader): ? I can not understand the format of train_loader .

train_loader is a python iterator that will return elements from your dataset batch by batch.
This allows you to use it as for data in train_loader:.

1 Like

@albanD please tell me how to get the label of the batch returned by Dataloader …
Sir please give an urgent reply…
Regards,
Adarsh

It will depend on your dataset, you dataloader will return batches of elements from your dataset.

@albanD I have actually seperated my features and labels …write now i am only feeding the features to the neural network and trying to predict the labels …so should i also forward the labels to NN in order to get it by my dataloader
please give an urgent reply sir …
Regards,
Adarsh

so should i also forward the labels to NN in order to get it by my dataloader

I think you should check out the tutorials to see better how to use dataloader and create a training loop.

1 Like

@albanD Thanks for your help!!!
Best Regards,

@albanD sir please help again…
for i,data in enumerate(dataloader):
#training starts
value,_=data
Traceback (most recent call last):

File “”, line 1, in
for i,data in enumerate(dataloader):

File “C:\Users\SHRIKANT\Anaconda3\lib\site-packages\torch\utils\data\dataloader.py”, line 819, in next
return self._process_data(data)

File “C:\Users\SHRIKANT\Anaconda3\lib\site-packages\torch\utils\data\dataloader.py”, line 846, in _process_data
data.reraise()

File “C:\Users\SHRIKANT\Anaconda3\lib\site-packages\torch_utils.py”, line 385, in reraise
raise self.exc_type(msg)

KeyError: Caught KeyError in DataLoader worker process 0.
Original Traceback (most recent call last):
File “C:\Users\SHRIKANT\Anaconda3\lib\site-packages\pandas\core\indexes\base.py”, line 2657, in get_loc
return self._engine.get_loc(key)
File “pandas/_libs/index.pyx”, line 108, in pandas._libs.index.IndexEngine.get_loc
File “pandas/_libs/index.pyx”, line 132, in pandas._libs.index.IndexEngine.get_loc
File “pandas/_libs/hashtable_class_helper.pxi”, line 1601, in pandas._libs.hashtable.PyObjectHashTable.get_item
File “pandas/_libs/hashtable_class_helper.pxi”, line 1608, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 3

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File “C:\Users\SHRIKANT\Anaconda3\lib\site-packages\torch\utils\data_utils\worker.py”, line 178, in _worker_loop
data = fetcher.fetch(index)
File “C:\Users\SHRIKANT\Anaconda3\lib\site-packages\torch\utils\data_utils\fetch.py”, line 44, in fetch
data = [self.dataset[idx] for idx in possibly_batched_index]
File “C:\Users\SHRIKANT\Anaconda3\lib\site-packages\torch\utils\data_utils\fetch.py”, line 44, in
data = [self.dataset[idx] for idx in possibly_batched_index]
File “C:\Users\SHRIKANT\Anaconda3\lib\site-packages\pandas\core\frame.py”, line 2927, in getitem
indexer = self.columns.get_loc(key)
File “C:\Users\SHRIKANT\Anaconda3\lib\site-packages\pandas\core\indexes\base.py”, line 2659, in get_loc
return self._engine.get_loc(self._maybe_cast_indexer(key))
File “pandas/_libs/index.pyx”, line 108, in pandas._libs.index.IndexEngine.get_loc
File “pandas/_libs/index.pyx”, line 132, in pandas._libs.index.IndexEngine.get_loc
File “pandas/_libs/hashtable_class_helper.pxi”, line 1601, in pandas._libs.hashtable.PyObjectHashTable.get_item
File “pandas/_libs/hashtable_class_helper.pxi”, line 1608, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 3

why am i getting this error??

Given the error, I would say some pandas related objects? I am not familiar with it so I don’t know.

1 Like