pub struct GroupedSampler<F> { /* private fields */ }Expand description
Sampler that groups indices by a key function and samples groups together.
This sampler allows you to define custom grouping criteria and control how samples within and between groups are ordered. This is useful for scenarios where you want to process related samples together.
§Use Cases
- Sequence Data: Group by sequence ID to process complete sequences
- Hierarchical Data: Group by category for structured processing
- Batch Constraints: Ensure certain samples appear in the same batch
- Memory Efficiency: Group similar samples for better cache locality
Implementations§
Source§impl<F> GroupedSampler<F>
impl<F> GroupedSampler<F>
Sourcepub fn new<D>(dataset: &D, group_fn: F) -> Selfwhere
D: Dataset,
pub fn new<D>(dataset: &D, group_fn: F) -> Selfwhere
D: Dataset,
Create a new grouped sampler.
§Arguments
dataset- Dataset to sample fromgroup_fn- Function that maps sample index to group ID
§Examples
ⓘ
use torsh_data::sampler::{Sampler, GroupedSampler};
let dataset = DummyDataset { len: 10 };
// Group by class (assuming 3 classes)
let group_by_class = |idx: usize| idx % 3;
let sampler = GroupedSampler::new(&dataset, group_by_class);Sourcepub fn with_shuffle_groups(self, shuffle: bool) -> Self
pub fn with_shuffle_groups(self, shuffle: bool) -> Self
Sourcepub fn with_shuffle_within_groups(self, shuffle: bool) -> Self
pub fn with_shuffle_within_groups(self, shuffle: bool) -> Self
Sourcepub fn with_generator(self, seed: u64) -> Self
pub fn with_generator(self, seed: u64) -> Self
Sourcepub fn num_groups(&self) -> usize
pub fn num_groups(&self) -> usize
Get the number of groups.
Sourcepub fn group_sizes(&self) -> Vec<usize>
pub fn group_sizes(&self) -> Vec<usize>
Get the sizes of all groups.
Sourcepub fn shuffles_groups(&self) -> bool
pub fn shuffles_groups(&self) -> bool
Check if groups will be shuffled.
Sourcepub fn shuffles_within_groups(&self) -> bool
pub fn shuffles_within_groups(&self) -> bool
Check if samples within groups will be shuffled.
Trait Implementations§
Source§impl<F: Debug> Debug for GroupedSampler<F>
impl<F: Debug> Debug for GroupedSampler<F>
Source§impl<F: Send> Sampler for GroupedSampler<F>
impl<F: Send> Sampler for GroupedSampler<F>
Source§type Iter = SamplerIterator
type Iter = SamplerIterator
Iterator type returned by the sampler
Source§fn into_batch_sampler(
self,
batch_size: usize,
drop_last: bool,
) -> BatchingSampler<Self>where
Self: Sized,
fn into_batch_sampler(
self,
batch_size: usize,
drop_last: bool,
) -> BatchingSampler<Self>where
Self: Sized,
Convert this sampler into a batch sampler
Source§fn into_distributed(
self,
num_replicas: usize,
rank: usize,
) -> DistributedWrapper<Self>where
Self: Sized,
fn into_distributed(
self,
num_replicas: usize,
rank: usize,
) -> DistributedWrapper<Self>where
Self: Sized,
Create a distributed version of this sampler
Auto Trait Implementations§
impl<F> Freeze for GroupedSampler<F>
impl<F> RefUnwindSafe for GroupedSampler<F>where
F: RefUnwindSafe,
impl<F> Send for GroupedSampler<F>where
F: Send,
impl<F> Sync for GroupedSampler<F>where
F: Sync,
impl<F> Unpin for GroupedSampler<F>where
F: Unpin,
impl<F> UnsafeUnpin for GroupedSampler<F>
impl<F> UnwindSafe for GroupedSampler<F>where
F: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
The inverse inclusion map: attempts to construct
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
Checks if
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
Use with care! Same as
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
The inclusion map: converts
self to the equivalent element of its superset.