Accessing one element from data_loader

I would like to get just one batch from the data_loader and push it through the model. I use the MNIST cpp example to make the data loader as:

auto train_loader = torch::data::make_data_loadertorch::data::samplers::SequentialSampler(std::move(train_dataset), batch_size);

But how do I access say the first or tenth batch? I don’t want to use the for loop iterator.

I tried train_loader.get(1) or but they don’t work.

this may help you, just return 10 first sample from dataset

trainset = torchvision.datasets.MNIST(root=DtSetRootPath, train=True,download=True)[0:10,:,:,:]
trainloader =, batch_size=10,shuffle=True,num_workers=0)

testset = torchvision.datasets.MNIST(root=DtSetRootPath, train=False,download=True)[0:10,]
testloader =, batch_size=10,shuffle=True, num_workers=0)

return trainloader,testloader

Thank you so much! This is very helpful. I also found a way to access trainload as

auto dat=train_loader->begin()
auto& eg2=dat->data;

I seem to have not interpreted train_loader’s type properly.