# Setting seeds does not give reproducible code

**URL:** <https://discuss.pytorch.org/t/setting-seeds-does-not-give-reproducible-code/172792>\
**Category:** PyTorch Live\
**Created:** [February 17, 2023, 2:28am UTC](https://discuss.pytorch.org/t/setting-seeds-does-not-give-reproducible-code/172792 "2023-02-17T02:28:13Z")\
**Posts on this page:** 1\
**Showing post:** 5

<div class="post-metadata">

**Author:** ![shrbrh](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/s/b5e925/32.png) [@shrbrh](https://discuss.pytorch.org/u/shrbrh)\
**Post date:** [February 17, 2023, 12:01pm UTC](https://discuss.pytorch.org/t/setting-seeds-does-not-give-reproducible-code/172792/5 "2023-02-17T12:01:22Z")

</div>

@ptrblck Actually I am getting non-deterministic values for multiple outputs. Upon searching for solutions, I got a post [here](https://discuss.pytorch.org/t/is-there-a-way-to-fix-the-random-seed-of-every-workers-in-dataloader/21687/5) that says setting `num_workers = 0` for `Dataloader` works. I tried that but it still doesn’t work for me. But by making a few changes I am thinking it has something to do with the `Dataloader` part of my code. Here is the snippet:

```auto
content_iter = iter(data.DataLoader(
    content_dataset, batch_size=4,
    sampler=InfiniteSamplerWrapper(content_dataset),
    num_workers=16))
style_iter = iter(data.DataLoader(
    style_dataset, batch_size=number_of_styles,
    sampler=InfiniteSamplerWrapper(style_dataset),
    num_workers=16))

```

Here, `number_of_styles` is 19.  
And the `InfiniteSamplerWrapper ` method is defined in a different file as follows:

```auto
import numpy as np
from torch.utils import data

def InfiniteSampler(n):
    # i = 0
    i = n - 1
    order = np.random.permutation(n)
    while True:
        yield order[i]
        i += 1
        if i >= n:
            np.random.seed(1)
            order = np.random.permutation(n)
            i = 0

class InfiniteSamplerWrapper(data.sampler.Sampler):
    def __init__ (self, data_source):
        self.num_samples = len(data_source)
        #print("Num samples:",self.num_samples)

    def __iter__ (self):
        return iter(InfiniteSampler(self.num_samples))

    def __len__ (self):
        return 2 ** 31

```

If I leave `np.random.seed()` as it is without specifying any number inside the braces, I get all different values from the beginning. Whereas, if I use `np.random.seed(1)` I get the exact values for the first iteration, then it starts to change from the second iteration.

---

_[View the full topic](https://discuss.pytorch.org/t/setting-seeds-does-not-give-reproducible-code/172792)._
