Skip to content

Commit 585563e

Browse files
chanshingholly1238
andauthored
add caveat on RNGs and dataloaders (#1121)
This is just to clarify the known issue (https://pytorch.org/docs/stable/notes/faq.html#my-data-loader-workers-return-identical-random-numbers) that `DataLoader`s and random number generators from external libraries (such as Numpy) may not interact as expected. I think this should be clarified in the beginner's tutorial on Datasets and DataLoaders as it uses `np.random.randint` in its `RandomCrop` example. Co-authored-by: holly1238 <77758406+holly1238@users.noreply.github.com>
1 parent be9d2e6 commit 585563e

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

beginner_source/data_loading_tutorial.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,13 @@ def __call__(self, sample):
290290
image = image.transpose((2, 0, 1))
291291
return {'image': torch.from_numpy(image),
292292
'landmarks': torch.from_numpy(landmarks)}
293-
293+
294+
######################################################################
295+
# .. note::
296+
# In the example above, `RandomCrop` uses an external library's random number generator
297+
# (in this case, Numpy's `np.random.int`). This can result in unexpected behavior with `DataLoader`
298+
# (see https://pytorch.org/docs/stable/notes/faq.html#my-data-loader-workers-return-identical-random-numbers).
299+
# In practice, it is safer to stick to PyTorch's random number generator, e.g. by using `torch.randint` instead.
294300

295301
######################################################################
296302
# Compose transforms

0 commit comments

Comments
 (0)