You can replace the line real = real.view(cur_batch_size, -1) with real = torch.stack(real).view(cur_batch_size, -1), or real = torch.stack(real).view(-1) (depending on the dimensions of your data, you do not need to specify cur_batch_size)