RuntimeError: index 19 is out of bounds for dimension 0 with size 18

Hello,
I am quite new in Torch implementation, I meet a problem while running:


        data_normal, target_normal = next(dataiter_normal)
        data_normal = Variable(data_normal.cuda(async=True))		
        target_normal = Variable(target_normal.cuda(async=True))

        idx_rand = Variable(
            torch.randperm(batch_size * 2).cuda(async=True))
        
        data = torch.cat([data_tumor, data_normal])[idx_rand]
        target = torch.cat([target_tumor, target_normal])[idx_rand]
        output = model(data)
        loss = loss_fn(output, target)

it gives me error:
data = torch.cat([data_tumor, data_normal])[idx_rand]
RuntimeError: index 19 is out of bounds for dimension 0 with size 18

Could someone help me to debug this error ?

Thanks alot

Your code logic should generally work.
Could it be, that you are getting this error for the last batch of your DataLoader?
If the length of your Dataset is not divisible without a remainder by the batch size, the last batch will be smaller using the default settings.
You could just drop the last batch with drop_last=True in your DataLoader or alternatively don’t use the “global” batch_size to create your idx_rand, but get the current batch size using N = data_normal.size(0).

1 Like

Thank you. I use the first choice and it works fine now.