Upload operators.py with huggingface_hub
Browse files- operators.py +60 -0
operators.py
CHANGED
@@ -1704,6 +1704,66 @@ class Shuffle(PagedStreamOperator):
|
|
1704 |
yield from page
|
1705 |
|
1706 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1707 |
class EncodeLabels(StreamInstanceOperator):
|
1708 |
"""Encode each value encountered in any field in 'fields' into the integers 0,1,...
|
1709 |
|
|
|
1704 |
yield from page
|
1705 |
|
1706 |
|
1707 |
+
class FeatureGroupedShuffle(Shuffle):
|
1708 |
+
"""Class for shuffling an input dataset by instance 'blocks', not on the individual instance level.
|
1709 |
+
|
1710 |
+
Example is if the dataset consists of questions with paraphrases of it, and each question falls into a topic.
|
1711 |
+
All paraphrases have the same ID value as the original.
|
1712 |
+
In this case, we may want to shuffle on grouping_features = ['question ID'],
|
1713 |
+
to keep the paraphrases and original question together.
|
1714 |
+
We may also want to group by both 'question ID' and 'topic', if the question IDs are repeated between topics.
|
1715 |
+
In this case, grouping_features = ['question ID', 'topic']
|
1716 |
+
|
1717 |
+
Args:
|
1718 |
+
grouping_features (list of strings): list of feature names to use to define the groups.
|
1719 |
+
a group is defined by each unique observed combination of data values for features in grouping_features
|
1720 |
+
shuffle_within_group (bool): whether to further shuffle the instances within each group block, keeping the block order
|
1721 |
+
|
1722 |
+
Args (of superclass):
|
1723 |
+
page_size (int): The size of each page in the stream. Defaults to 1000.
|
1724 |
+
Note: shuffle_by_grouping_features determines the unique groups (unique combinations of values of grouping_features)
|
1725 |
+
separately by page (determined by page_size). If a block of instances in the same group are split
|
1726 |
+
into separate pages (either by a page break falling in the group, or the dataset was not sorted by
|
1727 |
+
grouping_features), these instances will be shuffled separately and thus the grouping may be
|
1728 |
+
broken up by pages. If the user wants to ensure the shuffle does the grouping and shuffling
|
1729 |
+
across all pages, set the page_size to be larger than the dataset size.
|
1730 |
+
See outputs_2features_bigpage and outputs_2features_smallpage in test_grouped_shuffle.
|
1731 |
+
"""
|
1732 |
+
|
1733 |
+
grouping_features: List[str] = None
|
1734 |
+
shuffle_within_group: bool = False
|
1735 |
+
|
1736 |
+
def process(self, page: List[Dict], stream_name: Optional[str] = None) -> Generator:
|
1737 |
+
if self.grouping_features is None:
|
1738 |
+
super().process(page, stream_name)
|
1739 |
+
else:
|
1740 |
+
yield from self.shuffle_by_grouping_features(page)
|
1741 |
+
|
1742 |
+
def shuffle_by_grouping_features(self, page):
|
1743 |
+
import itertools
|
1744 |
+
from collections import defaultdict
|
1745 |
+
|
1746 |
+
groups_to_instances = defaultdict(list)
|
1747 |
+
for item in page:
|
1748 |
+
groups_to_instances[
|
1749 |
+
tuple(item[ff] for ff in self.grouping_features)
|
1750 |
+
].append(item)
|
1751 |
+
# now extract the groups (i.e., lists of dicts with order preserved)
|
1752 |
+
page_blocks = list(groups_to_instances.values())
|
1753 |
+
# and now shuffle the blocks
|
1754 |
+
self.random_generator.shuffle(page_blocks)
|
1755 |
+
if self.shuffle_within_group:
|
1756 |
+
blocks = []
|
1757 |
+
# reshuffle the instances within each block, but keep the blocks in order
|
1758 |
+
for block in page_blocks:
|
1759 |
+
self.random_generator.shuffle(block)
|
1760 |
+
blocks.append(block)
|
1761 |
+
page_blocks = blocks
|
1762 |
+
|
1763 |
+
# now flatten the list so it consists of individual dicts, but in (randomized) block order
|
1764 |
+
return list(itertools.chain(*page_blocks))
|
1765 |
+
|
1766 |
+
|
1767 |
class EncodeLabels(StreamInstanceOperator):
|
1768 |
"""Encode each value encountered in any field in 'fields' into the integers 0,1,...
|
1769 |
|