I have a Dataset named unlabeled_set and a corresponding DataLoader named unlabeled_loader. To get batched data, I know that I can do the following on DataLoader.
unlabeled_loader = DataLoader(unlabeled_set, batch_size=batch_size, shuffle=True)

for img,_ in tqdm(unlabeled_loader):
    out=model(img.to(device))

which runs in the speed of 1.2it/s (that is, 0.83s/it) by tqdm.
However, if I just iterate the the dataset and use torch.cat() to get the batched data like that:
for _ in tqdm(range(53)):
    in = torch.as_tensor([])
    for j in range(128):
        in = torch.cat((in, torch.unsqueeze(unlabeled_set[_ * 128 + j][0], 0)), 0)
    out = model(in.to(device))

where 128 is the batch_size and 53 is (dataset_len / 128) . This way runs in the speed of 1.55s/it, much slower.
So my question is: Why is Dataloader faster? How to modify the latter to make it faster?