OK, it seems like I found something interesting.
In your code, try to replace
loader = torch.utils.data.DataLoader(dataset)

with
loader = torch.utils.data.DataLoader(dataset, batch_size=100)

Whether or not the error appears depends on the batch_size. For me, it breaks when batch_size is ≥30.
Looks like exploding gradients to me.