pub struct DataLoader<Src, S: Sampler, C, T = Identity> { /* private fields */ }Expand description
High-performance DataLoader with a PyTorch-like interface.
The dataset fetches items; the loader applies its Transform to each one
(T, Identity unless set with
transform), then collates them with C.
Src holds the dataset: SyncSource for a Dataset, AsyncSource for
an AsyncDataset (feature async).
Implementations§
Source§impl<D: Dataset> DataLoader<SyncSource<D>, SequentialSampler, VecCollator>
impl<D: Dataset> DataLoader<SyncSource<D>, SequentialSampler, VecCollator>
Sourcepub fn builder(
dataset: D,
) -> DataLoaderBuilder<SyncSource<D>, SequentialSampler, VecCollator>
pub fn builder( dataset: D, ) -> DataLoaderBuilder<SyncSource<D>, SequentialSampler, VecCollator>
Create a DataLoaderBuilder starting with SequentialSampler and
VecCollator as defaults.
Source§impl<Src, S, C, T> DataLoader<Src, S, C, T>
impl<Src, S, C, T> DataLoader<Src, S, C, T>
Sourcepub fn iter(&mut self) -> DataLoaderIter<Src, C, T> ⓘ
pub fn iter(&mut self) -> DataLoaderIter<Src, C, T> ⓘ
Start one epoch of iteration.
Advances the sampler (hence &mut self); the returned iterator does
not borrow the loader.
Sourcepub fn iter_with_indices(&mut self) -> IndexedIter<Src, C, T> ⓘ
pub fn iter_with_indices(&mut self) -> IndexedIter<Src, C, T> ⓘ
Start one epoch, like iter, whose batches come with
the dataset indices of their items: (indices, batch), indices[k]
being the index of the batch’s k-th item as the sampler drew it. A
failed batch is still one step of the epoch: the next batch gets the
next indices.
use dataloader_rs::{DataLoader, Dataset, RandomSampler, error::Result};
struct Squares;
impl Dataset for Squares {
type Item = usize;
fn get(&self, index: usize) -> Result<usize> { Ok(index * index) }
fn len(&self) -> usize { 10 }
}
let mut loader = DataLoader::builder(Squares)
.batch_size(4)
.sampler(RandomSampler::new(7))
.build();
for batch in loader.iter_with_indices() {
let (indices, squares) = batch.unwrap();
assert!(indices.iter().zip(squares).all(|(i, s)| i * i == s));
}Sourcepub fn batch_len(&self) -> usize
pub fn batch_len(&self) -> usize
Total number of batches for one epoch with current batch settings.
Counts what the sampler yields, so a sharding or subsampling sampler
(e.g. DistributedSampler) is accounted for.
Trait Implementations§
Source§impl<Src, S, C, T> IntoIterator for &mut DataLoader<Src, S, C, T>
impl<Src, S, C, T> IntoIterator for &mut DataLoader<Src, S, C, T>
Auto Trait Implementations§
impl<Src, S, C, T = Identity> !RefUnwindSafe for DataLoader<Src, S, C, T>
impl<Src, S, C, T = Identity> !UnwindSafe for DataLoader<Src, S, C, T>
impl<Src, S, C, T> Forget for DataLoader<Src, S, C, T>
impl<Src, S, C, T> Freeze for DataLoader<Src, S, C, T>
impl<Src, S, C, T> Send for DataLoader<Src, S, C, T>
impl<Src, S, C, T> Sync for DataLoader<Src, S, C, T>
impl<Src, S, C, T> Unpin for DataLoader<Src, S, C, T>
impl<Src, S, C, T> UnsafeUnpin for DataLoader<Src, S, C, T>
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> 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 more