MCPcopy Create free account
hub / github.com/MaureenZOU/TSAM / BaseDataLoader

Class BaseDataLoader

src/base/base_data_loader.py:7–56  ·  view source on GitHub ↗

Base class for all data loaders

Source from the content-addressed store, hash-verified

5
6
7class BaseDataLoader(DataLoader):
8 """
9 Base class for all data loaders
10 """
11 def __init__(self, dataset, batch_size, shuffle, validation_split, num_workers, collate_fn=default_collate):
12 self.validation_split = validation_split
13 self.shuffle = shuffle
14
15 self.batch_idx = 0
16 self.n_samples = len(dataset)
17
18 self.sampler, self.valid_sampler = self._split_sampler(self.validation_split)
19
20 self.init_kwargs = {
21 'dataset': dataset,
22 'batch_size': batch_size,
23 'shuffle': self.shuffle,
24 'collate_fn': collate_fn,
25 'num_workers': num_workers
26 }
27 super(BaseDataLoader, self).__init__(sampler=self.sampler, **self.init_kwargs)
28
29 def _split_sampler(self, split):
30 if split == 0.0:
31 return None, None
32
33 idx_full = np.arange(self.n_samples)
34
35 np.random.seed(1)
36 np.random.shuffle(idx_full)
37
38 len_valid = int(self.n_samples * split)
39
40 valid_idx = idx_full[0:len_valid]
41 train_idx = np.delete(idx_full, np.arange(0, len_valid))
42
43 train_sampler = SubsetRandomSampler(train_idx)
44 valid_sampler = SubsetRandomSampler(valid_idx)
45
46 # turn off shuffle option which is mutually exclusive with sampler
47 self.shuffle = False
48 self.n_samples = len(train_idx)
49
50 return train_sampler, valid_sampler
51
52 def split_validation(self):
53 if self.valid_sampler is None:
54 return None
55 else:
56 return DataLoader(sampler=self.valid_sampler, **self.init_kwargs)

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected