MCPcopy Create free account
hub / github.com/bic-L/MaxFormer / split_to_train_test_set

Function split_to_train_test_set

event/train.py:171–207  ·  view source on GitHub ↗

:param train_ratio: split the ratio of the origin dataset as the train set :type train_ratio: float :param origin_dataset: the origin dataset :type origin_dataset: torch.utils.data.Dataset :param num_classes: total classes number, e.g., ``10`` for the MNIST dataset :type num

(train_ratio: float, origin_dataset: torch.utils.data.Dataset, num_classes: int, random_split: bool = False)

Source from the content-addressed store, hash-verified

169
170
171def split_to_train_test_set(train_ratio: float, origin_dataset: torch.utils.data.Dataset, num_classes: int, random_split: bool = False):
172 '''
173 :param train_ratio: split the ratio of the origin dataset as the train set
174 :type train_ratio: float
175 :param origin_dataset: the origin dataset
176 :type origin_dataset: torch.utils.data.Dataset
177 :param num_classes: total classes number, e.g., ``10`` for the MNIST dataset
178 :type num_classes: int
179 :param random_split: If ``False``, the front ratio of samples in each classes will
180 be included in train set, while the reset will be included in test set.
181 If ``True``, this function will split samples in each classes randomly. The randomness is controlled by
182 ``numpy.randon.seed``
183 :type random_split: int
184 :return: a tuple ``(train_set, test_set)``
185 :rtype: tuple
186 '''
187 label_idx = []
188 for i in range(num_classes):
189 label_idx.append([])
190
191 for i, item in enumerate(origin_dataset):
192 y = item[1]
193 if isinstance(y, np.ndarray) or isinstance(y, torch.Tensor):
194 y = y.item()
195 label_idx[y].append(i)
196 train_idx = []
197 test_idx = []
198 if random_split:
199 for i in range(num_classes):
200 np.random.shuffle(label_idx[i])
201
202 for i in range(num_classes):
203 pos = math.ceil(label_idx[i].__len__() * train_ratio)
204 train_idx.extend(label_idx[i][0: pos])
205 test_idx.extend(label_idx[i][pos: label_idx[i].__len__()])
206
207 return torch.utils.data.Subset(origin_dataset, train_idx), torch.utils.data.Subset(origin_dataset, test_idx)
208
209
210def train_one_epoch(model, criterion, optimizer, data_loader, device, epoch, print_freq, scaler=None, T_train=None, aug=None, trival_aug=None, mixup_fn=None):

Callers 5

load_dataFunction · 0.85
Cifar10DVSFunction · 0.85
AslDVSFunction · 0.85
NavGestureWalkFunction · 0.85
NavGestureSitFunction · 0.85

Calls 1

__len__Method · 0.45

Tested by

no test coverage detected