Upload operators.py with huggingface_hub
Browse files1703 unmodified lines1704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691703 unmodified lines yield from page
class FeatureGroupedShuffle(Shuffle): """Class for shuffling an input dataset by instance 'blocks', not on the individual instance level.
Example is if the dataset consists of questions with paraphrases of it, and each question falls into a topic. All paraphrases have the same ID value as the original. In this case, we may want to shuffle on grouping_features = ['question ID'], to keep the paraphrases and original question together. We may also want to group by both 'question ID' and 'topic', if the question IDs are repeated between topics. In this case, grouping_features = ['question ID', 'topic']
Args: grouping_features (list of strings): list of feature names to use to define the groups. a group is defined by each unique observed combination of data values for features in grouping_features shuffle_within_group (bool): whether to further shuffle the instances within each group block, keeping the block order
Args (of superclass): page_size (int): The size of each page in the stream. Defaults to 1000. Note: shuffle_by_grouping_features determines the unique groups (unique combinations of values of grouping_features) separately by page (determined by page_size). If a block of instances in the same group are split into separate pages (either by a page break falling in the group, or the dataset was not sorted by grouping_features), these instances will be shuffled separately and thus the grouping may be broken up by pages. If the user wants to ensure the shuffle does the grouping and shuffling across all pages, set the page_size to be larger than the dataset size. See outputs_2features_bigpage and outputs_2features_smallpage in test_grouped_shuffle. """
grouping_features: List[str] = None shuffle_within_group: bool = False
def process(self, page: List[Dict], stream_name: Optional[str] = None) -> Generator: if self.grouping_features is None: super().process(page, stream_name) else: yield from self.shuffle_by_grouping_features(page)
def shuffle_by_grouping_features(self, page): import itertools from collections import defaultdict
groups_to_instances = defaultdict(list) for item in page: groups_to_instances[ tuple(item[ff] for ff in self.grouping_features) ].append(item) # now extract the groups (i.e., lists of dicts with order preserved) page_blocks = list(groups_to_instances.values()) # and now shuffle the blocks self.random_generator.shuffle(page_blocks) if self.shuffle_within_group: blocks = [] # reshuffle the instances within each block, but keep the blocks in order for block in page_blocks: self.random_generator.shuffle(block) blocks.append(block) page_blocks = blocks
# now flatten the list so it consists of individual dicts, but in (randomized) block order return list(itertools.chain(*page_blocks))
class EncodeLabels(StreamInstanceOperator): """Encode each value encountered in any field in 'fields' into the integers 0,1,...
operators.py+60