Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question about SBI dataloader during training. #27

Open
Jiangpeiqi44 opened this issue Mar 1, 2023 · 0 comments
Open

Question about SBI dataloader during training. #27

Jiangpeiqi44 opened this issue Mar 1, 2023 · 0 comments

Comments

@Jiangpeiqi44
Copy link

Thanks for your amazing work! I have a question about the SBI dataloader : Are the SBI images kept the same within different epochs? In the source code, the worker_init_fn function is set to :np.random.seed(np.random.get_state()[1][0] + worker_id)
And I found that when num_workers > 0 , the np.random.get_state() of each worker in different epochs is the same. So the random numbers output by dataloader in different epochs is the same, does that means the SBI dataset kept the same in the training phase? Demo code is below:

import torch
from torch.utils.data import Dataset
import numpy as np
import random

class TestDataset(Dataset):
    def __init__(self):
        self.datas = np.arange(16)
        print('init')

    def __len__(self):
        return len(self.datas)

    def __getitem__(self, index):
        data = self.datas[index]
        random_data = np.random.uniform(0.0, 1.0)
        return  data, random_data
    
    def worker_init_fn(self, worker_id):
        np.random.seed(np.random.get_state()[1][0] + worker_id)

if __name__ == '__main__':
    simple_dataset = TestDataset()
    dataloader = torch.utils.data.DataLoader(simple_dataset, 
                                             batch_size=2,
                                             shuffle=True,
                                             worker_init_fn=simple_dataset.worker_init_fn,
                                             num_workers=2)
    n_epoch = 3
    seed = 5
    torch.manual_seed(seed)
    np.random.seed(seed)
    random.seed(seed)
    for epoch in range(n_epoch):
        print('epoch_%d'%epoch)
        np.random.seed(seed + epoch)
        for step, data in enumerate(dataloader):
            print(data)

And the output is :

init
epoch_0
[tensor([12,  3], dtype=torch.int32), tensor([0.1520, 0.8205], dtype=torch.float64)]
[tensor([2, 8], dtype=torch.int32), tensor([0.9544, 0.0756], dtype=torch.float64)]
[tensor([ 9, 15], dtype=torch.int32), tensor([0.7755, 0.5411], dtype=torch.float64)]
[tensor([14,  6], dtype=torch.int32), tensor([0.4624, 0.1885], dtype=torch.float64)]
[tensor([ 4, 11], dtype=torch.int32), tensor([0.3422, 0.2918], dtype=torch.float64)]
[tensor([1, 7], dtype=torch.int32), tensor([0.5054, 0.6988], dtype=torch.float64)]
[tensor([10,  0], dtype=torch.int32), tensor([0.4065, 0.4715], dtype=torch.float64)]
[tensor([13,  5], dtype=torch.int32), tensor([0.9122, 0.9069], dtype=torch.float64)]
epoch_1
[tensor([13,  8], dtype=torch.int32), tensor([0.1520, 0.8205], dtype=torch.float64)]
[tensor([15,  7], dtype=torch.int32), tensor([0.9544, 0.0756], dtype=torch.float64)]
[tensor([2, 1], dtype=torch.int32), tensor([0.7755, 0.5411], dtype=torch.float64)]
[tensor([11,  6], dtype=torch.int32), tensor([0.4624, 0.1885], dtype=torch.float64)]
[tensor([14,  4], dtype=torch.int32), tensor([0.3422, 0.2918], dtype=torch.float64)]
[tensor([12,  3], dtype=torch.int32), tensor([0.5054, 0.6988], dtype=torch.float64)]
[tensor([9, 5], dtype=torch.int32), tensor([0.4065, 0.4715], dtype=torch.float64)]
[tensor([10,  0], dtype=torch.int32), tensor([0.9122, 0.9069], dtype=torch.float64)]
epoch_2
[tensor([8, 5], dtype=torch.int32), tensor([0.1520, 0.8205], dtype=torch.float64)]
[tensor([6, 7], dtype=torch.int32), tensor([0.9544, 0.0756], dtype=torch.float64)]
[tensor([ 3, 12], dtype=torch.int32), tensor([0.7755, 0.5411], dtype=torch.float64)]
[tensor([13, 14], dtype=torch.int32), tensor([0.4624, 0.1885], dtype=torch.float64)]
[tensor([9, 1], dtype=torch.int32), tensor([0.3422, 0.2918], dtype=torch.float64)]
[tensor([15, 10], dtype=torch.int32), tensor([0.5054, 0.6988], dtype=torch.float64)]
[tensor([2, 0], dtype=torch.int32), tensor([0.4065, 0.4715], dtype=torch.float64)]
[tensor([ 4, 11], dtype=torch.int32), tensor([0.9122, 0.9069], dtype=torch.float64)]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant