No, random_split is doing fine. The problem is the way you obtained the predictions. Can you share the code that generated those predictions?



 patrick126:

dataset_valid.dataset.targets


Here you need to only pass targets with same size of batch.
For instance here is simple snippet:
for data, target in dataloader_valid:
    # this is one batch in every iteration of loop
    predictions = model(data)  # size [batch_size, ...]
    # now targets are also size [batch_size, ...]
 

But you are not using batch for labels, you passing entire labels out of loop



 patrick126:

dataset_valid.dataset.targets


This is not a batch of data, it is your whole dataset.