Last active
April 10, 2020 15:46
-
-
Save infinex/e2ef069fc7ad568b7dd0dd7fda81c42c to your computer and use it in GitHub Desktop.
torch
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| special_tok_ids = {} | |
| for tok_name, tok_symbol in tokenizer.special_tokens_map.items(): | |
| idx = tokenizer.all_special_tokens.index(tok_symbol) | |
| special_tok_ids[tok_name] = tokenizer.all_special_ids[idx] | |
| class JigSawDataset(Dataset): | |
| def __init__(self, data, special_tok_ids, is_training, targets=None): | |
| self.special_tok_ids = special_tok_ids | |
| self.is_training = is_training | |
| if self.is_training: | |
| self.targets = targets | |
| self.token_ids = data | |
| self.lengths = np.array([len(t) for t in data]) | |
| def __getitem__(self, index): | |
| if self.is_training: | |
| return (self.token_ids[index], self.lengths[index], self.targets[index]) | |
| else: | |
| return (self.token_ids[index], self.lengths[index]) | |
| def __len__(self): | |
| return len(self.lengths) | |
| def batch_sequences(self, batch): | |
| """ | |
| Do the padding and transform into torch.tensor. | |
| """ | |
| token_ids = [t[0] for t in batch] | |
| lengths = [t[1] for t in batch] | |
| assert len(token_ids) == len(lengths) | |
| if self.is_training: | |
| targets = [t[2] for t in batch] | |
| assert len(token_ids) == len(targets) | |
| # Max for paddings | |
| max_seq_len_ = max(lengths) | |
| # Pad token ids | |
| pad_idx = self.special_tok_ids['pad_token'] | |
| tk_ = [list(np.array(t).astype(int)) + [pad_idx] * (max_seq_len_ - len(t)) for t in token_ids] | |
| assert len(tk_) == len(token_ids) | |
| assert all(len(t) == max_seq_len_ for t in tk_) | |
| tk_t = torch.tensor(tk_) # (bs, max_seq_len_) | |
| if self.is_training: | |
| t_t = torch.tensor(targets) # (bs) | |
| return tk_t, t_t | |
| else: | |
| return tk_t | |
| dataset = JigSawDataset(x_train, special_tok_ids, is_training=True, targets=y_train) | |
| groups = create_lengths_groups(lengths=dataset.lengths, k=MAX_LEN) | |
| sampler = RandomSampler(dataset) | |
| sampler = GroupedBatchSampler(sampler=sampler, group_ids=groups, batch_size=BATCH_SIZE) | |
| train_dataloader = DataLoader(dataset=dataset, batch_sampler=sampler, collate_fn=dataset.batch_sequences) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # coding=utf-8 | |
| # Copyright 2019-present, the HuggingFace Inc. team and Facebook, Inc. | |
| # | |
| # Licensed under the Apache License, Version 2.0 (the "License"); | |
| # you may not use this file except in compliance with the License. | |
| # You may obtain a copy of the License at | |
| # | |
| # http://www.apache.org/licenses/LICENSE-2.0 | |
| # | |
| # Unless required by applicable law or agreed to in writing, software | |
| # distributed under the License is distributed on an "AS IS" BASIS, | |
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| # See the License for the specific language governing permissions and | |
| # limitations under the License. | |
| """ Adapted from PyTorch Vision (https://github.com/pytorch/vision/blob/master/references/detection/group_by_aspect_ratio.py) | |
| """ | |
| import bisect | |
| import copy | |
| from collections import defaultdict | |
| import numpy as np | |
| from torch.utils.data.sampler import BatchSampler, Sampler | |
| import logging | |
| logger = logging.getLogger(__name__) | |
| def _quantize(x, bins): | |
| bins = copy.deepcopy(bins) | |
| bins = sorted(bins) | |
| quantized = list(map(lambda y: bisect.bisect_right(bins, y), x)) | |
| return quantized | |
| def create_lengths_groups(lengths, k=0): | |
| bins = np.arange(start=3, stop=k, step=10).tolist() if k > 0 else [10] | |
| groups = _quantize(lengths, bins) | |
| # count number of elements per group | |
| counts = np.unique(groups, return_counts=True)[1] | |
| fbins = [0] + bins + [np.inf] | |
| logger.info("Using {} as bins for aspect lengths quantization".format(fbins)) | |
| logger.info("Count of instances per bin: {}".format(counts)) | |
| return groups | |
| class GroupedBatchSampler(BatchSampler): | |
| """ | |
| Wraps another sampler to yield a mini-batch of indices. | |
| It enforces that the batch only contain elements from the same group. | |
| It also tries to provide mini-batches which follows an ordering which is | |
| as close as possible to the ordering from the original sampler. | |
| Arguments: | |
| sampler (Sampler): Base sampler. | |
| group_ids (list[int]): If the sampler produces indices in range [0, N), | |
| `group_ids` must be a list of `N` ints which contains the group id of each sample. | |
| The group ids must be a continuous set of integers starting from | |
| 0, i.e. they must be in the range [0, num_groups). | |
| batch_size (int): Size of mini-batch. | |
| """ | |
| def __init__(self, sampler, group_ids, batch_size): | |
| if not isinstance(sampler, Sampler): | |
| raise ValueError( | |
| "sampler should be an instance of " "torch.utils.data.Sampler, but got sampler={}".format(sampler) | |
| ) | |
| self.sampler = sampler | |
| self.group_ids = group_ids | |
| self.batch_size = batch_size | |
| def __iter__(self): | |
| buffer_per_group = defaultdict(list) | |
| samples_per_group = defaultdict(list) | |
| num_batches = 0 | |
| for idx in self.sampler: | |
| group_id = self.group_ids[idx] | |
| buffer_per_group[group_id].append(idx) | |
| samples_per_group[group_id].append(idx) | |
| if len(buffer_per_group[group_id]) == self.batch_size: | |
| yield buffer_per_group[group_id] # TODO | |
| num_batches += 1 | |
| del buffer_per_group[group_id] | |
| assert len(buffer_per_group[group_id]) < self.batch_size | |
| # now we have run out of elements that satisfy | |
| # the group criteria, let's return the remaining | |
| # elements so that the size of the sampler is | |
| # deterministic | |
| expected_num_batches = len(self) | |
| num_remaining = expected_num_batches - num_batches | |
| if num_remaining > 0: | |
| # for the remaining batches, group the batches by similar lengths | |
| batch_idx = [] | |
| for group_id, idxs in sorted(buffer_per_group.items(), key=lambda x: x[0]): | |
| batch_idx.extend(idxs) | |
| if len(batch_idx) >= self.batch_size: | |
| yield batch_idx[: self.batch_size] | |
| batch_idx = batch_idx[self.batch_size :] | |
| num_remaining -= 1 | |
| if len(batch_idx) > 0: | |
| yield batch_idx | |
| num_remaining -= 1 | |
| assert num_remaining == 0 | |
| def __len__(self): | |
| """ | |
| Return the number of mini-batches rather than the number of samples. | |
| """ | |
| return (len(self.sampler) + self.batch_size - 1) // self.batch_size |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def set_random_seed(self, seed=0): | |
| tf.random.set_seed(seed) | |
| random.seed(seed) | |
| np.random.seed(seed) | |
| # torch.cuda.manual_seed_all(seed) | |
| os.environ['PYTHONHASHSEED'] = str(seed) | |
| os.environ['TF_DETERMINISTIC_OPS'] = '1' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment