pub struct AdaptiveSampler { /* private fields */ }Expand description
Adaptive sampler that dynamically adjusts sampling strategy based on training progress
This sampler combines multiple sampling strategies and adapts the sampling distribution based on model performance, loss patterns, and sample difficulty over time.
§Examples
use torsh_data::sampler::{AdaptiveSampler, AdaptiveStrategy, Sampler};
let mut sampler = AdaptiveSampler::new(1000, 64)
.with_adaptation_rate(0.1)
.with_warmup_epochs(5)
.with_generator(42);
// Add custom strategy
sampler = sampler.add_strategy(
AdaptiveStrategy::InverseFrequency { power: 1.0 },
0.2
);
// During training, update with sample losses
let sample_indices = vec![0, 1, 2, 3, 4];
let losses = vec![0.5, 0.8, 0.3, 0.9, 0.2];
sampler.update_sample_losses(&sample_indices, &losses);
// Set current epoch for adaptation
sampler.set_epoch(10);
// Get adaptive samples
let indices: Vec<usize> = sampler.iter().collect();
assert_eq!(indices.len(), 64);Implementations§
Source§impl AdaptiveSampler
impl AdaptiveSampler
Sourcepub fn new(dataset_size: usize, num_samples: usize) -> Self
pub fn new(dataset_size: usize, num_samples: usize) -> Self
Create a new adaptive sampler
Creates a sampler with default strategies: hard sampling, uniform, and uncertainty.
§Arguments
dataset_size- Total size of the datasetnum_samples- Number of samples to select per iteration
§Examples
use torsh_data::sampler::AdaptiveSampler;
let sampler = AdaptiveSampler::new(1000, 32);
assert_eq!(sampler.len(), 32);Sourcepub fn add_strategy(self, strategy: AdaptiveStrategy, weight: f64) -> Self
pub fn add_strategy(self, strategy: AdaptiveStrategy, weight: f64) -> Self
Add a custom sampling strategy
§Arguments
strategy- The adaptive strategy to addweight- Initial weight for this strategy (will be normalized)
Sourcepub fn with_adaptation_rate(self, rate: f64) -> Self
pub fn with_adaptation_rate(self, rate: f64) -> Self
Set adaptation rate for strategy weight updates
§Arguments
rate- Adaptation rate (typically 0.01-0.2)
Sourcepub fn with_smoothing_factor(self, factor: f64) -> Self
pub fn with_smoothing_factor(self, factor: f64) -> Self
Set smoothing factor for exponential moving average of losses
§Arguments
factor- Smoothing factor (0.0-1.0, higher values = more smoothing)
Sourcepub fn with_warmup_epochs(self, epochs: usize) -> Self
pub fn with_warmup_epochs(self, epochs: usize) -> Self
Sourcepub fn with_generator(self, seed: u64) -> Self
pub fn with_generator(self, seed: u64) -> Self
Sourcepub fn current_epoch(&self) -> usize
pub fn current_epoch(&self) -> usize
Get the current epoch
Sourcepub fn warmup_epochs(&self) -> usize
pub fn warmup_epochs(&self) -> usize
Get the warmup epochs
Sourcepub fn adaptation_rate(&self) -> f64
pub fn adaptation_rate(&self) -> f64
Get the adaptation rate
Sourcepub fn smoothing_factor(&self) -> f64
pub fn smoothing_factor(&self) -> f64
Get the smoothing factor
Sourcepub fn strategy_weights(&self) -> &[f64]
pub fn strategy_weights(&self) -> &[f64]
Get the current strategy weights
Sourcepub fn strategies(&self) -> &[AdaptiveStrategy]
pub fn strategies(&self) -> &[AdaptiveStrategy]
Get the current strategies
Sourcepub fn sample_losses(&self) -> &[f64]
pub fn sample_losses(&self) -> &[f64]
Get sample losses
Sourcepub fn sample_difficulties(&self) -> &[f64]
pub fn sample_difficulties(&self) -> &[f64]
Get sample difficulties
Sourcepub fn sample_frequencies(&self) -> &[usize]
pub fn sample_frequencies(&self) -> &[usize]
Get sample frequencies
Sourcepub fn is_warming_up(&self) -> bool
pub fn is_warming_up(&self) -> bool
Check if the sampler is in warmup phase
Sourcepub fn update_sample_losses(&mut self, sample_indices: &[usize], losses: &[f64])
pub fn update_sample_losses(&mut self, sample_indices: &[usize], losses: &[f64])
Update sample losses from training
This method should be called after each training batch to update the sampler’s understanding of sample difficulty.
§Arguments
sample_indices- Indices of samples in the batchlosses- Corresponding loss values for each sample
§Panics
Panics if the lengths of sample_indices and losses don’t match.
Sourcepub fn adaptive_stats(&self) -> AdaptiveStats
pub fn adaptive_stats(&self) -> AdaptiveStats
Get statistics about the current adaptive sampling state
Trait Implementations§
Source§impl Clone for AdaptiveSampler
impl Clone for AdaptiveSampler
Source§fn clone(&self) -> AdaptiveSampler
fn clone(&self) -> AdaptiveSampler
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Sampler for AdaptiveSampler
impl Sampler for AdaptiveSampler
Source§type Iter = SamplerIterator
type Iter = SamplerIterator
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,
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,
Auto Trait Implementations§
impl Freeze for AdaptiveSampler
impl RefUnwindSafe for AdaptiveSampler
impl Send for AdaptiveSampler
impl Sync for AdaptiveSampler
impl Unpin for AdaptiveSampler
impl UnsafeUnpin for AdaptiveSampler
impl UnwindSafe for AdaptiveSampler
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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
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>
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>
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>
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
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
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.