Repository: Lightning-Universe/lightning-bolts

Issue:
add seed to random_split
@nateraw 

all the random_splits need to change to the seed:

## before
```python
        dataset_train, _ = random_split(dataset, [100, 50])
```

## now
```python
        dataset_train, _ = random_split(dataset, [100, 50]), generator=torch.Generator().manual_seed(self.seed))
```


Discussion:
Hi! thanks for your contribution!, great first issue!

Resolving patch:
diff --git a/pl_bolts/datamodules/cifar10_datamodule.py b/pl_bolts/datamodules/cifar10_datamodule.py
--- a/pl_bolts/datamodules/cifar10_datamodule.py
+++ b/pl_bolts/datamodules/cifar10_datamodule.py
@@ -1,6 +1,7 @@
 from typing import Optional, Sequence
 
 from pytorch_lightning import LightningDataModule
+import torch
 from torch.utils.data import DataLoader, random_split
 from torchvision import transforms as transform_lib
 from torchvision.datasets import CIFAR10
@@ -17,9 +18,10 @@ class CIFAR10DataModule(LightningDataModule):
     def __init__(
             self,
             data_dir,
-            val_split=5000,
-            num_workers=16,
-            batch_size=32,
+            val_split: int = 5000,
+            num_workers: int = 16,
+            batch_size: int = 32,
+            seed: int = 42,
             *args,
             **kwargs,
     ):
@@ -64,6 +66,7 @@ def __init__(
         self.val_split = val_split
         self.num_workers = num_workers
         self.batch_size = batch_size
+        self.seed = seed
 
     @property
     def num_classes(self):
@@ -88,7 +91,11 @@ def train_dataloader(self):
 
         dataset = self.DATASET(self.data_dir, train=True, download=False, transform=transforms, **self.extra_args)
         train_length = len(dataset)
-        dataset_train, _ = random_split(dataset, [train_length - self.val_split, self.val_split])
+        dataset_train, _ = random_split(
+            dataset,
+            [train_length - self.val_split, self.val_split],
+            generator=torch.Generator().manual_seed(self.seed)
+        )
         loader = DataLoader(
             dataset_train,
             batch_size=self.batch_size,
@@ -107,7 +114,11 @@ def val_dataloader(self):
 
         dataset = self.DATASET(self.data_dir, train=True, download=False, transform=transforms, **self.extra_args)
         train_length = len(dataset)
-        _, dataset_val = random_split(dataset, [train_length - self.val_split, self.val_split])
+        _, dataset_val = random_split(
+            dataset,
+            [train_length - self.val_split, self.val_split],
+            generator=torch.Generator().manual_seed(self.seed)
+        )
         loader = DataLoader(
             dataset_val,
             batch_size=self.batch_size,
diff --git a/pl_bolts/datamodules/fashion_mnist_datamodule.py b/pl_bolts/datamodules/fashion_mnist_datamodule.py
--- a/pl_bolts/datamodules/fashion_mnist_datamodule.py
+++ b/pl_bolts/datamodules/fashion_mnist_datamodule.py
@@ -1,4 +1,5 @@
 from pytorch_lightning import LightningDataModule
+import torch
 from torch.utils.data import DataLoader, random_split
 from torchvision import transforms as transform_lib
 from torchvision.datasets import FashionMNIST
@@ -13,6 +14,7 @@ def __init__(
             data_dir: str,
             val_split: int = 5000,
             num_workers: int = 16,
+            seed: int = 42,
             *args,
             **kwargs,
     ):
@@ -42,6 +44,7 @@ def __init__(
         self.data_dir = data_dir
         self.val_split = val_split
         self.num_workers = num_workers
+        self.seed = seed
 
     @property
     def num_classes(self):
@@ -70,7 +73,11 @@ def train_dataloader(self, batch_size=32, transforms=None):
 
         dataset = FashionMNIST(self.data_dir, train=True, download=False, transform=transforms)
         train_length = len(dataset)
-        dataset_train, _ = random_split(dataset, [train_length - self.val_split, self.val_split])
+        dataset_train, _ = random_split(
+            dataset,
+            [train_length - self.val_split, self.val_split],
+            generator=torch.Generator().manual_seed(self.seed)
+        )
         loader = DataLoader(
             dataset_train,
             batch_size=batch_size,
@@ -93,7 +100,11 @@ def val_dataloader(self, batch_size=32, transforms=None):
 
         dataset = FashionMNIST(self.data_dir, train=True, download=True, transform=transforms)
         train_length = len(dataset)
-        _, dataset_val = random_split(dataset, [train_length - self.val_split, self.val_split])
+        _, dataset_val = random_split(
+            dataset,
+            [train_length - self.val_split, self.val_split],
+            generator=torch.Generator().manual_seed(self.seed)
+        )
         loader = DataLoader(
             dataset_val,
             batch_size=batch_size,
diff --git a/pl_bolts/datamodules/mnist_datamodule.py b/pl_bolts/datamodules/mnist_datamodule.py
--- a/pl_bolts/datamodules/mnist_datamodule.py
+++ b/pl_bolts/datamodules/mnist_datamodule.py
@@ -1,3 +1,4 @@
+import torch
 from pytorch_lightning import LightningDataModule
 from torch.utils.data import DataLoader, random_split
 from torchvision import transforms as transform_lib
@@ -14,6 +15,7 @@ def __init__(
             val_split: int = 5000,
             num_workers: int = 16,
             normalize: bool = False,
+            seed: int = 42,
             *args,
             **kwargs,
     ):
@@ -45,6 +47,7 @@ def __init__(
         self.val_split = val_split
         self.num_workers = num_workers
         self.normalize = normalize
+        self.seed = seed
 
     @property
     def num_classes(self):
@@ -73,7 +76,11 @@ def train_dataloader(self, batch_size=32, transforms=None):
 
         dataset = MNIST(self.data_dir, train=True, download=False, transform=transforms)
         train_length = len(dataset)
-        dataset_train, _ = random_split(dataset, [train_length - self.val_split, self.val_split])
+        dataset_train, _ = random_split(
+            dataset,
+            [train_length - self.val_split, self.val_split],
+            generator=torch.Generator().manual_seed(self.seed)
+        )
         loader = DataLoader(
             dataset_train,
             batch_size=batch_size,
@@ -95,7 +102,11 @@ def val_dataloader(self, batch_size=32, transforms=None):
         transforms = transforms or self.val_transforms or self._default_transforms()
         dataset = MNIST(self.data_dir, train=True, download=True, transform=transforms)
         train_length = len(dataset)
-        _, dataset_val = random_split(dataset, [train_length - self.val_split, self.val_split])
+        _, dataset_val = random_split(
+            dataset,
+            [train_length - self.val_split, self.val_split],
+            generator=torch.Generator().manual_seed(self.seed)
+        )
         loader = DataLoader(
             dataset_val,
             batch_size=batch_size,


Test patch: