Skip to content

Commit ae507ad

Browse files
sdaultonfacebook-github-bot
authored andcommitted
use torch.random to set default seed for samplers
Summary: Using `torch.random` rather than `random` makes the default (randomly selected) seed in the sampler deterministic when the `torch.manual_seed` is set. This increases reproducibility when `torch.manual_seed` is set Reviewed By: Balandat Differential Revision: D15941742 fbshipit-source-id: 7acee2efae616958ed0c145d02677cfa0f9ff406
1 parent a8d90cc commit ae507ad

File tree

1 file changed

+2
-3
lines changed

1 file changed

+2
-3
lines changed

botorch/sampling/samplers.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
Sampler modules to be used with MC-evaluated acquisition functions.
77
"""
88

9-
import random
109
from abc import ABC, abstractmethod
1110
from typing import Optional
1211

@@ -131,7 +130,7 @@ def __init__(
131130
self._sample_shape = torch.Size([num_samples])
132131
self.collapse_batch_dims = collapse_batch_dims
133132
self.resample = resample
134-
self.seed = seed if seed is not None else random.randint(0, 1000000)
133+
self.seed = seed if seed is not None else torch.randint(0, 1000000, (1,)).item()
135134

136135
def _construct_base_samples(self, posterior: Posterior, shape: torch.Size) -> None:
137136
r"""Generate iid `N(0,1)` base samples (if necessary).
@@ -195,7 +194,7 @@ def __init__(
195194
self._sample_shape = torch.Size([num_samples])
196195
self.collapse_batch_dims = collapse_batch_dims
197196
self.resample = resample
198-
self.seed = seed if seed is not None else random.randint(0, 1_000_000)
197+
self.seed = seed if seed is not None else torch.randint(0, 1000000, (1,)).item()
199198

200199
def _construct_base_samples(self, posterior: Posterior, shape: torch.Size) -> None:
201200
r"""Generate quasi-random Normal base samples (if necessary).

0 commit comments

Comments
 (0)