From ca9a86cc9b5b9851b4efbcc73e7252ee23e9ae23 Mon Sep 17 00:00:00 2001 From: Marcus Campbell <21266489+marcus-campbell@users.noreply.github.com> Date: Sat, 15 Aug 2026 10:01:45 -0700 Subject: [PATCH] fix: use stable seeds in PyTorch notebooks Replace process-randomized hash-derived seeds with fixed integer seeds so the PyTorch and Lightning examples initialize their RNGs consistently across Python processes. --- ...g_with_Pytorch_Lightning_and_Weights_and_Biases.ipynb | 3 ++- colabs/pytorch/Simple_PyTorch_Integration.ipynb | 9 +++++---- examples/pytorch/pytorch-intro/intro.ipynb | 9 +++++---- 3 files changed, 12 insertions(+), 9 deletions(-) diff --git a/colabs/pytorch-lightning/Supercharge_your_Training_with_Pytorch_Lightning_and_Weights_and_Biases.ipynb b/colabs/pytorch-lightning/Supercharge_your_Training_with_Pytorch_Lightning_and_Weights_and_Biases.ipynb index b2e76665..a4b69730 100644 --- a/colabs/pytorch-lightning/Supercharge_your_Training_with_Pytorch_Lightning_and_Weights_and_Biases.ipynb +++ b/colabs/pytorch-lightning/Supercharge_your_Training_with_Pytorch_Lightning_and_Weights_and_Biases.ipynb @@ -144,7 +144,8 @@ "# ⚡ PyTorch Lightning\n", "import lightning.pytorch as pl\n", "import torchmetrics\n", - "pl.seed_everything(hash(\"setting random seeds\") % 2**32 - 1)\n", + "seed = 42\n", + "pl.seed_everything(seed)\n", "\n", "# 🏋️‍♀️ Weights & Biases\n", "import wandb\n", diff --git a/colabs/pytorch/Simple_PyTorch_Integration.ipynb b/colabs/pytorch/Simple_PyTorch_Integration.ipynb index bf1a6054..39a2edbc 100644 --- a/colabs/pytorch/Simple_PyTorch_Integration.ipynb +++ b/colabs/pytorch/Simple_PyTorch_Integration.ipynb @@ -145,11 +145,12 @@ "from tqdm.auto import tqdm\n", "\n", "# Ensure deterministic behavior\n", + "seed = 42\n", "torch.backends.cudnn.deterministic = True\n", - "random.seed(hash(\"setting random seeds\") % 2**32 - 1)\n", - "np.random.seed(hash(\"improves reproducibility\") % 2**32 - 1)\n", - "torch.manual_seed(hash(\"by removing stochasticity\") % 2**32 - 1)\n", - "torch.cuda.manual_seed_all(hash(\"so runs are repeatable\") % 2**32 - 1)\n", + "random.seed(seed)\n", + "np.random.seed(seed)\n", + "torch.manual_seed(seed)\n", + "torch.cuda.manual_seed_all(seed)\n", "\n", "# Device configuration\n", "device = torch.device(\"cuda:0\" if torch.cuda.is_available() else \"cpu\")\n", diff --git a/examples/pytorch/pytorch-intro/intro.ipynb b/examples/pytorch/pytorch-intro/intro.ipynb index c2a9d5d0..1ee57d34 100755 --- a/examples/pytorch/pytorch-intro/intro.ipynb +++ b/examples/pytorch/pytorch-intro/intro.ipynb @@ -53,11 +53,12 @@ "from tqdm.auto import tqdm\n", "\n", "# Ensure deterministic behavior\n", + "seed = 42\n", "torch.backends.cudnn.deterministic = True\n", - "random.seed(hash(\"setting random seeds\") % 2**32 - 1)\n", - "np.random.seed(hash(\"improves reproducibility\") % 2**32 - 1)\n", - "torch.manual_seed(hash(\"by removing stochasticity\") % 2**32 - 1)\n", - "torch.cuda.manual_seed_all(hash(\"so runs are repeatable\") % 2**32 - 1)\n", + "random.seed(seed)\n", + "np.random.seed(seed)\n", + "torch.manual_seed(seed)\n", + "torch.cuda.manual_seed_all(seed)\n", "\n", "# Device configuration\n", "device = torch.device(\"cuda:0\" if torch.cuda.is_available() else \"cpu\")"