The batch preparer works as follows: - First adds datasets before iterator starts. - When creating the iterator (beginning of a epoch): 1. It determines the samples to used in the iteration based on dataset_ratios. 2. It groups samples of similar length into batch (usin
| 24 | |
| 25 | |
| 26 | class BatchPreparer: |
| 27 | """ |
| 28 | The batch preparer works as follows: |
| 29 | |
| 30 | - First adds datasets before iterator starts. |
| 31 | - When creating the iterator (beginning of a epoch): |
| 32 | 1. It determines the samples to used in the iteration based on dataset_ratios. |
| 33 | 2. It groups samples of similar length into batch (using bucket_sampler or sort_sampler). |
| 34 | - Every iteration: |
| 35 | The samples are loaded in parallel using pytorch's dataloader. |
| 36 | |
| 37 | Data flow: |
| 38 | |
| 39 | - Each dataset is stored in a pytorch's Dataset (with an additional method 'get_seq_lens' implemented). |
| 40 | |
| 41 | ex: seq_lens = dataset.get_seq_lens() |
| 42 | |
| 43 | - seq_lens is a list of all samples stored in the dataset. |
| 44 | - seq_lens[i] = length of dataset[i] |
| 45 | |
| 46 | - The datasets are concatenated to a single dataset by torch.utils.data.ConcatDataset for pytorch's dataloader. |
| 47 | """ |
| 48 | |
| 49 | def __init__( |
| 50 | self, |
| 51 | batch_size, |
| 52 | collate_fn, |
| 53 | drop_last=False, |
| 54 | num_workers=0, |
| 55 | batch_wrt_length=True, |
| 56 | shuffle=True, |
| 57 | max_total_samples_per_epoch=-1, |
| 58 | max_batch_combined_size=-1, |
| 59 | batch_sampler_type="sort", |
| 60 | ): |
| 61 | """ |
| 62 | Args: |
| 63 | batch_size: |
| 64 | typical batch_size |
| 65 | collate_fn: |
| 66 | collate function used to combine samples from dataset |
| 67 | drop_last: |
| 68 | whether to drop the rest of the data that cannot form a batch |
| 69 | num_workers: |
| 70 | number of loading threads to use |
| 71 | batch_wrt_length: |
| 72 | whether to batch samples of similar sequence lengths |
| 73 | shuffle: |
| 74 | whether to shuffle data within each bucket |
| 75 | max_total_samples_per_epoch: |
| 76 | maximum total number of samples per epoch. -1: ignored |
| 77 | max_batch_combined_size: |
| 78 | limitation on batch_size * seq_len. -1: ignored |
| 79 | batch_sampler_type: |
| 80 | type of the batch sampler [bucket | sort] |
| 81 | """ |
| 82 | |
| 83 | self.batch_size = batch_size |