Minor revision of numpy seeding
@@ -22,17 +22,16 @@ SLOTS = [0, 1, 2]
S, P, O = SLOTS
-def _worker_init_fn(numpy_seed):
- if numpy_seed > -1:
- def result_fn(worker_num):
+def _worker_init_fn(worker_num, fixed_seed):
# ensure that NumPy uses different seeds at each worker
- np.random.seed(numpy_seed + worker_num)
- return result_fn
+ if fixed_seed:
+ # reseed based on current seed (same for all workers) and worker number
+ # (different)
+ base_seed = np.random.randint(2**32-1)
+ np.random.seed(base_seed + worker_num)
else:
- def result_fn(worker_num):
- # ensure that NumPy uses different seeds at each worker
+ # reseed fresh
np.random.seed()
- return result_fn
class TrainingJob(Job):
@@ -598,13 +597,17 @@ class TrainingJobKvsAll(TrainingJob):
) = index_KvsAll_to_torch(index)
# create dataloader
+ if self.config.get("random_seed.numpy") < 0:
+ worker_init_fn = lambda worker_num: _worker_init_fn(worker_num, False)
+ else:
+ worker_init_fn = lambda worker_num: _worker_init_fn(worker_num, True)
self.loader = torch.utils.data.DataLoader(
range(self.num_examples),
collate_fn=self._get_collate_fun(),
shuffle=True,
batch_size=self.batch_size,
num_workers=self.config.get("train.num_workers"),
- worker_init_fn=_worker_init_fn(self.config.get("random_seed.numpy")),
+ worker_init_fn=worker_init_fn,
pin_memory=self.config.get("train.pin_memory"),
)
